Boxplots with 95% Confidence Intervals in R

11,075

Here is an ideia, with normal dist:

set.seed(123)
a = cumsum(rnorm(100))
n=length(a)
mm=mean(a)
dd=sd(a)
error <- qnorm(0.975)*dd/sqrt(n)

inf <- mm-error
sup <- mm+error

boxplot(a,col=3)
lines(c(0.75,1.25),c(inf,inf),col=4)
lines(c(0.75,1.25),c(mm,mm),col=2,lwd=2)
lines(c(0.75,1.25),c(sup,sup),col=4)
legend("topleft", c("95% CI", "Mean"), lty=1,col = c(4, 2),bty ="n")

enter image description here

Share:
11,075
Joshua
Author by

Joshua

Updated on June 04, 2022

Comments

  • Joshua
    Joshua almost 2 years

    I'm trying to generate boxplots in R that display the 95% confidence intervals of the mean but I can't find any way to display this statistic. I typically use ggplot2 for data visualisation in R but I'm open to using another package if necessary. Does anyone have any suggestions on how to do this? Thanks.