R - frequency histogram with normal curve

By | November 23, 2013

Creating a histogram in R is a piece of cake. But often times, you'll want to take a look at the gaussian distribution that your data may represent. It's easy enough to look at the frequency histogram separately from the gaussian, but what if we want to overlay the gaussian on top of the histogram? This is a little tricky, because the gaussian gives density, not frequency. With some scaling, we get the desired effect. I've also added a blue line to indicate the mean of the distribution.

histWithNormal = function(d,xlab="",main="",breaks=10,
                          ylim=c(0,length(d)/2),xlim=c(min(d),max(d)),
                          includeMeanLine=T,col="lightgray") {
    g = d
    h<-hist(g, ylim=ylim,xlim=xlim,breaks=breaks, density=10, col=col, xlab=xlab, main=main) 
    xfit<-seq(min(g),max(g),length=100) 
    yfit<-dnorm(xfit,mean=mean(g),sd=sd(g)) 
    yfit <- yfit*diff(h$mids[1:2])*length(g) 
    lines(xfit, yfit, col="black", lwd=2)
    if(includeMeanLine) {
        m = mean(d,na.rm=T)
        segments(m,dnorm(m,mean=m,sd=sd(g))*diff(h$mids[1:2])*length(g),m,0,lwd=2,col="blue")
    }
}

histWithNormal(rnorm(1000),ylim=c(0,250))

RplotN

Share
www.pdf24.org    Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *


four + 1 =