removing a layer legend in ggplot

淺唱寂寞╮ 提交于 2019-11-30 19:35:44

Depending on the version of ggplot2 you are using you get this problem. Using ggplot2 vs 0.9.0 on R2.14.1 I get this graph:

which does not include the legend for the vline. In this version of ggplot2 you can tweak the occurence of the legend using show_guide:

ggplot(test, aes(value, fill=cond)) + 
  geom_density(alpha=0.5) + 
  labs(x='Energy', y='Density', fill='Group') + 
  opts(
    panel.background=theme_blank(), 
    panel.grid.major=theme_blank(), 
    panel.grid.minor=theme_blank(), 
    panel.border=theme_blank(), 
    axis.line=theme_segment()
  ) + 
  geom_vline(data=test.cdf, aes(xintercept=value.mean, colour=cond), 
    linetype='dashed', size=1, show_guide = TRUE)

which reproduces your problem. Default, show_guide = FALSE. In older versions, you can add legend = FALSE to geom_vline in order to omit the legend. Adding legend = FALSE still works still works in the current version, but it throws a warning:

Warning message:
In get(x, envir = this, inherits = inh)(this, ...) :
  "legend" argument in geom_XXX and stat_XXX is deprecated. Use show_guide = TRUE or show_guide = FALSE for display or suppress the guide display.

I would recommend upgrading ggplot2.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!