我有一个数据框如下:
id month type count
___ _______ ______ ______
1 1 1 10
1 1 2 09
1 1 3 26
1 2 1 60
1 2 2 90
2 2 3 80
2 1 1 10
2 1 2 09
2 1 3 26
2 2 1 60
2 2 2 90
2 2 3 80
3 1 1 10
3 1 2 09
3 1 3 26
3 2 1 60
3 2 2 90
3 2 3 80
我认为最好的可视化方式是堆叠的组栏,如下所示:
所以我试着
ggplot(df,aes(x=id,y=count,fill=month))+geom_bar(stat="identity",position=position_dodge())+geom_text(aes(label=count),size=3)
这给出了一个和我预期的有点不同的情节。任何帮助都很感激。
2条答案
按热度按时间n8ghc7c11#
使用tidyverse包和
facet_grid
可以更干净地解决这个问题:请注意,您必须在
col_types
参数中将前三列指定为“character”,否则看起来不太好。用有意义的东西替换数字代码会更好(例如,将月份变成有序因子“January”,“February”而不是1,2;类型和ID类似)。j1dl9f462#
假设您想要将
id
绘制为x轴,并排显示月份,并堆叠不同类型,您可以按月份拆分 Dataframe ,并为每个月份添加一个条形图层,为第二个月的条形图移动x
一个量,以便将它们分隔开:给出: