Skip to content

Conversation

Yunuuuu
Copy link
Contributor

@Yunuuuu Yunuuuu commented Aug 23, 2025

fix #6575

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)))
    )
image

Copy link
Collaborator

@teunbrand teunbrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to add optional_aes = "width" to the StatBoxplot class, if we're going to treat it as an aesthetic.

Copy link
Collaborator

@teunbrand teunbrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely, thank you for adopting the proposals!
If you want, you can add a bulletpoint to the news file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Respect width aes in geom_boxplot
2 participants