Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alignment problem with preserve = "single" and preserve = "total" #4350

Closed
ThomasKraft opened this issue Feb 18, 2021 · 6 comments
Closed

alignment problem with preserve = "single" and preserve = "total" #4350

ThomasKraft opened this issue Feb 18, 2021 · 6 comments
Labels
bug an unexpected problem or unintended behavior positions 🥇

Comments

@ThomasKraft
Copy link

This issue is a follow-up on #2712 (comment).

The previously posted solution does not work when there are 3 grouping levels, in which case the geom_point and geom_boxplot layers no longer match up again.

Reprex:

mtcars2 <- bind_rows(mtcars, data.frame(gear = c(4,5), mpg = c(22, 19), am = c(2, 2)))
ggplot(mtcars2, aes(factor(gear), mpg, fill = factor(am))) + 
   geom_boxplot(position = position_dodge2(0.75, preserve = 'single')) +
   geom_point(position = position_dodge(0.75, preserve = 'total'))
@clauswilke
Copy link
Member

Running the reprex:

library(tidyverse)

mtcars2 <- bind_rows(mtcars, data.frame(gear = c(4,5), mpg = c(22, 19), am = c(2, 2)))
ggplot(mtcars2, aes(factor(gear), mpg, fill = factor(am))) + 
  geom_boxplot(position = position_dodge2(0.75, preserve = 'single')) +
  geom_point(position = position_dodge(0.75, preserve = 'total'))

Created on 2021-02-18 by the reprex package (v1.0.0)

Is #2801 related?

@ThomasKraft
Copy link
Author

That does seem to be a related issue -- was there a workaround to implement that solution prior to the commit which fixed the issue?

@thomasp85 thomasp85 added bug an unexpected problem or unintended behavior positions 🥇 labels Mar 24, 2021
@thomasp85 thomasp85 added this to the ggplot2 3.3.4 milestone Mar 25, 2021
@thomasp85 thomasp85 modified the milestones: ggplot2 3.3.4, ggplot2 3.4.0 May 3, 2021
@thomasp85 thomasp85 removed this from the ggplot2 3.4.0 milestone May 20, 2022
@javlon
Copy link
Contributor

javlon commented Aug 13, 2022

Is this the same issue as #3647?

@javlon
Copy link
Contributor

javlon commented Aug 14, 2022

Above PR solves the current bug. But it's not a good solution because future changes in the position selection algorithm in one of the position_dodge() and position_dodge2 functions might lead to the same misalignment problem.

Following script reproduces the same problem as in this issue and #3647:

library(tidyverse)
set.seed(42)

df <- bind_rows(lapply(1:7, function(i) {
  data.frame(
    x = i,
    y = rnorm(10*i),
    g = sample(LETTERS[seq_len(i)], 10*i, replace = T)
  )
}))

ggplot(df, aes(as.factor(x), y, fill = g)) +
  geom_boxplot(position = position_dodge2(0.75, preserve = "single")) +
  geom_point(position = position_dodge(0.75, preserve = "total"), shape = 4)+
  xlab("number of groups")

Created on 2022-08-14 by the reprex package (v2.0.1)

And the plot generated by the above PR fixes the misalignment problem.

@smouksassi
Copy link

in your plot, the position of the boxplots has changed from before and the distance between the boxplots depends on the n of groups not sure this will be a desirable visual change

@teunbrand
Copy link
Collaborator

teunbrand commented Jul 24, 2024

Is this the same issue as #3647?

Yes thanks for spotting this! I'll close this as a duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior positions 🥇
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants