简体   繁体   中英

adding summary statistics to two factor boxplot

I would like to add summary statistics (eg mean) to the boxplot which have two factors. I have tried this:

library(ggplot2)
ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
   stat_boxplot(geom = "errorbar", aes(col = supp, fill=supp), position = position_dodge(width = 0.85)) + 
   geom_boxplot(aes(col = supp, fill=supp), notch=T, notchwidth = 0.5, outlier.size=2, position =  position_dodge(width = 0.85)) +
   stat_summary(fun.y=mean, aes(supp,dose), geom="point", shape=20, size=7, color="violet", fill="violet") +
   scale_color_manual(name = "SUPP", values = c("blue", "darkgreen")) +
   scale_fill_manual(name = "SUPP", values = c("lightblue", "green"))

I got this picture:

在此处输入图片说明

It is possible somehow put the sample size of each box (eg top of the whiskers)? I have tried this:

ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
   stat_boxplot(geom = "errorbar", aes(col = supp, fill=supp), position = position_dodge(width = 0.85)) + 
geom_boxplot(aes(col = supp, fill=supp), notch=T, notchwidth = 0.5, outlier.size=2, position =  position_dodge(width = 0.85)) +
stat_summary(fun.y=mean,aes(supp,dose),geom="point", shape=20, size=7, color="violet", fill="violet") +
scale_color_manual(name = "SUPP", values = c("blue", "darkgreen")) +
scale_fill_manual(name = "SUPP", values = c("lightblue", "green")) +
geom_text(data = ToothGrowth,
          group_by(dose, supp),
          summarize(Count = n(),
                  q3 = quantile(ToothGrowth, 0.75),
                  iqr = IQR(ToothGrowth),
                  aes(x= dose, y = len,label = paste0("n = ",Count, "\n")), position = position_dodge(width = 0.75)))   

You can state the aesthetics just once by putting them in the main ggplot call and then they will apply to all of the geom layers: ggplot(ToothGrowth, aes(x = factor(dose), y = len, color=supp, fill=supp))

For the count of observations: The data summary step in geom_text isn't coded properly. Also, to set len (the y-value) for the text placement, the summarize function needs to output values for len .

To add the mean values in the correct locations on the x-axis, use stat_summary with the exact same aesthetics as the other geoms and stats. I've overridden the color aesthetic by setting the color to yellow so that the point markers will be visible on top of the box plot fill colors.

The code to implement the plot is below:

library(tidyverse)

pd = position_dodge(0.85)

ggplot(ToothGrowth, aes(x = factor(dose), y = len, color=supp, fill=supp)) +
  stat_boxplot(geom = "errorbar", position = pd) + 
  geom_boxplot(notch=TRUE, notchwidth=0.5, outlier.size=2, position=pd) +
  stat_summary(fun.y=mean, geom="point", shape=3, size=2, colour="yellow", stroke=1.5, 
               position=pd, show.legend=FALSE) +
  scale_color_manual(name = "SUPP", values = c("blue", "darkgreen")) +
  scale_fill_manual(name = "SUPP", values = c("lightblue", "green")) +
  geom_text(data = ToothGrowth %>% group_by(dose, supp) %>% 
              summarize(Count = n(),
                        len=max(len) + 0.05 * diff(range(ToothGrowth$len))),
            aes(label = paste0("n = ", Count)), 
            position = pd, size=3, show.legend = FALSE) +
  theme_bw()

在此处输入图片说明

Note that the notch goes outside the hinges for all of the box plots. Also, having the sample size just above the maximum of each boxplot seems distracting and unnecessary to me. You could place all of the text annotations at the bottom of the plot like this:

  geom_text(data = ToothGrowth %>% group_by(dose, supp) %>% 
              summarize(Count = n()) %>% 
              ungroup %>% 
              mutate(len=min(ToothGrowth$len) - 0.05 * diff(range(ToothGrowth$len))),
            aes(label = paste0("n = ", Count)), 
            position = pd, size=3, show.legend = FALSE) +

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