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

geom_boxplot + width != 1 moves boxes #2331

Closed
ldecicco-USGS opened this issue Nov 9, 2017 · 5 comments
Closed

geom_boxplot + width != 1 moves boxes #2331

ldecicco-USGS opened this issue Nov 9, 2017 · 5 comments
Labels
bug an unexpected problem or unintended behavior layers 📈
Milestone

Comments

@ldecicco-USGS
Copy link

(possibly related to #2290)
I'd like to make the width of the boxplots a bit fatter, but when I do that, the labels no longer align with the boxplot:

ggplot(data = starwars) + 
geom_boxplot(aes(x = hair_color, y = mass), width = 2) + 
scale_y_log10()

image

I would have guessed the width of the boxplot gets bigger, but the position would stay the same.

@ldecicco-USGS
Copy link
Author

I should mention, adding

position = position_dodge2(preserve = "total")

as described in #2290 eliminated my need to widen up the box plots (I'm doing a bunch of facet boxplots)...but this behavior still seems strange to me.

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

hadley commented May 9, 2018

Reprex with simpler data, although it needs a bigger width before the problem becomes apparent:

library(ggplot2)
df <- data.frame(
  x = rep(c("a", "b"), each = 3),
  y = c(1, 2, 3, 5, 6, 7)
)

ggplot(df, aes(x, y)) + 
  geom_boxplot(width = 1) +
  geom_vline(xintercept = c(0.5, 1, 1.5, 2, 2.5), alpha = 1/4) 

  
ggplot(df, aes(x, y)) + 
  geom_boxplot(width = 2) +
  geom_vline(xintercept = c(0.5, 1, 1.5, 2, 2.5), alpha = 1/4) 

ggplot(df, aes(x, y)) + 
  geom_boxplot(width = 3) +
  geom_vline(xintercept = c(0.5, 1, 1.5, 2, 2.5), alpha = 1/4) 

I bet this is because boxplots now are wide enough that they overlap, and hence the new dodging algorithm combines into a single group.

@karawoo maybe this is correct behaviour and not actually a bug?

@karawoo
Copy link
Member

karawoo commented May 9, 2018

position_dodge2() actually considers both the second and third overlapping. I think the reason that the third plot looks messed up but the second doesn't is these lines:

# If n is null, preserve total widths of elements at each position by
# dividing widths by the number of elements at that position
n <- table(df$xid)
df$new_width <- (df$xmax - df$xmin) / as.numeric(n[df$xid])

In the third width is 3 and number of elements is 2, so after this calculation the width is still greater than 1, whereas in the second it's exactly 1.

I think position_dodge2() is behaving correctly. But, I'm not entirely sure what behavior users expect when asking for geom_boxplot(width = [something >1]). I can think of two possibilities:

  1. The boxes should overlap each other. This can be done with position_dodge() (though it throws a warning). Avoiding overlap was part of the motivation for position_dodge2() in the first place, so I'm not sure how to change it to support overlap.
  2. The boxes should just appear wider on the screen, and corresponding axis ticks etc. should also be farther apart. This is so dependent on the size and aspect ratio of the plotting window anyway that to me it makes more sense to change those rather than the plotting data.

tl;dr I think the current behavior is correct. But maybe we should document somewhere that width should not be greater than 1?

@hadley
Copy link
Member

hadley commented May 9, 2018

I agree - I think this is correct behaviour.

I'm not sure how we should document since width could be greater than one if the boxplots are spread further apart (i.e. numeric x rather than categorical x). So I think we're ok here.

@lock
Copy link

lock bot commented Nov 5, 2018

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 Nov 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior layers 📈
Projects
None yet
Development

No branches or pull requests

3 participants