I'm trying to keep the legend of one layer (smooth) and remove the legend of the other (point). I have tried shutting off the legends with guides(colour = FALSE)
and geom_point(aes(color = vs), show.legend = FALSE)
.
Edit: As this question and its answers are popular, a reproducible example seems in order:
library(ggplot2)
ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = vs)) +
geom_point(aes(shape = factor(cyl))) +
geom_line(aes(linetype = factor(gear))) +
geom_smooth(aes(fill = factor(gear), color = gear)) +
theme_bw()
There might be another solution to this:
Your code was:
You can specify the
show.legend
parameter after theaes
call:then the corresponding legend should disappear
from r cookbook, where bp is your ggplot:
Remove legend for a particular aesthetic (fill):
It can also be done when specifying the scale:
This removes all legends:
If your chart uses both
fill
andcolor
aesthetics, you can remove the legend with:As the question and user3490026's answer are a top search hit, I have made a reproducible example and a brief illustration of the suggestions made so far, together with a solution that explicitly addresses the OP's question.
One of the things that
ggplot2
does and which can be confusing is that it automatically blends certain legends when they are associated with the same variable. For instance,factor(gear)
appears twice, once forlinetype
and once forfill
, resulting in a combined legend. By contrast,gear
has its own legend entry as it is not treated as the same asfactor(gear)
. The solutions offered so far usually work well. But occasionally, you may need to override the guides. See my last example at the bottom.Remove all legends: @user3490026
Remove all legends: @duhaime
Turn off legends: @Tjebo
Remove fill so that linetype becomes visible
Same as above via the scale_fill_ function:
And now one possible answer to the OP's request
Turn some on some off ad-hoc post-hoc