Respect `width` aes in `geom_boxplot`
Closed this issue · 2 comments
Currently, the geom_boxplot does not fully respect the width aesthetic when using aes(width = ...). This makes it difficult to control the exact width of boxplots in cases where variable widths are required.
n <- vapply(
split(mpg, ~cyl),
function(d) length(unique(d$class)),
integer(1L)
)
dplyr::mutate(mpg,
width = length(unique(class)),
median = length(unique(class)) / 2L,
.by = cyl
) |>
ggplot() +
geom_boxplot(
aes(median, hwy, group = median, width = width)
) +
facet_wrap(vars(cyl), nrow = 1L, space = "free_x", scales = "free_x") +
ggh4x::facetted_pos_scales(
lapply(n, function(max) scale_x_continuous(limits = c(0, max)))
)
Thanks for the report!
AFAICT, width is not an aesthetic in StatBoxplot, but it is a computed variable.
For that reason, StatBoxplot is not strictly obligated to obey a width aesthetic (even though GeomBoxplot is).
So the computed width overrides the data$width column leading to the behaviour you pointed out.
I agree that this is somewhat confusing, but it should be given some consideration whether width should be promoted to an (optional) aesthetic in StatBoxplot and whether this would apply to other Stats as well.
Thanks! I think that if a Geom understands the width aesthetic, the corresponding Stat should not override a mapped width. Otherwise, the width aesthetic in the Geom ends up being unhelpful.