Delete legend ggplot 2.2

I am trying to save the legend of one layer (smoothly) and delete the legend of another (point). I tried to disable legends using guides(colour = FALSE) and geom_point(aes(color = vs), show.legend = FALSE) .

Edit : since this question and its answers are popular, the reproduced example is as follows:

 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

+149
r ggplot2 legend
Feb 25 '16 at 4:19
source share
4 answers

from r cookbook , where bp is your ggplot:

Remove legend for a specific aesthetic (fill):

 bp + guides(fill=FALSE) 

This can also be done by specifying the scale:

 bp + scale_fill_discrete(guide=FALSE) 

This removes all the legends:

 bp + theme(legend.position="none") 
+325
Feb 25 '16 at 8:48
source share

It may be different:
Your code:

 geom_point(aes(..., show.legend = FALSE)) 

You can specify the show.legend parameter after calling aes :

 geom_point(aes(...), show.legend = FALSE) 

then the corresponding legend should disappear

+42
Nov 01 '17 at 2:36 on
source share

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

 + guides(fill=FALSE, color=FALSE) 
+10
Aug 20 '18 at 2:48
source share

Since the answer to the question and user3490026 is the best search hit, I made a reproducible example and a brief illustration of the suggestions made so far, along with a solution that explicitly solves the OP question.

One of the things that ggplot2 does that can be confusing is that it automatically mixes certain legends when they are associated with the same variable. For example, factor(gear) appears twice, once for linetype and once for fill , which leads to a combined legend. On the contrary, gear has its own legend record, since it is not considered the same as factor(gear) . Suggested solutions usually work well. But sometimes you may need to redefine the guides. See my last example below.

 # 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

Delete all legends: @ user3490026

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

Delete all legends: @duhaime

 p + guides(fill = FALSE, color = FALSE, linetype = FALSE, shape = FALSE) 

Disable 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 the fill to make the line view visible.

 p + guides(fill = FALSE) 

Same as above, via the scale_fill_ function:

 p + scale_fill_discrete(guide = FALSE) 

And now one possible response to an OP request

"keep the legend of one layer (smooth) and delete the legend of the other (point)",

Turn some on some extraordinary

 p + guides(fill = guide_legend(override.aes = list(color = NA)), color = FALSE, shape = FALSE) 

enter image description here

+2
Nov 26 '18 at 5:26
source share



All Articles