[英]remove legend title in ggplot
我正在尝试删除ggplot2
中的图例标题:
df <- data.frame(
g = rep(letters[1:2], 5),
x = rnorm(10),
y = rnorm(10)
)
library(ggplot2)
ggplot(df, aes(x, y, colour=g)) +
geom_line(stat="identity") +
theme(legend.position="bottom")
我见过这个问题,但似乎没有一个解决方案对我有用。 大多数都给出了关于如何弃用opts
并改用theme
的错误。 我也尝试过各种版本的theme(legend.title=NULL)
、 theme(legend.title="")
、 theme(legend.title=element_blank)
等。典型的错误信息是:
'opts' is deprecated. Use 'theme' instead. (Deprecated; last used in version 0.9.1)
'theme_blank' is deprecated. Use 'element_blank' instead. (Deprecated; last used in version 0.9.1)
自从 0.9.3 版本发布以来,我第一次使用ggplot2
,我发现很难浏览一些变化......
你theme(legend.title=element_blank())
:只需添加theme(legend.title=element_blank())
ggplot(df, aes(x, y, colour=g)) +
geom_line(stat="identity") +
theme(legend.position="bottom") +
theme(legend.title=element_blank())
Cookbook for R 上的此页面提供了有关如何自定义图例的大量详细信息。
这也有效,并且还演示了如何更改图例标题:
ggplot(df, aes(x, y, colour=g)) +
geom_line(stat="identity") +
theme(legend.position="bottom") +
scale_color_discrete(name="")
由于一个图中可能有多个图例,因此有选择地仅删除一个标题而不留空白的方法是将scale_
函数的name
参数设置为NULL
,即
scale_fill_discrete(name = NULL)
(感谢@pascal 对另一个线程发表评论)
对于Error: 'opts' is deprecated
。 改用theme()
。 (已失效;最后在 0.9.1 版中使用)' 我将opts(title = "Boxplot - Candidate's Tweet Scores")
替换为labs(title = "Boxplot - Candidate's Tweet Scores")
。 有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.