Points for means aligning vertically through center of group of boxplots instead of on respective boxplot

25 views Asked by At

I'm using the most recent versions of R, R Studio, reshape2, and ggplot2 as of 3/17/2024.

I have this code:

# Create long-form data
Apples.Long <- Apples[, 2:8]
Apples.Long <- melt(Apples.Long)
names(Apples.Long) <- c("Characteristic", "Score")

# Create boxplots
ggplot(data = Apples.Long, mapping = aes(x = Characteristic, y = Score, fill = Characteristic)) +
  geom_boxplot() +
  stat_boxplot(geom = "errorbar") +
  stat_summary(fun = "mean", shape = 20) +
  labs(x = NULL, caption = "Figure 1: Boxplots of characteristics") +
  theme_bw()

which creates this figure:

Single group of boxplots with means plotted as dots

I want to create another figure but by splitting the data into two groups. I have this code to try to do that:

# Create long-form data
Apples.Long <- Apples[, 2:9]
Apples.Long <- melt(Apples.Long)
names(Apples.Long) <- c("Quality", "Characteristic", "Score")

# Create boxplots
ggplot(data = Apples.Long, mapping = aes(x = Quality, y = Score, fill = Characteristic)) +
  geom_boxplot() +
  stat_boxplot(geom = "errorbar") +
  stat_summary(fun = "mean", shape = 20) +
  labs(x = NULL, caption = "Figure 4: Boxplots of characteristics by quality") +
  theme_bw()

which creates this figure:

Two groups of boxplots with means plotted as dots

Why are the dots for the means aligning along the center of the groups? How can I fix it? Any help is appreciated. I haven't been able to find anything about it online and AI hasn't been much help either.

1

There are 1 answers

2
zephryl On BEST ANSWER

Boxplots use position_dodge2() by default, so you need to apply the same position adjustment to your mean points.

Also note that as of ggplot2 v3.5.0, you can use the staplewidth argument of geom_boxplot() to add staples rather than needing to use error bars.

library(ggplot2)

ggplot(mpg, aes(x = factor(year), y = hwy, fill = class)) +
  geom_boxplot(staplewidth = 1) +
  stat_summary(
    fun = "mean", 
    shape = 20, 
    position = position_dodge2(width = 0.75)
  ) +
  theme_bw()