Desktop Survival Guide
by Graham Williams

Basic Histogram with Density Curve

R allows plots to be built up--this example shows a density histogram of a set of random numbers extracted from a normal distribution with the density curve of the same normal distribution also displayed. In the R code we build the histogram at first without plotting it, so as to determine the y limits ($range$ selects the minimum and maximum values, while $h\$density$ is the list of density values being plotted and $dnorm(0)$ is the maximum possible value of the density), since otherwise the curve might push up into the title!

Image rplot-hist-density

ds <- rnorm(200)
  h <- hist(ds, plot=F)
  ylim <- range(0, h$density, dnorm(0))
  hist(ds, xlab="normal", ylim=ylim, freq=F,
       main="Histogram of Normal Distribution with Density")
  curve(dnorm, col=2, add=T)

Copyright © 2004-2010 Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
The PDF version is a formatted comprehensive draft book (with over 800 pages).
Brought to you by Togaware. This page generated: Sunday, 22 August 2010