Skip to content

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

Closed
@alistaire47

Description

@alistaire47

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions