[R] How to superimpose a histogram and density plot

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Jun 7 23:08:34 CEST 1999

"Venables, Bill (CMIS, Cleveland)" <Bill.Venables at cmis.CSIRO.AU> writes:

> The fact that every elementary book on statistics does it this way does not
> make it correct.  To be helpful, a histogram really has to be a
> non-parametric
> density estimator, period.
> Enough already of polemics.  

Not quite! There is a reason for doing it the other way, namely that
the concept of a histogram generally comes before the concept of a
probability density, pedagogically. It is very easy to explain that
you chop up the axis into bins and count the number of data points
that fall in each of them. I bet that half of the MDs that I teach
never quite understand the density (hell, the author of the textbook I
use managed to plot three identical gaussian curves with identical y
axis but different x axes... and he's a statistician). So for the
basic uses of the histogram, one would be replacing a perfectly
intuitive simple unit with a substantially more complex one.

>If you want a density estimate and a histogram 
> on the same scale, I suggest you try something like this:
> > IQR <- diff(summary(data)[c(5,2)])
> > dest <- density(data, width = 2*IQR)  # or some smaller width, maybe,
> > hist(data, xlim = range(dest$x), xlab = "x", ylab = "density",
> +      probability = TRUE)    # <<<--- this is the vital argument
> > lines(dest, lty=2)

Yep. frequency=FALSE has the same effect and might be more logical,
since the y-axis is not really probability but "probability per x

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list