如何使用ggplot2手动堆叠已经计数的拆分栏?

时间:2017-06-22 05:07:14

标签: r ggplot2 bar-chart geom-bar

我有以下数据框,我正在尝试使用ggplot2将堆积的条形图拆分为"早期"和"晚"。专栏" Count"是总数(早期+晚期)。

    Stage   Count   Early   Late
------------------------------------
    PreMBT  2208    1539    669
    Dome    2050    1507    543 
    Shield  1939    1442    479
    Bud     1865    1377    488

我希望舞台在x轴上,并在凹轴上计数。但是,我想通过将Early作为一种颜色而将Late作为另一种颜色来打破这些条纹,传说中也有这两种颜色。我尝试了很多东西,但它没有用......这就是为什么我想知道是否有一种手动方式将它分开?任何帮助赞赏!这就是我到目前为止(早期/晚期尚未解决):

densityplot <-ggplot(data, aes(x=Stage,fill=Stage,weight=Count))+geom_bar()

但是当我尝试填充=早期时,它不起作用。

1 个答案:

答案 0 :(得分:1)

我排除了Count

mlt = melt(df, id = "Stage")
mlt

ggplot(mlt, aes(x=Stage, y = value, fill = variable))+
  geom_bar(position = "stack", stat = "identity")

enter image description here