I was using the package HistogramTools in R to create Histogram.

> H<-hist(1:100)
> sum(H$density)
[1] 0.1

Somehow the sum of the density is not 1, regardless of the data I use (I have tried several generated random data as well, and it is also not 1)

but then,

> sum(H$counts)
[1] 100

the total counts match the total number of data.

Am I wrong in understanding "density" or "relative frequency"??

  • $\begingroup$ You would find it informative to plot H. $\endgroup$ – whuber Sep 23 '19 at 19:54
  • $\begingroup$ Pretty sure this is essentially a duplicate. Its sort of covered in Silverfish's answer here but I think there's an answer (maybe from whuber) elsewhere that's more direct. I just don't seem to be turning it up right now. $\endgroup$ – Glen_b Sep 24 '19 at 3:21

$density property of an histogram object gives you the y-axis values of each bin. In order to get an area of $1$, you'll have to multiply it with the bin width. In your case, number of bins is 10 by default, which gives you a bin width of 10. Multiplying it with H$density gives $1$. Try the following, which will give 0.2 when executed (here bin width will be 5):

H<-hist(1:100, 20)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.