简体   繁体   中英

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

在此处输入图片说明

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. (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. Instead I get a mean line for each site box.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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