[R] histogramm$density

Peter Dalgaard
Fri, 26 Dec 2008 12:25:42 -0000

austernkommunikation at googlemail.com wrote:
> hello,
>
>
> i am using the hist function with classified values. The class breaks are
>1, so
> histogram$density is != 1.
>
> How to plot the histogram with freq=FALSE and the real class density
values.
> I used:
>
>> h2 = hist(value, breaks = breaks_vector)
>> h2$density = round(h2$counts/sum(h2$counts), 2)
>> h2$intensities = h2$density
>> plot(h2, freq=F)
>
> but this isn't the best way, i think.
>
> Also i would like to add the density function line with
>
>>  lines(density(value)
>
> but of course it doesn't fit, due to the modified h2$density values.
>
> Where is my fault?

You are confusing the concepts of density and relative frequency. The
former is what is on the same scale as density(.) -- if you multiply the
x values by 10, the density is divided by 10 so that the _integral_
under the histogram is kept at 1, and if you join two intervals, the
density will become an average of the original two.

With relative frequencies, the _sum_ of the column heights is 1, and
joining columns will _add_ the heights. For essentially the same reason,
with non-equidistant breaks, this is not comparable to density(.).


--
    O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907