Basically, I'm just trying to plot a curve over my histogram for the weight of a few students.

 lines(density(weight), lwd = 2, col = "red")

However, what I get is a line on the bottom of the graph. I suppose the density has to be such that the integral of the curve is 1. What command should I be using instead of density, if I want to have a fitting curve for the histogram?

  • 1
    $\begingroup$ Although asked in terms of R code, this is actually a conceptual / statistical confusion, & not a coding question. IMO, it should be on topic here. $\endgroup$ Aug 27, 2015 at 17:07

1 Answer 1


The problem is that a density and the usual frequency (i.e. count) histogram aren't on the same scale (i.e. at heart this isn't an R problem, it's a problem that a count histogram isn't a legitimate density).

Typically, a density has area 1, but a histogram has area $n$. This kind of problem would occur any time you compared things with different area.

[Edit: I was thinking of a histogram like this, where the count is definitely represented by area, but A.Donda is quite right to point out in comments that R's hist doesn't do that*; it represents count by height and so the area is of the histogram will be $n\times$ the binwidth ($b$, say). *(and indeed more generally it's very common that people define the count in relation to the heights of the histogram rather than in terms of area. My desire to call that a bar chart doesn't change what the hist command does, for example). So consequently, in many cases the area will actually be $nb$, as it is here.]

To make them comparable, you will either need to scale your histogram to have area 1 (making the histogram into a density-estimate, the solution I would suggest), or you need to scale your density to have area $n$ (at which point it's no longer a density of course, but is at least something comparable to the frequency histogram).

(An easy way to achieve the first in R is just to use freq=FALSE in your call to hist)

There's an example of the resulting comparison (having the two displays both be valid densities) in this post:

hist and kde for gamma(10) r.v., n = 200

  • $\begingroup$ To be pedantic, a histogram has area $n \Delta$, where $\Delta$ is the bin width. $\endgroup$
    – A. Donda
    Aug 27, 2015 at 16:45
  • $\begingroup$ @A.Donda That's not correct, because the histogram's area depends on the heights. Otherwise it would be practically useless! $\endgroup$
    – whuber
    Aug 27, 2015 at 17:07
  • $\begingroup$ @whuber, a single histogram bar has a height $c_i$ and a width $\Delta$; its area is therefore $c_i \Delta$. The total area of all bars is therefore $\sum_i c_i \Delta = n \Delta$ if the sample size is $n = \sum_i c_i$. The heights are implicit in $n$. $\endgroup$
    – A. Donda
    Aug 27, 2015 at 17:10
  • 1
    $\begingroup$ @A.Donda You're right to point out that my answer was inadequate. I've made an edit; hopefully that's sufficient. Sorry to have deleted my comment; if I'd realized you were replying to it I'd have left it there. $\endgroup$
    – Glen_b
    Aug 27, 2015 at 17:23
  • 1
    $\begingroup$ Regarding helpfulness, right back at you: You could have simply pointed out that in your understanding (backed by standard textbooks, I get it) a histogram shows count densities on the vertical axis, and all this back and forth could have been avoided. $\endgroup$
    – A. Donda
    Aug 27, 2015 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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