Skip to content

position_dodge(perserve="single") shifts everything left #2076

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
alistaire47 opened this issue Mar 20, 2017 · 2 comments
Closed

position_dodge(perserve="single") shifts everything left #2076

alistaire47 opened this issue Mar 20, 2017 · 2 comments
Labels
feature a feature request or enhancement

Comments

@alistaire47
Copy link
Contributor

The new preserve parameter of position_dodge is awesome (#1935, I think), but when applied to stat_count or stat_boxplot (maybe more), with missing levels that aren't the final one, it shifts groups to the left to fill the empty level:

library(ggplot2)

df <- data.frame(x = rep(letters[1:3], each = 3), 
                 fill = factor(c(2,2,3, 1:3, 3,3,3)))

ggplot(df, aes(x, fill = fill)) + 
    geom_bar(position = position_dodge(preserve = 'single'))

plot with left-shifted bars

ggplot(mtcars, aes(factor(vs), mpg, fill = factor(cyl, c(8,6,4)))) + 
    geom_boxplot(position = position_dodge(preserve = 'single'))

boxplot example

While that behavior makes sense given that these stats previously didn't care about empty groups didn't need drawing or spacing, I suspect most users would expect bars to keep their group position à la geom_col/stat_identity (which has 0s to keep things in place):

ggplot(data.frame(table(df)), aes(x, Freq, fill = fill)) + 
    geom_col(position = 'dodge'))

plot with normally-arranged bars

Is this desired behavior or a bug?

@has2k1
Copy link
Contributor

has2k1 commented Mar 20, 2017

I encountered this "feature" and I think it is hard to fix. At the instant when the dodging is done, the group information is insufficient. I think you would have to change/add another a group/sub-group that can be used by the position aesthetics. That would be a significant change to the underlying data model that has been constant and sufficient for a very long time.

May be there is another way, I may look at it again.

@karawoo karawoo added the feature a feature request or enhancement label Jun 12, 2017
@karawoo
Copy link
Member

karawoo commented Aug 1, 2017

The new position_dodge2() function improves this by centering each group of bars (or boxes, etc.) at their x position.

@karawoo karawoo closed this as completed Aug 1, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants