删除ggplot中的图层图例 [英] removing a layer legend in ggplot
问题描述
另一个ggplot传奇问题!
我有一个数据集形式
<$ (c(A,B),每个= 200)),
value = c(rnorm(200 ),rnorm(200,mean = 0.8))
)
值,我想绘制密度。我还想添加一条线来表示每个组的平均值,所以我:
test.cdf< - ddply (test,。(cond),summarize,value.mean = mean(value))
然后在ggplot调用:
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,color = cond),
linetype ='dashed',size = 1)
如果您运行上面的代码,您会得到一个指示每个组的图例,但也会显示一个用于平均指示线的图例。我的问题是如何摆脱 geom_vline()
的图例?
根据您使用的ggplot2版本,您会遇到此问题。在R2.14.1上使用ggplot2 vs 0.9.0我得到了这张图:
不包括vline的图例。在这个版本的ggplot2中,您可以使用 show_guide
来调整图例的出现:
ggplot(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,color = cond),
linetype ='dashed',size = 1,show_guide = TRUE)
默认情况下, show_guide = FALSE
。在旧版本中,为了省略图例,您可以添加 legend = FALSE
到 geom_vline
。添加 legend = FALSE
仍然可以在当前版本中运行,但会引发警告:
警告消息:
在geom_XXX和stat_XXX中,get(x,envir = this,inherits = inh)(this,...):
legend参数已弃用。使用show_guide = TRUE或show_guide = FALSE进行显示或禁止显示指南。
我会建议升级 ggplot2
。
Another ggplot legend question!
I have a dataset of the form
test <- data.frame(
cond = factor(rep(c("A", "B"), each=200)),
value = c(rnorm(200), rnorm(200, mean=0.8))
)
So two groups and some values and I want to plot the density. I also want to add a line indicating the mean for each group to the plot so I:
test.cdf <- ddply(test, .(cond), summarise, value.mean=mean(value))
Then in ggplot call:
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)
If you run the above code, you get a legend indicating each group, but also one for the mean indicator vline. My question is how can I get rid of the legend for the geom_vline()
?
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
.
这篇关于删除ggplot中的图层图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!