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,
                          includeMeanLine=T,col="lightgray") {
    g = d
    h<-hist(g, ylim=ylim,xlim=xlim,breaks=breaks, density=10, col=col, xlab=xlab, main=main) 
    yfit <- yfit*diff(h$mids[1:2])*length(g) 
    lines(xfit, yfit, col="black", lwd=2)
    if(includeMeanLine) {
        m = mean(d,na.rm=T)



www.pdf24.org    Send article as PDF   

Leave a Reply

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

eight + 2 =