wythe4
wythe4

Reputation: 135

Confidence interval box plots

I would like to create a boxplot-like graph in ggplot, except that the height of the boxes reflects 95% confidence intervals around the mean. I will eventually overlay a mean crossbar and raw data points, but the main sticking point is creating a custom "box". geom_box doesn't seem to have the capability, and I'm having trouble with geom_rect because the x-axis is categorical and so I have no idea what to put for xmin & xmax? Or maybe there's a better way than geom_rect? I know pirateplot will create graphs like this, but I find their formatting options too limiting.

Thanks, Wythe (reprex below)

id <- (1:20)
female <- rnorm(20, mean = 5, sd = 1)
male <- rnorm(20, mean = 7, sd = 2)

df <- data.frame(x,y,id) %>%
  pivot_longer(cols = !(id), names_to = 'sex', values_to = 'score')

sumdf <- df %>%
  group_by(sex) %>%
  summarize(mean = mean(score), n = n(),
            SE = sd(score)/sqrt(n),
            CI = SE*1.96)

ggplot(data = sumdf, aes(sex, mean))  +
  geom_rect(mapping = aes(ymin = mean-CI, ymax = mean+CI, xmin = ?, xmax = ?),
            position=position_dodge(.92)) 

Upvotes: 2

Views: 954

Answers (1)

user102162
user102162

Reputation: 852

You can use stat = "identity" with geom_boxplot to plot the summary statistics as-is:

ggplot(
  data = sumdf,
  mapping = aes(
    x = sex,
    lower = mean - CI,
    middle = mean,
    upper = mean + CI,
    ymin = mean - CI,
    ymax = mean + CI
    )
  ) +
  geom_boxplot(stat = "identity")

Upvotes: 1

Related Questions