我有3列数据表-month,p1,p2 我需要显示p1和p2的分组条形图。 x轴为月。 我想知道如何在图表上显示百分比。
答案 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())