+ Reply to Thread
Results 1 to 3 of 3

Thread: [R] Plotting a normal curve along with a histogram

  1. #1
    Points: 8,000, Level: 60
    Level completed: 25%, Points required for next Level: 150

    Location
    Massachusetts
    Posts
    111
    Thanks
    0
    Thanked 6 Times in 6 Posts

    [R] Plotting a normal curve along with a histogram




    The R code below is my attempt to plot a histogram and then plot a normal curve for reference. A blue line is drawn to show the mean value. From the example image I've attached you can see that the blue line does not run through the center of the normal curve, as it should; the normal curve appears to be scaled improperly along the x-axes.

    I've found many examples on the Internet of how to add normal curves to histograms in R, but none of those examples seem reliable when tested. Can anyone here please give correct R code for this? Thank you.

    Code: 
    # Draw a histogram with a normal curve.
    histnorm=function(x, ...)
    {
       if (length(x)>0)
       {
          hist(x,freq=FALSE, ...)
          rug(x)
          curve(dnorm(x, mean=mean(x), sd=sd(x)), add=TRUE, col="red", lty="dotted", xaxt="n")
          abline(v=mean(x),col="blue")
          mtext(paste("mean ", round(mean(x),1), "; sd ", round(sd(x),1), "; N ", length(x),sep=""), side=1, cex=.75)
       } # fi
    } # histnorm
    Attached Images  

  2. #2
    Beep
    Points: 60,927, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,037
    Thanks
    260
    Thanked 2,134 Times in 1,815 Posts

    Re: [R] Plotting a normal curve along with a histogram

    Quote Originally Posted by SlideRule View Post
    Code: 
    # Draw a histogram with a normal curve.
    histnorm=function(x, ...)
    {
       if (length(x)>0)
       {
          hist(x,freq=FALSE, ...)
          rug(x)
          curve(dnorm(x, mean=mean(x), sd=sd(x)), add=TRUE, col="red", lty="dotted", xaxt="n")
          abline(v=mean(x),col="blue")
          mtext(paste("mean ", round(mean(x),1), "; sd ", round(sd(x),1), "; N ", length(x),sep=""), side=1, cex=.75)
       } # fi
    } # histnorm
    Your problem comes from the fact that you're simultaneously using "x" to represent the data and as a variable inside of curve.

    A couple possible fixes: Change the input from "x" to "y" or "data" or something like that and make the corresponding changes in the function
    Code: 
    # Draw a histogram with a normal curve.
    histnorm=function(dat, ...)
    {
       if (length(dat)>0)
       {
          hist(dat ,freq=FALSE, ...)
          rug(dat)
          curve(dnorm(x, mean = mean(dat), sd = mean(dat)), add=TRUE, col="red", lty="dotted", xaxt="n")
          abline(v=mean(dat),col="blue")
          mtext(paste("mean ", round(mean(dat),1), "; sd ", round(sd(dat),1), "; N ", length(dat),sep=""), side=1, cex=.75)
       } # fi
    } # histnorm
    Or you could keep everything the same but you need to remember that the inside of curve considers x to be the variable of interest (not data!) so we can calculate the mean and standard deviation before plugging into curve:

    Code: 
    # Draw a histogram with a normal curve.
    histnorm=function(x, ...)
    {
       if (length(x)>0)
       {
          hist(x,freq=FALSE, ...)
          rug(x)
          mn <- mean(x)
          stdev <- sd(x)
          curve(dnorm(x, mean = mn, sd= stdev), add=TRUE, col="red", lty="dotted", xaxt="n")
          abline(v=mean(x),col="blue")
          mtext(paste("mean ", round(mean(x),1), "; sd ", round(sd(x),1), "; N ", length(x),sep=""), side=1, cex=.75)
       } # fi
    } # histnorm

  3. #3
    Points: 8,000, Level: 60
    Level completed: 25%, Points required for next Level: 150

    Location
    Massachusetts
    Posts
    111
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: [R] Plotting a normal curve along with a histogram


    Thank you, your second example does seem to work. Can you please explain more about how the values of x are being reassigned in my code? My code doesn't have any explicit reassignment of x, and I don't understand anything in the R 2.12.2 documentation to say that the value of the input arguments to the curve or dnorm functions would be side-affected.

+ Reply to Thread

           




Similar Threads

  1. Replies: 3
    Last Post: 08-18-2010, 04:20 PM
  2. Replies: 3
    Last Post: 03-24-2010, 06:18 AM
  3. probability and the normal curve
    By PedroNotTheLion in forum Probability
    Replies: 4
    Last Post: 06-24-2009, 07:55 PM
  4. Plotting a standardized histogram in MINITAB
    By Zanello in forum Other Software
    Replies: 2
    Last Post: 06-21-2008, 05:53 PM
  5. Normal Curve
    By cannoli in forum Probability
    Replies: 1
    Last Post: 01-19-2007, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats