具有多个群体的R直方图

Adi*_*Adi 11 graphics r histogram

我有兴趣在R中创建一个直方图,它将包含两个(或更多)群体,这意味着 - 我不希望两个直方图共享相同的图形,而是一个包含两种或更多颜色的条形图.

找到下面的图片 - 这就是我想要完成的.

例

有任何想法吗?

Rol*_*and 14

这实际上是ggplot2中令人讨厌的默认值:

library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, fill=Species)) +
  geom_histogram()
Run Code Online (Sandbox Code Playgroud)

结果情节


小智 6

这是不使用ggplot的另一个选项:

#plot the entire data set (everything)
hist(everything, breaks=c(1:10), col="Red")

#then everything except one sub group (1 in this case)
hist(everything[everything!=1], breaks=c(1:10), col="Blue", add=TRUE)

#then everything except two sub groups (1&2 in this case)
hist(everything[everything!=1 && everything!=2], breaks=c(1:10), col="Green", add=TRUE)
Run Code Online (Sandbox Code Playgroud)