Skip to content

position_dodge2() should handle both point and interval geoms #2480

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
frostell opened this issue Mar 14, 2018 · 10 comments
Closed

position_dodge2() should handle both point and interval geoms #2480

frostell opened this issue Mar 14, 2018 · 10 comments
Labels
feature a feature request or enhancement layers 📈

Comments

@frostell
Copy link

Displaying raw data together with box plots can be useful.

If using both 'x' and 'fill' aesthetic geom_boxplot() handles NA's elegantly through the new position_dodge2(), but unfortunately geom_point() with a matching 'colour' aesthetic is misaligned with the boxes both when using position_dodge() and position_dodge2().

This problem only occurs when handling NA's, and position_dodge() and position_dodge2() gets it wrong in different ways:

library('tidyverse')

dat <- data.frame("value" = rnorm(n=30, mean=2, sd=0.5),
                  "group" = LETTERS[1:3],
                  "x" = factor(1:2))

dat$value[dat$group=="A"&dat$x=="1"] <- NA

ggplot(dat, aes(x=x, y=value)) +
  geom_boxplot(aes(fill=group), alpha=0.3) +
  geom_point(aes(colour=group), position=position_dodge(width=0.75), size=3, alpha=0.5)

ggplot(dat, aes(x=x, y=value)) +
  geom_boxplot(aes(fill=group), alpha=0.3) +
  geom_point(aes(colour=group), position=position_dodge2(width=0.75), size=3, alpha=0.5)
@frostell frostell changed the title enable position_dodge2() to be used in geom_point() for alignment with geom_boxplot() alignment of geom_boxplot() and geom_point() with NA's Mar 14, 2018
@frostell frostell changed the title alignment of geom_boxplot() and geom_point() with NA's misalignment of geom_boxplot() and geom_point() with NA's Mar 14, 2018
@batpigandme

This comment has been minimized.

@frostell

This comment has been minimized.

@batpigandme

This comment has been minimized.

@frostell

This comment has been minimized.

@hadley hadley added bug an unexpected problem or unintended behavior layers 📈 labels Apr 27, 2018
@hadley hadley added this to the v2.3.0 milestone Apr 27, 2018
@hadley
Copy link
Member

hadley commented May 9, 2018

Slightly more minimal reprex:

library(ggplot2)

df <- tibble::tribble(
  ~g, ~x, ~y,
  "x", "1", 1,
  "x", "1", 2,
  "x", "1", 3,
  "y", "1", NA,
  "x", "2", 1,
  "x", "2", 2,
  "x", "2", 3,
  "y", "2", 4,
  "y", "2", 5,
  "y", "2", 6
)

ggplot(df, aes(x, y, colour = g)) +
  geom_boxplot() +
  geom_point(position = position_dodge(width = 0.75))
#> Warning: Removed 1 rows containing non-finite values (stat_boxplot).
#> Warning: Removed 1 rows containing missing values (geom_point).

ggplot(df, aes(x, y, colour = g)) +
  geom_boxplot() +
  geom_point(position = position_dodge2(width = 0.75))
#> Warning: Removed 1 rows containing non-finite values (stat_boxplot).

#> Warning: Removed 1 rows containing missing values (geom_point).

Created on 2018-05-09 by the reprex package (v0.2.0).

@hadley
Copy link
Member

hadley commented May 9, 2018

I think the key problem is that position_dodge2() does not handle point geoms, where grouping is determined based on the group, not the position.

@hadley
Copy link
Member

hadley commented May 9, 2018

This is going to require more thinking about how dodging works, and it's technically a feature not a bug, so I'm going to move to a future ggplot2 release.

@hadley hadley added feature a feature request or enhancement and removed bug an unexpected problem or unintended behavior labels May 9, 2018
@hadley hadley removed this from the v2.3.0 milestone May 9, 2018
@hadley hadley changed the title misalignment of geom_boxplot() and geom_point() with NA's position_dodge2() should handle both point and interval geoms May 9, 2018
@frostell
Copy link
Author

frostell commented May 9, 2018

probably wise! (for anyone in need of an ad hoc solution, see #2481)

@hadley
Copy link
Member

hadley commented Jun 18, 2019

Closing in favour of #3022 which has more discussion

@hadley hadley closed this as completed Jun 18, 2019
@lock
Copy link

lock bot commented Dec 15, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Dec 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement layers 📈
Projects
None yet
Development

No branches or pull requests

3 participants