简体   繁体   English

R ggplot2:如何在子组箱形图中绘制2组的平均线?

[英]R ggplot2: How to plot a mean line for 2 groups across subgroup boxplots?

I have 13 sites that are classified into 2 groups, and I have plotted a boxplot of values for each site using ggplot2. 我有13个站点,分为2个组,并且已经使用ggplot2绘制了每个站点的值的箱线图。 They are identified by group based on the fill color. 它们根据填充颜色按组进行标识。 How can I add a mean line that shows the mean of the two groups, across the site boxplots for each group? 如何在每组的站点框图中添加一条平均线来显示两组的平均值? (ie one mean line across the boxes for group 1, one line across group 2) (即,第1组的方框中有一条平均线,第2组的方框中有一条线)

在此处输入图片说明

I have tried using "stat_summary()" but can only get it to plot lines for each mean for the individual sites, not the mean across the whole groups. 我尝试使用“ stat_summary()”,但只能使它绘制各个站点的每个均值的线,而不是整个组的均值。 (Individual mean lines are shown in image, NOT what I want) (图像中显示了各个均线,而不是我想要的)

ggplot(data, aes(y = value, x=site, fill=group)) +
  geom_boxplot() + labs(y="value")+
  theme_classic() +
  theme(legend.position="bottom", 
        axis.title.x=element_blank(),
        axis.ticks.x=element_blank(), 
        text=element_text(size=15)) +
  scale_fill_manual(values=c("#E69F00", "#56B4E9"), name="group", 
                    labels=c("1", "2")) +
  stat_summary(fun.y = mean, geom = "errorbar", 
               aes(ymax = ..y.., ymin = ..y.., group =factor(mgmt)),
               width = 0.75, linetype = "dashed")

I expect 2 mean lines, one for each group, that extend across the site boxes for each group. 我期望2条平均线,每组一条,延伸到每个组的站点框。 Instead I get a mean line for each site box. 取而代之的是,我得到每个站点框的平均线。

例如geom_hline将使您在整个图表上成为一条水平线, geom_segment可以使它更短( 前者后者 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM