Skip to content

Could position_dodge(preserve = 'single') centre bars/boxs where not all categories are present like in dodge2? #6611

@davidhodge931

Description

@davidhodge931

Might not be possible, but thought I'd chuck it up just in case

See how waterpolo and gymnastics categories are treated in dodge. I'd prefer they were centred like in dodge2

library(tidyverse)
library(patchwork)

sports <- c("water polo", "swimming", "gymnastics", "field", "netball")

p1 <- ggridges::Aus_athletes |>
  dplyr::filter(sport %in% sports) |>
  ggplot(aes(height, sport, fill = sex)) +
  geom_boxplot(position = position_dodge(preserve = "single")) +
  labs(title = "position_dodge(preserve = 'single')") +
  theme(legend.position = "none")

p2 <- ggridges::Aus_athletes |>
  dplyr::filter(sport %in% sports) |>
  ggplot(aes(height, sport, fill = sex)) +
  geom_boxplot(position = position_dodge2(preserve = "single")) +
  labs(title = "position_dodge2(preserve = 'single')") +
  theme(legend.position = "none")

p1 / p2

Created on 2025-09-15 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions