Skip to content

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Jul 15, 2024

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
Copy link
Collaborator Author

teunbrand commented Jul 24, 2024

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

Copy link
Member

@thomasp85 thomasp85 left a comment

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
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
13 checks passed
@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