[R] How to superimpose a histogram and density plot

Venables, Bill (CMIS, Cleveland) Bill.Venables at cmis.CSIRO.AU
Mon Jun 7 22:22:36 CEST 1999


Peter,

1. Why would R users have the S-PLUS 4 Guide to Statistics handy?  :-)

2. The problem with hist() is that it does not do a real histogram.  The
vertical scale is, by default, a *frequency* axis, not a *relative
frequency density* axis which would put it on the same scale as a density
estimate.  Brian Ripley and I got so annoyed about this we wrote our own
function for the MASS library called "truehist" - to make a true histogram!
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.  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)

Best of luck,
Bill Venables.


-----Original Message-----
From: Peter B. Mandeville
To: r-help at stat.math.ethz.ch
Sent: 6/7/99 9:24 PM
Subject: [R] How to superimpose a histogram and density plot

NCSS has a nice plot of the histogram and density plot superimposed. The
S-PLUS 4 Guide to Statistics on page 45 suggests the following code for
a general purpose density plot for a vector named data

iqd <- summary(data)[5]-summary(data)[2]
plot(density(data,width=2*iqd),xlab="x",ylab="",type="l")

Is it possible to superimpose the density plot on a histogram produced
with hist()? I tried lines(density(data,width=2*iqd),type="l") but the
results weren't satisfactory.

Thank you,

Peter B.

--
Peter B. Mandeville
mandevip at deimos.tc.uaslp.mx
Jefe del Depto. de Informática y Bioestadística
rpe1531 at pasteur.fmed.uaslp.mx 
Facultad de Medicine                            Tel: 48 26-23-45 ext.
232
Universidad Autónoma de San Luis Potosí         Fax: 48 28-23-52
Av. V. Carranza 2405
Col. Los Filtros
Apartado Postal 145
San Luis Potosí, S.L.P.
78210 México

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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