如何使用 ggplot2 手动堆叠条形图和已计算的拆分?
How can I stack bars manually with already counted splits using ggplot2?
我有以下数据框,我正在尝试使用 ggplot2 将堆叠条形图拆分为 "Early" 和 "Late"。 "Count" 列是总计数(早+晚)。
Stage Count Early Late
------------------------------------
PreMBT 2208 1539 669
Dome 2050 1507 543
Shield 1939 1442 479
Bud 1865 1377 488
我想要舞台在 x 轴上,计数在 y 轴上。但是,我想通过让 Early 成为一种颜色而 Late 成为另一种颜色来打破条形,图例也有这两种颜色。我已经尝试了很多东西,但它不起作用......这就是为什么我想知道是否有手动方法来分离它?任何帮助表示赞赏!这是我目前所拥有的(尚未被 Early/Late 分解):
densityplot <-ggplot(data, aes(x=Stage,fill=Stage,weight=Count))+geom_bar()
但是当我尝试 fill= Early 时,它不起作用。
我排除了Count
:
mlt = melt(df, id = "Stage")
mlt
ggplot(mlt, aes(x=Stage, y = value, fill = variable))+
geom_bar(position = "stack", stat = "identity")
我有以下数据框,我正在尝试使用 ggplot2 将堆叠条形图拆分为 "Early" 和 "Late"。 "Count" 列是总计数(早+晚)。
Stage Count Early Late
------------------------------------
PreMBT 2208 1539 669
Dome 2050 1507 543
Shield 1939 1442 479
Bud 1865 1377 488
我想要舞台在 x 轴上,计数在 y 轴上。但是,我想通过让 Early 成为一种颜色而 Late 成为另一种颜色来打破条形,图例也有这两种颜色。我已经尝试了很多东西,但它不起作用......这就是为什么我想知道是否有手动方法来分离它?任何帮助表示赞赏!这是我目前所拥有的(尚未被 Early/Late 分解):
densityplot <-ggplot(data, aes(x=Stage,fill=Stage,weight=Count))+geom_bar()
但是当我尝试 fill= Early 时,它不起作用。
我排除了Count
:
mlt = melt(df, id = "Stage")
mlt
ggplot(mlt, aes(x=Stage, y = value, fill = variable))+
geom_bar(position = "stack", stat = "identity")