Skip to content

Respect width aes in geom_boxplot #6575

@Yunuuuu

Description

@Yunuuuu

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions