qplot barplot重叠条,如何避免呢?

时间:2018-07-28 17:07:54

标签: r ggplot2 bar-chart

我正在使用以下代码创建barplot:

qplot(forest_visit, data = W522, geom = "bar",
   fill=sex,xlab= "Waldbesuche",ylab="Anzahl der Probanden")+ geom_bar(position="dodge")

这是结果:

enter image description here

我想要的是彼此相邻的红色和蓝色条,而不是红色条中的蓝色条。 我该如何实现?

1 个答案:

答案 0 :(得分:1)

请进行一些研究,并至少在尝试之前尝试Internet资源。由于您未共享数据,因此我也无法使用您的数据。

因此,我只使用来自链接网站的样本数据。我没有用谷歌搜索

  

堆叠的分组条形图ggplot

找到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-轴。在这种情况下,您可以使用fill参数设置一个组。