Jacques Wagnor
2009-May-09 16:10 UTC
[R] Histogram frequencies with a normal pdf curve overlay
Dear List, When I plot a histogram with 'freq=FALSE' and overlay the histogram with a normal pdf curve, everything looks as expected, as follows: x <- rnorm(1000) hist(x, freq=FALSE) curve(dnorm(x), add=TRUE, col="blue") What do I need to do if I want to show the frequencies (freq=TRUE) with the same normal pdf overlay, so that the plot would still look the same? Regards, Jacques platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 8.0 year 2008 month 10 day 20 svn rev 46754 language R version.string R version 2.8.0 (2008-10-20)
(Ted Harding)
2009-May-09 16:31 UTC
[R] Histogram frequencies with a normal pdf curve overlay
On 09-May-09 16:10:42, Jacques Wagnor wrote:> Dear List, > When I plot a histogram with 'freq=FALSE' and overlay the > histogram with a normal pdf curve, everything looks as expected, > as follows: > > x <- rnorm(1000) > hist(x, freq=FALSE) > curve(dnorm(x), add=TRUE, col="blue") > > What do I need to do if I want to show the frequencies (freq=TRUE) > with the same normal pdf overlay, so that the plot would still look > the same? > > Regards, > JacquesThink first about how you would convert the histogram densities (heights of the bars on the "density" scale) into histogram frequencies. Density * (bin width) * N = frequency where N = total number in sample. Then all you need to is multiply the Normal density by the same factor. To find out the bin width, take the difference between succesive values of the "breaks" component of the histogram. One way to do all this is N <- 1000 x <- rnorm(N) H <- hist(x, freq=TRUE) ## This will plot the histogram as well dx <- min(diff(H$breaks)) curve(N*dx*dnorm(x), add=TRUE, col="blue") Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 09-May-09 Time: 17:31:03 ------------------------------ XFMail ------------------------------
Assuming a constant bin width, you need to multiply the density by n*binwidth, where the bin width is (obviously!) the width of the histogram bins.>>> Jacques Wagnor <jacques.wagnor at gmail.com> 05/09/09 5:10 PM >>>Dear List, When I plot a histogram with 'freq=FALSE' and overlay the histogram with a normal pdf curve, everything looks as expected, as follows: x <- rnorm(1000) hist(x, freq=FALSE) curve(dnorm(x), add=TRUE, col="blue") What do I need to do if I want to show the frequencies (freq=TRUE) with the same normal pdf overlay, so that the plot would still look the same? Regards, Jacques platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 8.0 year 2008 month 10 day 20 svn rev 46754 language R version.string R version 2.8.0 (2008-10-20) ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Apparently Analagous Threads
- Shift the normal curve to the top or near to the top of the histogram
- Overlay PDF on histogram
- How to plot Histogram with frequence overlaid by distribution curve
- Howto fit normal curve into histogram using GGPLOT2
- Shift the normal curve to the top or near to the top of the histogram