Hope it will be easy to understand. It's basically the same example as here.
Using
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
geom_bar(position = position_dodge(preserve = "single"))
But I'm getting Error in position_dodge(preserve = "single") :
unused argument (preserve = "single")
/. ggplot2 version 2.2.1
So how to modify code
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
geom_bar(position = "dodge")
To not get this super wide bar like below but same as there.
That argument was added to position_dodge
in the development version in january. It's not yet on CRAN.
A workaround would be to calculate the statistics outside ggplot2:
ggplot(as.data.frame(with(mtcars, table(cyl = factor(cyl), vs = factor(vs)))),
aes(factor(cyl), y = Freq, fill = factor(vs))) +
geom_col(position = "dodge") +
scale_fill_discrete(drop = FALSE)
This works because the zero count is included in the data passed to the geom.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With