376

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() 

enter image description here

2
  • 7
    A minimal reproducible example would be far simpler. In the long line of code there appears to be a lot of unnecessary (to the problem at hand) code as well as it being entirely unreproducible as it currently is written.
    – mnel
    Commented Feb 25, 2016 at 4:28
  • Possible duplicate of Turning off some legends in a ggplot
    – Scransom
    Commented Oct 12, 2018 at 5:58

4 Answers 4

643

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")
5
  • 33
    As a reply to a comment by @Alex: Note that theme_bw() can interfere with any definitions done using theme(). When using theme_bw(), make sure to add it to the plot before you alter any other theme options.
    – fabern
    Commented Aug 3, 2017 at 10:50
  • 2
    but when I do something like this bp + theme(legend.position="none") + theme_classic(), the legends come back. So how to remove it?
    – loveR
    Commented Aug 26, 2019 at 13:39
  • 10
    @loveR put theme_classic first: bp + theme_classic() + theme(legend.position="none")
    – hypothesis
    Commented Oct 8, 2019 at 13:27
  • 1
    This had no effect on my plot. I used @Tjebo 's approach successfully. Commented Feb 14, 2020 at 17:14
  • For the middle example, guide=FALSE has been deprecated. Now they suggest using guide="none"
    – divibisan
    Commented Nov 18, 2021 at 23:06
121

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

2
  • 4
    This is a very nice approach. More intuitive as well.
    – patL
    Commented Dec 9, 2018 at 1:45
  • 2
    This was the solution that worked to remove letters from the legend when I used geom_text to annotate lines in color. There is no scale_label command (nor does this make sense), and guide(label = FALSE) also did not work. Thanks! Commented Aug 2, 2019 at 19:03
78

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() 

enter image description here

Remove all legends: @user3490026

p + theme(legend.position = "none")

Remove all legends: @duhaime

p + guides(fill = "none", color = "none", linetype = "none", shape = "none")

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 = "none")

Same as above via the scale_fill_discrete function:

p + scale_fill_discrete(guide = "none")

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 = "none", 
           shape = "none")  

enter image description here

3
  • 3
    Just noticed the blue fill color in the last plot doesn't appear on the graph, not sure why...
    – PatrickT
    Commented Nov 26, 2018 at 5:29
  • Type warnings() after you ran your code and you' ll see the reason(s) for the blue ribbon not showing up.
    – markus
    Commented Jan 12, 2019 at 19:09
  • @markus: Got it thanks! I won't bother to edit the example, since the issue is separate. However, it makes little sense to have a legend for se when it is not computed. Probably worth an issue at github, but I don't have the energy right now...
    – PatrickT
    Commented Jan 12, 2019 at 19:14
18

If your chart uses both fill and color aesthetics, you can remove the combined legend with:

+ guides(fill="none", color="none")

Not the answer you're looking for? Browse other questions tagged or ask your own question.