Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R histogram with multiple populations

I'm interested in creating a histogram in R that will contain two (or more) population on top of each other, meaning - I don't want a two histograms sharing the same graph but a bar containing two colors or more.

Found the image below - this is what I want to accomplish.

example

Any ideas?

like image 880
Adi Avatar asked Oct 28 '14 15:10

Adi


2 Answers

That is actually the annoying default in ggplot2:

library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, fill=Species)) +
  geom_histogram()

resulting plot

like image 64
Roland Avatar answered Oct 01 '22 12:10

Roland


Here is another option without using 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)
like image 40
GuestBruce Avatar answered Oct 01 '22 13:10

GuestBruce