2

I'd like to make a ggplot2 scatter plot (geom_point) where the points code for three different characteristics. So I thought of using shape, colour and fill (by using shapes 21 to 25 that can be filled). Although the plot itself looks fine, the legend doesn't show the fill colours (all the points appear black). Here is an MWE, note the black symbols for carb:

p <- ggplot(mtcars, aes(wt, mpg, shape = factor(gear),
                    fill = factor(carb), colour = factor(cyl)))
p + geom_point(size = 3) + scale_shape_manual(values = 21:25)

Is there a way to change the fill-legend so that it shows the real colours?

AnjaM
  • 2,941
  • 8
  • 39
  • 62
  • I'm not sure, but I don't think you can map `color` and `fill` to different variables in `geom_point`. You shouldn't anyway. – Roland May 10 '14 at 14:08
  • @Roland The mapping itself does work (as in the MWE), it's just the legend that doesn't. And yes, I do need to code (at least) three features in one plot, it's not going to be a diagram with meaningful numbers but just an image showing the design of an experiment, similar to this one: http://www.r-bloggers.com/plotting-microtiter-plate-maps/. However, if you have any suggestions for how else to code the third feature, I would be happy to know. – AnjaM May 10 '14 at 14:12
  • `ggplot(mtcars, aes(wt, mpg, shape = factor(gear), colour = factor(carb))) + geom_point(size = 3) + facet_grid(.~cyl)` would be a better plot. – Roland May 10 '14 at 14:17
  • @Roland Please have a look at the weblink provided. Once again: I'm not going to plot meaningful numbers but a plate design. A microtiter plate cannot be faceted. – AnjaM May 10 '14 at 14:28

1 Answers1

1

I think this works, changing the shape in the fill legend to 21:

p + geom_point(size = 3) + 
    scale_shape_manual(values = 21:25)+
      scale_fill_discrete(guide=guide_legend(override.aes=list(shape=21)))
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks a lot, that looks perfect! Do you know if it's possible to make the coloured lines around the points thicker so that they become more visible on the plot? – AnjaM May 10 '14 at 14:34
  • I was just going to suggest that. I think adjusting the linewidth (`lwd`) will help. – Ben Bolker May 10 '14 at 14:35
  • Unfortunately, setting `lwd` in `geom_point` does not do anything. – AnjaM May 10 '14 at 14:38
  • This seems to be the answer to the `lwd` story: http://stackoverflow.com/questions/17677687/change-thickness-of-a-marker-in-ggplot2 Thanks for your help! – AnjaM May 10 '14 at 15:01