条形图中的并排条形

2024-05-01

这是基于这个帖子 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例如,将是红色的。


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))

library("ggplot2")
library("reshape2")

df=merge(y,z,by=c("Specie"))
names(df)=c("Specie","y","z")

df=melt(df)

ggplot(df,aes(x=Specie,y=value,fill=variable))+geom_bar( stat="identity",position=position_dodge())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

条形图中的并排条形 的相关文章

随机推荐