首页 文章

删除ggplot中的图例 Headers

提问于
浏览
85

我正在尝试删除 ggplot2 中的图例 Headers :

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

enter image description here

我见过this question,似乎没有一个解决方案适合我 . 大多数人都会对如何弃用 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 ,我发现很难导航一些变化......

3 回答

  • 8

    这也有效,并演示了如何更改图例 Headers :

    ggplot(df, aes(x, y, colour=g)) +
      geom_line(stat="identity") + 
      theme(legend.position="bottom") +
      scale_color_discrete(name="")
    
  • 149

    对于 Error: 'opts' is deprecated . 请改用 theme() . (已解散;最后一次在版本0.9.1中使用)'我用 labs(title = "Boxplot - Candidate's Tweet Scores") 替换了 opts(title = "Boxplot - Candidate's Tweet Scores") . 有效!

  • 0

    你几乎就在那里:只需添加 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提供了有关如何自定义图例的大量详细信息 .

相关问题