Skip to content

point is misplaced if width = 0 is used in position_dodge2 with geom_point #4327

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

Closed
joelgautschi opened this issue Jan 26, 2021 · 4 comments · Fixed by #4936 or #5939
Closed

point is misplaced if width = 0 is used in position_dodge2 with geom_point #4327

joelgautschi opened this issue Jan 26, 2021 · 4 comments · Fixed by #4936 or #5939
Labels
bug an unexpected problem or unintended behavior messages requests for improvements to error, warning, or feedback messages positions 🥇

Comments

@joelgautschi
Copy link

If position_dodge2(width = 0) is used with the position argument in geom_point() (one?) is wrongly placed horizontally (i. e., wrong 'column' for discrete values). This only seems to be an issue if width = 0.
If position_dodge2() is not intended to be used with geom_point() a warning would be helpful.

library(tidyverse)

df <- tribble(~dimension, ~group, ~value,
              "dim 1", "A", 0.6,
              "dim 1", "B", 0.7,
              "dim 2", "A", 0.4,
              "dim 2", "B", 0.5)

plot <- ggplot(data = df,
               mapping = aes(x = dimension,
                             y = value,
                             color = group))  

# point in wrong column (point of 'dim 1' 'B' appears in column of 'dim 2'
plot +  geom_point(
             position = position_dodge2(width = 0))

# correct column
plot + geom_point(
  position = position_dodge2(width = 0.5))

Created on 2021-01-26 by the reprex package (v0.3.0)

@yutannihilation
Copy link
Member

Thanks. So, maybe position_dodge2() should raise an error when width <= 0?

@thomasp85 thomasp85 added bug an unexpected problem or unintended behavior messages requests for improvements to error, warning, or feedback messages 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 9, 2022

I think only width<0 should raise an error.

To fix the bug need to rewrite a condition inside the find overlaps function. Here:

if (is.na(df$xmin[i]) || is.na(df$xmax[i - 1]) || df$xmin[i] >= df$xmax[i - 1]) {

There are two solutions, I think the first one is better.

  1. Fixing it by checking xmin of the next object is strictly greater than xmax of the previous object.
    if (is.na(df$xmin[i]) || is.na(df$xmax[i - 1]) || df$xmin[i] > df$xmax[i - 1]) {
  1. Check additionally equal case that the width is more than zero.
    if (is.na(df$xmin[i]) || is.na(df$xmax[i - 1]) || df$xmin[i] > df$xmax[i - 1] ||
        (df$xmin[i] == df$xmax[i - 1] && df$xmin[i] != df$xmax[i])) {

@javlon
Copy link
Contributor

javlon commented Aug 13, 2022

I figured out that PR #4936 doesn't work correctly for one of the examples (diamonds) from the documentation.

I have tested the second solution (#4327 (comment)). It fixed the new bug and the previous one. I have added this solution with PR #4944. (I'm not sure if it is ok or not)

@yutannihilation
Copy link
Member

@javlon
Sorry, it was my fault that I didn't notice the issue. Thanks for catching.

Could you just revert the change in a different PR (hopefully with a test, but it's optional)? Your first solution was simple enough so I thought it's worth adding, but now I'm not confident that there's no corner case that df$xmin[i] == df$xmax[i - 1] && df$xmin[i] != df$xmax[i] doesn't catch well.

(I still think this should be solved by just making it error because dodging with zero width sounds meaningless...)

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 messages requests for improvements to error, warning, or feedback messages positions 🥇
Projects
None yet
4 participants