删除图例ggplot 2.2 [英] Remove legend ggplot 2.2

查看:71
本文介绍了删除图例ggplot 2.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图保留一层的图例(平滑)并删除另一层的图例(点).我尝试用guides(colour = FALSE)geom_point(aes(color = vs), show.legend = FALSE)关闭图例.

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

推荐答案

,其中bp是您的ggplot:

from r cookbook, where bp is your ggplot:

删除图例以达到特定的美感(填充)

Remove legend for a particular aesthetic (fill):

bp + guides(fill=FALSE)

也可以在指定比例时完成:

It can also be done when specifying the scale:

bp + scale_fill_discrete(guide=FALSE)

这会删除所有图例:

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

这篇关于删除图例ggplot 2.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆