Histogram + Density Plot Combo in R

[This article was first published on Mollie's Research Blog, 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.

Plotting a histogram using hist from the graphics package is pretty straightforward, but what if you want to view the density plot on top of the histogram? This combination of graphics can help us compare the distributions of groups.

Let’s use some of the data included with R in the package datasets. It will help to have two things to compare, so we’ll use the beaver data sets, beaver1 and beaver2: the body temperatures of two beavers, taken at 10 minute intervals.

First we want to plot the histogram of one beaver:

hist(beaver1$temp, # histogram
 col="peachpuff", # column color
 border="black",
 prob = TRUE, # show densities instead of frequencies
 xlab = "temp",
 main = "Beaver #1")

Next, we want to add in the density line, using lines:


hist(beaver1$temp, # histogram
 col="peachpuff", # column color
 border="black",
 prob = TRUE, # show densities instead of frequencies
 xlab = "temp",
 main = "Beaver #1")
lines(density(beaver1$temp), # density plot
 lwd = 2, # thickness of line
 col = "chocolate3")


Now let’s show the plots for both beavers on the same image. We’ll make a histogram and density plot for Beaver #2, wrap the graphs in a layout and png, and change the x-axis to be the same, using xlim.


Here’s the final code, also available on gist:
png("beaverhist.png")
layout(matrix(c(1:2), 2, 1,
 byrow = TRUE))
hist(beaver1$temp, # histogram
 col = "peachpuff", # column color
 border = "black",
 prob = TRUE, # show densities instead of frequencies
 xlim = c(36,38.5),
 xlab = "temp",
 main = "Beaver #1")
lines(density(beaver1$temp), # density plot
 lwd = 2, # thickness of line
 col = "chocolate3")
hist(beaver2$temp, # histogram
 col = "peachpuff", # column color
 border = "black",
 prob = TRUE, # show densities instead of frequencies
 xlim = c(36,38.5),
 xlab = "temp",
 main = "Beaver #2")
lines(density(beaver2$temp), # density plot
 lwd = 2, # thickness of line
 col = "chocolate3")
dev.off()

To leave a comment for the author, please follow the link and comment on their blog: Mollie's Research Blog.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)