Plot zero-count factor levels on bar chart -- ggplot2

You just have to add scale_x_discrete(drop=FALSE)

library(ggplot2)

y <- data.frame(howgood = c("Better","Better","Good","Good","Better"))
y$howgood <- factor(y$howgood, levels = c("Good", "Better", "Best"))

ggplot(y, aes(howgood)) +
    geom_bar() +
    scale_x_discrete(drop=FALSE)

Created on 2019-03-19 by the reprex package (v0.2.1)

5 Likes