[R] hist() and density

Bill Simpson wsi at gcal.ac.uk
Mon Nov 20 11:12:42 CET 2000


On 17 Nov 2000, Peter Dalgaard BSA wrote:

> Bill Simpson <wsi at gcal.ac.uk> writes:

> > It doesn't seem easy to make density on the y-axis with the current
> > hist().  The freq argument only lets you choose counts (TRUE) or relative
> > frequency (FALSE). I would like to suggest that freq (or some renamed
> > version of the argument) take on the values
> > counts
> > freqs
> > densities
> > So that you can easily do a density histogram.
> 
> Look closer:  freq=FALSE  *does* give densitities. Try for instance
> 
> x<-rnorm(500,sd=100)
> hist(x,freq=F)
> curve(dnorm(x,sd=100),add=T)
> 
> freq=T gives absolute frequencies i.e. counts
OK then, the help page and the plot are wrong.
The help page says:

freq	logical; if TRUE, the histogram graphic is to present a
representation of frequencies, i.e, the counts component of the result; if
FALSE, relative frequencies (``probabilities''), the rel.freqs, are
plotted. Defaults to TRUE iff breaks are equidistant.

It says that freq=FALSE produces a plot with relative frequency on the
y-axis. In general rel freq != density. The two will be equal only if the
bin width is 1. So my suggestion still stands. Right now it seems you can
have counts or densities on the y-axis (mis-labelled on the plot and
mis-labelled in the help as "relative frequency"). Maybe some people want
relative frequency, so allow them to do it too.

rel freq = count/n
density = (count/n)/bin width

So at least the help page should be re-written and the axis label for the
hist when freq=F should be changed to "Density".

New help:

freq    logical; if TRUE, the histogram graphic is to present a
representation of frequencies, i.e, the counts component of the result; if
FALSE, densities, the rel.freqs/bin width, are
plotted. Defaults to TRUE iff breaks are equidistant.

There must be lots of people who have been confused by histogram that was
supposedly rel freq but was really density.

Bill

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