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.
Any ideas?
That is actually the annoying default in ggplot2:
library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, fill=Species)) +
geom_histogram()
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With