ggplot2 overlay histogram code example
Example 1: ggplot2 histogram
library(ggplot2)
ggplot(df, aes(x=weight)) + geom_histogram()
ggplot(df, aes(x=weight)) +
geom_histogram(binwidth=1)
p<-ggplot(df, aes(x=weight)) +
geom_histogram(color="black", fill="white")
p
Example 2: ggplot2 overlapping histograms
dat <- data.frame(xx = c(runif(100,20,50),runif(100,40,80),runif(100,0,30)),yy = rep(letters[1:3],each = 100))
ggplot(dat,aes(x=xx)) +
geom_histogram(data=subset(dat,yy == 'a'),fill = "red", alpha = 0.2) +
geom_histogram(data=subset(dat,yy == 'b'),fill = "blue", alpha = 0.2) +
geom_histogram(data=subset(dat,yy == 'c'),fill = "green", alpha = 0.2)