Skip to content

Histogram frequencies with a normal pdf curve overlay

4 messages · Jacques Wagnor, (Ted Harding), Greg Snow

#
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)
#
On 09-May-09 16:10:42, Jacques Wagnor wrote:
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 ------------------------------
#
Thank you!

On Sat, May 9, 2009 at 11:31 AM, Ted Harding
<Ted.Harding at manchester.ac.uk> wrote:
1 day later
#
You may also want to look at the rootogram function in the vcd package.

You could also use the updateusr function in the TeachingDemos package to reset the y-scale before plotting the curve (works well if you just want the shape of the curve, probably more work than the other suggestions if you want the areas to match).

Hope this helps,