Overlaying a density line over a histogram

In this recipe, we will learn how to superimpose a kernel density line on top of a histogram.

Getting ready

We will continue using the airpollution.csv example dataset. You can simply type in the recipe code at the R prompt. If you wish to use the code later, you should save it as a script file. First, let's load the data file.

air<-read.csv("airpollution.csv")

How to do it...

Let's overlay a line that shows the kernel density of respirable particle concentrations on top of a probability distribution histogram:

par(yaxs="i",las=1) hist(air$Respirable.Particles, prob=TRUE,col="black",border="white", xlab="Respirable Particle Concentrations", main="Distribution of Respirable Particle Concentrations") box(bty="l") ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.