ChatGPT解决这个技术问题 Extra ChatGPT

Remove legend ggplot 2.2

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

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.
Possible duplicate of Turning off some legends in a ggplot

A
Andrew

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

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.
but when I do something like this bp + theme(legend.position="none") + theme_classic(), the legends come back. So how to remove it?
@loveR put theme_classic first: bp + theme_classic() + theme(legend.position="none")
This had no effect on my plot. I used @Tjebo 's approach successfully.
For the middle example, guide=FALSE has been deprecated. Now they suggest using guide="none"
z
zx8754

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


This is a very nice approach. More intuitive as well.
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!
P
PatrickT

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


Just noticed the blue fill color in the last plot doesn't appear on the graph, not sure why...
Type warnings() after you ran your code and you' ll see the reason(s) for the blue ribbon not showing up.
@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 You may want to update the answer to e.g. color = "none" instead of color = FALSE as the FALSE version is now deprecated.
d
duhaime

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

+ guides(fill=FALSE, color=FALSE)

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now