rcategoriesmeanboxplot

Plot boxplot with mean and standard deviation by group in data frame in R


I want to plot mean and standard deviation bar by two levels of a factor. I found the solution on Plot mean and standard deviation by category.

According to the previous solution, I just get lineplot. However, I would like to get boxplot rather than lineplot. How could I get boxplots with sd bars?


Solution

  • set.seed(101)
    y <- rnorm(100)
    x <- gl(2, 50)
    
    boxplot(y~x, las=1)
    means <- by(y, x, mean); means
    sds <- by(y, x, sd); sds
    arrows(x0=c(1,2), y0=means-sds, y1=means+sds, col="blue", angle=90, length=0.1, code = 3, lwd=2)
    points(means, pch=21, bg="red", col="blue", cex=2)
    

    enter image description here