[R] Histogram frequencies with a normal pdf curve overlay
(Ted Harding)
Ted.Harding at manchester.ac.uk
Sat May 9 18:31:06 CEST 2009
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,
> Jacques
Think 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 ------------------------------
More information about the R-help
mailing list