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

前端 未结 2 986
别那么骄傲
别那么骄傲 2020-11-30 10:58

I have a data frame like below:

id    month     type    count
___  _______   ______   ______
1      1          1       10
1      1          2       09
1              


        
2条回答
  •  被撕碎了的回忆
    2020-11-30 11:02

    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)
    

    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).

提交回复
热议问题