如何用R创建聚类柱形图?

时间:2018-08-26 07:09:50

标签: r

我有3列数据表-month,p1,p2 我需要显示p1和p2的分组条形图。 x轴为月。 我想知道如何在图表上显示百分比。

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

您提供的图像称为群集条形图。 您需要重新定义数据集,因为它无法提供群集条形图。

创建数据集:

dt<-data.frame(
  month= c("Jan", "Feb", "Mar", "Apr","May","Jun","Jul","Agu","Sep","Oct","Nov","Dec",
           "Jan", "Feb", "Mar", "Apr","May","Jun","Jul","Agu","Sep","Oct","Nov","Dec"),
  category=c("P1","P1","P1","P1","P1","P1","P1","P1","P1","P1","P1","P1",
             "P2","P2","P2","P2","P2","P2","P2","P2","P2","P2","P2","P2"),
  value=sample(50000:100000,24))

head(dt)

然后绘制

 library(ggplot2)

  ggplot(data=dt, aes(x=month, y=value, fill=category)) +
  geom_bar(stat="identity", position=position_dodge())

enter image description here