[R] Histogram frequencies with a normal pdf curve overlay

Jacques Wagnor jacques.wagnor at gmail.com
Sat May 9 19:04:20 CEST 2009


Thank you!

On Sat, May 9, 2009 at 11:31 AM, Ted Harding
<Ted.Harding at manchester.ac.uk> wrote:
> 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