-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Labels
bugan unexpected problem or unintended behavioran unexpected problem or unintended behavior
Description
The default width of geom_boxplot seems to be 0.75*(resolution), whereas for stat_identity it is 0.9*(resolution). This causes unexpected behavior when using stat = "identity" in geom_boxplot, because the default width is changed. The default width of boxplots seems to be undocumented, or at least I could not find it anywhere.
This took me an annoyingly long time to figure out, and I think it'd be great if these values could be made consistent to aid in user-friendliness. Barring that, documenting the width calculation in the doc for geom_boxplot would also be very helpful.
library(tidyverse)
# Example data
ncat1 <- 2
ncat2 <- 3
a <- data.frame(cat1 = factor(rep(1:ncat1, each = ncat2*100)),
cat2 = factor(rep(rep(1:ncat2, each = 100), ncat1)),
val = rnorm(ncat1*ncat2*100))
abox <- a %>%
group_by(cat1, cat2) %>%
summarise(ymin = min(val),
lower = quantile(val, .25),
middle = median(val),
upper = quantile(val, .75),
ymax = max(val),
mean = mean(val))
# Plot boxplots and means with default boxplot stat and dodge width = 0.75
a %>%
ggplot(aes(cat1, val)) +
geom_boxplot(aes(fill = cat2)) +
geom_point(data = abox, aes(group = cat2, y = mean),
position = position_dodge(width = .75),
color = 'blue',
size = 3)
# Plot boxplots and means with identity stat, having whiskers cover entire range of data,
# and with dodge width = 0.75 (points misaligned)
abox %>%
ggplot(aes(cat1)) +
geom_boxplot(aes(fill = cat2,
ymin = ymin,
lower = lower,
middle = middle,
upper = upper,
ymax = ymax),
stat = 'identity') +
geom_point(aes(group = cat2, y = mean),
position = position_dodge(width = .75),
color = 'blue',
size = 3)
Metadata
Metadata
Assignees
Labels
bugan unexpected problem or unintended behavioran unexpected problem or unintended behavior