# adding summary statistics to two factor boxplot

by Baltazár Tivadar   Last Updated December 29, 2017 00:26 AM

I would like to add summary statistics (e.g. 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 (e.g. 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)))
Tags :

The data in geom_text wasn't being processed properly. Also, to set the len (the y-value) for the text placement, the summarize function needs to output values for len. Here's an example:

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(aes(col=supp, fill=supp), notch=T, 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=factor(dose), supp) %>%
summarize(Count = n(),
q3 = quantile(len, 0.75),
iqr = IQR(len),
len = max(len)),
aes(label = paste0("n = ",Count, "\n")),
position = pd, size=3, show.legend = FALSE) +
theme_bw()

eipi10
December 29, 2017 00:23 AM