我用下面的代码创建了一个条形图:
qplot(forest_visit, data = W522, geom = "bar",
fill=sex,xlab= "Waldbesuche",ylab="Anzahl der Probanden")+ geom_bar(position="dodge")
这就是结果:
我想要的是相邻的红色和蓝色条,而不是红色条里面的蓝色条。我该如何实现这一点?
发布于 2018-07-29 01:21:56
在你问之前,请先做一些调查,至少尝试一下互联网资源。我也不能使用你的数据,因为你没有分享它。
因此,我只使用链接网站上的样本数据。这不是我创造的我只是在谷歌上搜索
堆叠分组条形图
并找到了this:
# library
library(ggplot2)
# create a sample dataset
specie=c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition=rep(c("normal" , "stress" , "Nitrogen") , 4)
value=abs(rnorm(12 , 0 , 15))
data=data.frame(specie,condition,value)
# Grouped
ggplot(data, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="dodge", stat="identity")
一般来说,在绘图时不使用ggplot中的qplot
函数。您可以使用ggplot
函数。在该函数中指定所使用的数据,在aes
中定义x
和/或y
-Axis。在本例中,使用fill
-argument设置了一个组。
https://stackoverflow.com/questions/51573523
复制相似问题