R 从ggplot2中的图例类别中删除统计摘要符号

R 从ggplot2中的图例类别中删除统计摘要符号,r,ggplot2,violin-plot,R,Ggplot2,Violin Plot,我有以下代码创建了两个小提琴图: 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_sha

我有以下代码创建了两个小提琴图:

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


除了传说之外,这些情节的一切都是正确的。我正在尝试从站点类别下的图例中删除黑色圆圈,并将其替换为+符号。我还想将+和mean图例符号移动到站点类别图例项下,使其看起来像一个图例。

您不应该在
aes
调用中使用
shape=“mean”
。这不是一个审美地图!将其置于
aes
中会使
ggplot
认为您正在将
shape
设置为映射到一个字符变量,该变量的值始终为
“mean”
。于是就有了奇怪的传说

您可以在
stat\u summary
调用中将
shape=“+”
用作参数,以获得所需的效果。您可能还需要拿出
scale\u shape\u手册(“摘要统计”,values=c(“Mean”=“+”))
行,因为不再有形状比例

要回答问题的最后一部分,如果您想为图例添加单独的“平均值”行,可以在映射到
fill
美学的变量中添加额外级别的“平均值”(然后手动将其fill设置为transparent)。见下文:

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

show.legend=FALSE
添加到stat calla dupe@camille Adding show.legend=FALSE会删除站点类别图例中的圆圈,但也会删除图例中的平均值。哦,明白了。您可以对特定图例类型使用
override.aes
,在本例中为填充图例。似乎这篇文章应该涵盖你们,其中一篇可能会有帮助well@Tjebo他们不想删除任何完整的图例,这就是那篇文章的内容。这似乎有效,我唯一的问题是,我的图例自动按字母顺序排列,导致“Mean”被放在我传说的中心,扔掉色阶。@coconn41抱歉我没想到。。您必须在
scale\u fill\u manual
层中设置订单级别。我将更新我的答案。@coconn41我已经编辑过,如果Mean不是按字母顺序排列的最后一个,那么您需要为每个刻度设置
limits()
选项中的级别顺序。
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"))