R manual boxplot with means and standard deviations (ggplot2)

11,849

I don't think you want a boxplot in this case. You could use something like geom_errorbar from the ggplot2 package. Please provide data or sample data to make your question reproducible.

df <- data.frame(means = rnorm(20, 5, 2),
                   sds = rnorm(20),
                 feats = c(paste0("Feature ", letters[1:10])),
                 group = rep(c("group 1", "group 2"), each = 2))
head(df)
#      means        sds     feats   group
# 1 7.298374 -1.1545645 Feature a group 1
# 2 6.124870 -0.0694843 Feature b group 1
# 3 3.855704  0.3802556 Feature c group 2
# 4 6.357659  2.2822757 Feature d group 2
# 5 3.572474 -0.9488784 Feature e group 1
# 6 3.526351  2.5956482 Feature f group 1

library(ggplot2)
ggplot(df, aes(x = feats, color = group)) +
  geom_errorbar(aes(ymax = means + sds, ymin = means - sds),
                position = "dodge")

plots

Share:
11,849
lrthistlethwaite
Author by

lrthistlethwaite

Degrees: PhD in Computational Biology BS in Computer Science BS in Behavioral Neuroscience. I'm currently a Senior Data Scientist at Parenthetic, where I specialize in building deep neural models specifically for natural language processing tasks.

Updated on June 15, 2022

Comments

  • lrthistlethwaite
    lrthistlethwaite almost 2 years

    I have two groups with mean scores and standard deviations which represent how confident we are with the mean estimates. Note: I do not have raw scores, just mean estimates outputted from a model and the SD of the estimates outputted from the model, around that mean.

    I have a feature set around 20, and I want to compare for each feature the mean +/- standard deviations of each of my 2 groups. It will essentially look like this:

    enter image description here

    ggplot() seems to work with data that has the raw data and it calculates the mean and standard deviation from the arrays of each feature. boxplot() works similarly.

    Can anyone help me figure out a way to visualize my results in this way?

  • lrthistlethwaite
    lrthistlethwaite over 8 years
    This is absolutely beautiful! Thank you for such an elegant answer! I will modify my question so my original question has a reproducible code, but your illustration/code is 100% what I was talking about. Thank you, again!