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()
https://i.stack.imgur.com/Im5Kh.png
from r cookbook, where bp is your ggplot:
Remove legend for a particular aesthetic (fill):
bp + guides(fill="none")
It can also be done when specifying the scale:
bp + scale_fill_discrete(guide="none")
This removes all legends:
bp + theme(legend.position="none")
There might be another solution to this: Your code was:
geom_point(aes(..., show.legend = FALSE))
You can specify the show.legend
parameter after the aes
call:
geom_point(aes(...), show.legend = FALSE)
then the corresponding legend should disappear
scale_label
command (nor does this make sense), and guide(label = FALSE)
also did not work. Thanks!
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 for linetype
and once for fill
, resulting in a combined legend. By contrast, gear
has its own legend entry as it is not treated as the same as factor(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.
# reproducible example:
library(ggplot2)
p <- 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()
https://i.stack.imgur.com/PPcHn.png
Remove all legends: @user3490026
p + theme(legend.position = "none")
Remove all legends: @duhaime
p + guides(fill = FALSE, color = FALSE, linetype = FALSE, shape = FALSE)
Turn off legends: @Tjebo
ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = vs), show.legend = FALSE) +
geom_point(aes(shape = factor(cyl)), show.legend = FALSE) +
geom_line(aes(linetype = factor(gear)), show.legend = FALSE) +
geom_smooth(aes(fill = factor(gear), color = gear), show.legend = FALSE) +
theme_bw()
Remove fill so that linetype becomes visible
p + guides(fill = FALSE)
Same as above via the scale_fill_ function:
p + scale_fill_discrete(guide = FALSE)
And now one possible answer to the OP's request
"to keep the legend of one layer (smooth) and remove the legend of the other (point)"
Turn some on some off ad-hoc post-hoc
p + guides(fill = guide_legend(override.aes = list(color = NA)),
color = FALSE,
shape = FALSE)
https://i.stack.imgur.com/HG211.png
warnings()
after you ran your code and you' ll see the reason(s) for the blue ribbon not showing up.
se
when it is not computed. Probably worth an issue at github, but I don't have the energy right now...
If your chart uses both fill
and color
aesthetics, you can remove the legend with:
+ guides(fill=FALSE, color=FALSE)
Success story sharing
theme_bw()
can interfere with any definitions done usingtheme()
. When usingtheme_bw()
, make sure to add it to the plot before you alter any other theme options.bp + theme(legend.position="none") + theme_classic()
, the legends come back. So how to remove it?bp + theme_classic() + theme(legend.position="none")
guide=FALSE
has been deprecated. Now they suggest usingguide="none"