Closed
Description
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'))
ggplot(mtcars, aes(factor(vs), mpg, fill = factor(cyl, c(8,6,4)))) +
geom_boxplot(position = position_dodge(preserve = 'single'))
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'))
Is this desired behavior or a bug?