[英]Removing stat_summary symbols from legend categories in ggplot2
I have the following code that has created two violin plots:我有以下代码创建了两个小提琴图:
ggplot(both,aes(x=levelsname,y=loginc,fill=levelsname))+
geom_violin() +
stat_summary(fun.y = mean,
aes(shape="Mean"),
colour='black',
geom="point",
size=3)+
scale_shape_manual("Summary Statistics", values=c("Mean"="+"))+
scale_fill_manual(values=c('gray70','orange','red'))+
scale_x_discrete(name="Site Category")+
scale_y_continuous(name = "Log(Incidence/100,000")+
guides(fill=guide_legend(title = "Site Category"))+
facet_grid(~ANA)+
theme_classic()+
theme(axis.text.x=element_blank())
Everything is correct for these plots apart from the legend.除了图例之外,这些情节的一切都是正确的。 I am attempting to remove the black circles from the legend under site category and replace them with the + symbol.
我试图从站点类别下的图例中删除黑色圆圈,并将它们替换为 + 符号。 I also would like to move the + and mean legend symbol underneath the site category legend items, such that it looks like one legend.
我还想移动站点类别图例项下方的 + 和表示图例符号,使其看起来像一个图例。
You shouldn't have shape="Mean"
within the aes
call.你不应该在
aes
调用中使用shape="Mean"
。 It is not an aesthetic mapping!这不是审美映射! Having it in the
aes
makes ggplot
think you are setting shape
to be mapped to a character variable that always takes the value "mean"
.将它放在
aes
会使ggplot
认为您正在设置shape
以映射到一个始终采用值"mean"
的字符变量。 Hence the weird legend.于是有了奇怪的传说。
You can just use shape="+"
as an argument in the stat_summary
call to get the effect you want.您可以在
stat_summary
调用中使用shape="+"
作为参数来获得您想要的效果。 You'll probably have to take out the scale_shape_manual("Summary Statistics", values=c("Mean"="+"))
line as well, because there is no longer a shape scale.您可能还必须取出
scale_shape_manual("Summary Statistics", values=c("Mean"="+"))
行,因为不再有形状比例。
To answer the last part of your question, if you want to have a separate "mean" line for your legend you can add an extra level "Mean" to the variable mapped to the fill
aesthetic (then manually set its fill to transparent).要回答问题的最后一部分,如果您想为图例设置单独的“平均”线,您可以向映射到
fill
美学的变量添加一个额外的“平均”级别(然后手动将其填充设置为透明)。 See below:见下文:
d <- data.frame(x=factor(c(1,2)), y=rnorm(100))
ggplot(d, aes(x,y, group=x, fill=x)) +
geom_violin() +
stat_summary(shape="+", fun="mean", aes(fill="Mean"), geom="point", size=3) +
scale_fill_manual(values=c("blue", "red", "#00000000"), limits=c(1,2,"Mean"))
Edit: I found a way to get rid of the box around the + in the Mean line of the legend but it's a horrible hack.编辑:我找到了一种方法来摆脱图例平均线中 + 周围的框,但这是一个可怕的黑客攻击。 You need two
stat_summary
layers, one with color
set to transparent with an aesthetic mapping (so that the legend box is transparent, but this makes the legend "+" transparent also) and then second with color="black"
directly which replaces the "+" in the legend but not the box.您需要两个
stat_summary
图层,一个将color
设置为透明并带有美学映射(这样图例框是透明的,但这也使图例“+”透明),然后第二个直接使用color="black"
替换“ +”在图例中,但不是框中。
ggplot(d, aes(x,y, group=x, fill=x, color=x)) +
geom_violin() +
stat_summary(shape="+", fun="mean", aes(fill="Mean",color="Mean"), geom="point", size=3)+
stat_summary(shape="+", fun="mean",color="black", geom="point", size=3) +
theme_classic() +
scale_fill_manual(values=c("lightblue", "red", "#00000000"), limits=c(1,2,"Mean"))+
scale_color_manual(values=c("black", "black", "#00000000"), limits=c(1,2,"Mean"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.