这是基于这个帖子 https://stackoverflow.com/questions/26913954/make-barplot-in-ggplot2-with-summary-statistics?noredirect=1#comment42377447_26913954。假设我有这样的数据:
y = data.frame(Specie=c('A','V','R','P','O'),Number=c(18756,8608,3350,3312,1627))
z = data.frame(Specie=c('A','V','R','P','O'),Number=c(17000,1000,8000,5500,9000))
请注意,Specie
变量是相同的y
and z
.
我可以通过以下方式分别创建 y 和 z 的条形图:
library(ggplot2)
qplot(x=y[,1], y=y[,2], geom="bar", stat="identity")
qplot(x=z[,1], y=z[,2], geom="bar", stat="identity")
我如何将这两个图合并为一个?我们的想法是让y
and z
条形图彼此相邻,都在同一个关联中Specie
多变的。酒吧为x
例如,蓝色和条形y
例如,将是红色的。