[R] Histogram frequencies with a normal pdf curve overlay
Greg Snow
Greg.Snow at imail.org
Mon May 11 17:54:28 CEST 2009
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,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Jacques Wagnor
> Sent: Saturday, May 09, 2009 11:04 AM
> To: ted.harding at manchester.ac.uk
> Cc: r-help at r-project.org
> Subject: Re: [R] Histogram frequencies with a normal pdf curve overlay
>
> 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 ------------------------------
> >
>
> ______________________________________________
> 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.
More information about the R-help
mailing list