How to plot a Stacked and grouped bar chart in ggplot?

This problem can be solved much more cleanly with facet_grid:

library(tidyverse)
read_tsv("tmp.tsv", col_types = "ccci") %>%  
ggplot(aes(x=month, y=count, fill=type)) + geom_col() + facet_grid(.~id)

stacked bars side-by-side

Note that you have to specify the first three columns as “character” in the col_types argument otherwise it won’t look so good. It would be even better to replace the numeric codes with something meaningful (e.g. make the months into ordered factors “January”, “February” instead of 1, 2; something similar for type and id).

Leave a Comment