I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this:
df
group=c(rep("a",10),rep("b",10)))
And I'm trying to make a plot:
g
g
g
g
The result looks fine with one exception. It has an extra legend showing the alpha for my geom_point layer.
How can I keep the legend showing group colors, but not the one that shows my alpha settings?
解决方案
Aesthetics can be set or mapped within a ggplot call.
An aesthetic defined within aes(...) is mapped from the data, and a legend created.
An aesthetic may also be set to a single value, by defining it outside aes().
In this case, it appears you wish to set alpha = 0.8 and map colour = group.
To do this,
Place the alpha = 0.8 outside the aes() definition.
g
g
g
g
For any mapped variable you can supress the appearance of a legend by using guide = 'none' in the appropriate scale_... call. eg.
g2
geom_line(aes(colour = group)) +
geom_point(aes(colour = group, alpha = 0.8))
g2 + scale_alpha(guide = 'none')
Which will return an identical plot
EDIT
@Joran's comment is spot-on, I've made my answer more comprehensive