In this recipe, we will learn how to superimpose a kernel density line on top of a histogram.
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")
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") lines(density(air$Respirable.Particles,na.rm=T),col="red",lwd=4) grid(nx=NA,ny=NULL,lty=1,lwd=1,col="gray")