[英]remove legend title in ggplot
I'm trying to remove the title of a legend in ggplot2
:我正在尝试删除
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")
I've seen this question and none of the solutions there seem to work for me.我见过这个问题,但似乎没有一个解决方案对我有用。 Most give an error about how
opts
is deprecated and to use theme
instead.大多数都给出了关于如何弃用
opts
并改用theme
的错误。 I've also tried various versions of theme(legend.title=NULL)
, theme(legend.title="")
, theme(legend.title=element_blank)
, etc. Typical error messages are:我也尝试过各种版本的
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)
I'm using ggplot2
for the first time since version 0.9.3 was released and I'm finding it difficult to navigate some of the changes...自从 0.9.3 版本发布以来,我第一次使用
ggplot2
,我发现很难浏览一些变化......
You were almost there : just add theme(legend.title=element_blank())
你
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())
This page on Cookbook for R gives plenty of details on how to customize legends. Cookbook for R 上的此页面提供了有关如何自定义图例的大量详细信息。
This works too and also demonstrates how to change the legend title:这也有效,并且还演示了如何更改图例标题:
ggplot(df, aes(x, y, colour=g)) +
geom_line(stat="identity") +
theme(legend.position="bottom") +
scale_color_discrete(name="")
Since you may have more than one legends in a plot, a way to selectively remove just one of the titles without leaving an empty space is to set the name
argument of the scale_
function to NULL
, ie由于一个图中可能有多个图例,因此有选择地仅删除一个标题而不留空白的方法是将
scale_
函数的name
参数设置为NULL
,即
scale_fill_discrete(name = NULL)
(kudos to @pascal for a comment on another thread ) (感谢@pascal 对另一个线程发表评论)
For Error: 'opts' is deprecated
.对于
Error: 'opts' is deprecated
。 Use theme()
instead.改用
theme()
。 (Defunct; last used in version 0.9.1)' I replaced opts(title = "Boxplot - Candidate's Tweet Scores")
with labs(title = "Boxplot - Candidate's Tweet Scores")
. (已失效;最后在 0.9.1 版中使用)' 我将
opts(title = "Boxplot - Candidate's Tweet Scores")
替换为labs(title = "Boxplot - Candidate's Tweet Scores")
。 It worked!有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.