Joseph Churchill
Joseph Churchill

Reputation: 11

Adding percentiles and summary statistics to ggplot boxplot

Looking for some help with adding summary statistics like 0.25 and 0.75 percentile lines to ggplot boxplot.

My current code is as follows:

ggplot(aes(y = HT, x = LOCATION, fill = LOCATION, na.rm = TRUE), data = HD1) + geom_boxplot(notch = TRUE) +
  scale_fill_manual(breaks = c("NVRN", "PCN"), values=c("sky blue", "red"))+
  coord_cartesian(ylim=c(0,80)) +
  geom_smooth(method="lm") + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

My data is quite simple with one response and one predictor - the predictor has two categorical groups (2 locations) and I need to add the summary statistics directly onto the ggplot.

I have tried using different functions like add_summary and stat_summary to add percentile lines (in red) and means for both groups onto my ggplot. I cannot seem to figure out exactly how to do this with my code, any help would be much appreciated.

I hope this is enough information.

Thanks in advance

Upvotes: 1

Views: 341

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76460

Here is a solution.
In the case below a mean line is added. Other statistics can be added with similar code, just change the function fun=mean to the relevant function.

library(ggplot2)

HD1 <- iris[51:150, 4:5]
names(HD1) <- c("HT","LOCATION")
brks <- unique(HD1[[2]])

ggplot(data = HD1, aes(x = LOCATION, y = HT, fill = LOCATION)) + 
  geom_boxplot(notch = TRUE) +
  scale_fill_manual(breaks = brks, values = c("sky blue", "red")) +
  stat_summary(aes(ymax = ..y.., ymin = ..y..),
               geom = "errorbar", fun = mean,
               width = 0.70,
               size = 1.5, linetype = "dotted") +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())

Created on 2022-06-13 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions