tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.45k stars 2.02k forks source link

`position_dodge` not working well with `preserve = "single"` and `geom_text` (or `geom_point`) #5995

Open davidhodge931 opened 1 month ago

davidhodge931 commented 1 month ago

In CRAN version, it doesn't manage to dodge the text correctly...

library(tidyverse, quietly = TRUE)
#> Warning: package 'ggplot2' was built under R version 4.4.1
library(palmerpenguins, quietly = TRUE)

penguins |>
  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,
  )
#> Warning in max(table(panel$xmin)): no non-missing arguments to max; returning
#> -Inf

Created on 2024-07-12 with reprex v2.1.0

With DEV version downloaded just now, it errors image

teunbrand commented 1 month ago

Thanks for the report David! I'm surprised that also the old version has buggy dodging, but that might explain why it is missing from the test suite 😅 Not sure if the old version it is the same issue as #3022. But yeah, should probably get fixed!