Skip to content

Fix for position_dodge(preserve = "single") with location based data - #6000

Merged
teunbrand merged 2 commits into
tidyverse:mainfrom
teunbrand:dodge_x
Sep 13, 2024
Merged

Fix for position_dodge(preserve = "single") with location based data#6000
teunbrand merged 2 commits into
tidyverse:mainfrom
teunbrand:dodge_x

Conversation

@teunbrand

@teunbrand teunbrand commented Jul 15, 2024

Copy link
Copy Markdown
Collaborator

This PR aims to fix #5995 and fix #3647.

Briefly, when xmin is absent from the data, x is used to count the number of groups per location.
In addition, some guardrails are in place to not use non-existing columns in the calculation.

Reprex from issue:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

palmerpenguins::penguins |>
  dplyr::count(species, sex) |>
  ggplot(
    aes(x = sex,
        y = n,
        fill = species,
        label = n),
  ) +
  geom_col(
    position = position_dodge(preserve = "single"),
    width = 0.75,
    alpha = 0.9,
  ) +
  geom_text(
    colour = "black",
    position = position_dodge(width = 0.75, preserve = "single"),
    vjust = 1.33,
  )

Created on 2024-07-15 with reprex v2.1.1

@teunbrand

teunbrand commented Jul 24, 2024

Copy link
Copy Markdown
Collaborator Author

Accidentally also fixes #3647:

Note previously recommended as workaround in #2712 (comment) was to combine position_dodge() in point layer with position_dodge2() in boxplot layer, but this workaround is no longer necessary.

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

dodge <- position_dodge(0.75, preserve = "single")

ggplot(mtcars, aes(factor(am), mpg, fill = factor(carb))) + 
  geom_boxplot(position = dodge) +
  geom_point(position = dodge, shape = 21)

Created on 2024-07-24 with reprex v2.1.1

Comment thread R/position-dodge.R

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Apart from the ongoing discussion where I am still not convinced this LGTM

@teunbrand

Copy link
Copy Markdown
Collaborator Author

We can have another stab at ironing out details once #6101 is fleshed out

@teunbrand
teunbrand merged commit c3dd767 into tidyverse:main Sep 13, 2024
@teunbrand
teunbrand deleted the dodge_x branch September 13, 2024 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants