Skip to content

fix alignment problem #4944

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

Conversation

javlon
Copy link
Contributor

@javlon javlon commented Aug 13, 2022

Fix #4350

This PR fixes the alignment problem. The problem is fixed by creating normalized fake width.

@thomasp85
Copy link
Member

@karawoo would you be able to take a look at this?

also, @javlon have you fixed the issue pointed out by @smouksassi in #4350?

@javlon
Copy link
Contributor Author

javlon commented Aug 23, 2022

also, @javlon have you fixed the issue pointed out by @smouksassi in #4350?

Sorry I haven't seen this issue. The only way to fix mentioned issue requires changes in the position_dodge algorithm. Changes include information about the width of the boxplot (or any other object) from position_dodge2. It means position_dodge should know information about other layers which might lead to more trouble in the future than the proposed solution.

Above PR include changes only in the position_dodge2 algorithm, and now it has the same algorithm for position selection as position_dodge.

Yes, the distance between boxplots has changed slightly, but the overall width for each group is constant and hasn't changed.

Copy link
Member

@karawoo karawoo left a comment

Choose a reason for hiding this comment

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

This PR does fix the alignment of points and box plots in some cases, but I think the issue that @smouksassi mentioned about the distance between boxes is a problem. Bar charts really take a hit with this approach. Taking the example from the comments of #4350, currently they look like this:

library("tidyverse")

set.seed(123)

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), fill = g)) +
  geom_bar(position = position_dodge2(preserve = "single"))

Created on 2022-08-23 with reprex v2.0.2

With this PR the spacing changes quite a bit and doesn't look very good IMO:

And there are still other cases where box alignment won't work with points, e.g. when varwidth = TRUE

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

I don't think there's any way to address all the edge cases of alignment without a rewrite that lets layers know more about each other. And even though point/box alignment is a frequently raised issue, I'm not sure it's worth the cost of the spacing changes.

@karawoo
Copy link
Member

karawoo commented Aug 23, 2022

I should say @javlon that I really appreciate you working on this and your solution does look really good for basic boxes + points. It's just a tricky problem with a lot of edge cases, unfortunately 😕

@javlon
Copy link
Contributor Author

javlon commented Aug 24, 2022

Thank you, @karawoo, for your clear explanation of the problem. I forgot about this kind of usage of boxplots.

@javlon javlon closed this Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

alignment problem with preserve = "single" and preserve = "total"
3 participants