Overlapping Histogram in R
[This article was first published on Data Analysis and Visualization in R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
While preparing a class exercise involving the use of overlaying of histogram, I searched Google on possible article or discussion on the said topic. Luckily, I found a blog where the author demonstrated an R function to create an overlapping histogram. However, a comment from a guy also showed the same output using transparency. Below were the sample codes that can be used to generate overlapping histogram in R as based on the blog and the viewers comment.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Here are the codes:
#Random numbers
h2<-rnorm(1000,4)
h1<-rnorm(1000,6)
# Histogram Grey Color
hist(h1, col=rgb(0.1,0.1,0.1,0.5),xlim=c(0,10), ylim=c(0,200), main=”Overlapping Histogram”)
hist(h2, col=rgb(0.8,0.8,0.8,0.5), add=T)
box()
# Histogram Colored (blue and red)
hist(h1, col=rgb(1,0,0,0.5),xlim=c(0,10), ylim=c(0,200), main=”Overlapping Histogram”, xlab=”Variable”)
hist(h2, col=rgb(0,0,1,0.5), add=T)
box()
To leave a comment for the author, please follow the link and comment on their blog: Data Analysis and Visualization in R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.