# how to scale the density plot for my histogram

I have the histogram plot and I'd like to overlap it with density line for the same data. Importantly, I don't want to turn histogram into density values, but want to keep N (numbers) on y axis. Is there any way to overlap the histogram and density plot without transforming the histogram, but rather to scale up the density curve ?   Thanks a lot!

Eugene

• Generic answer is that you can always replace the labels on y-axis. – Tim Oct 7 '19 at 21:23
• To get the scaled density that converts to counts you just multiply your density curve by $n$. – Ben Oct 8 '19 at 0:40

The area under a true density function is 1. So unless the total area of the bars in the histogram is also 1, you cannot make a useful match between a true density function and the histogram.

Using actual density functions. A correct (and perhaps the easiest) course of action is to do what you explicitly say (without giving a reason) that you do not want to do: Put the histogram on a density scale and then superimpose either a density estimator based on data or the density function of the hypothetical distribution from which the data in the histogram where sampled. If you do this, the vertical scale of the histogram is automatically the correct scale for the densities.

Below is a histogram of data from a mixture of normal distributions, simulated in R, along with a kernel density estimator (KDE) of the data (red), and the distribution used to simulate the data (dotted). [With sample size as large as $$n=6000$$ you can expect a good match between the histogram and the KDE---even if not always as good as shown here.] The relevant R code is shown below.

set.seed(710)
mix = sample(c(-.6, 0, .6), 6000, rep=T, p=c(.1,.8,.1))
x = rnorm(6000, mix, .15)
lbl = "Histogram of Data with KDE (red) and Population Density"
hist(x, prob=T, br=50, col="skyblue2", main=lbl)
lines(density(x), col="red")
curve(.1*dnorm(x,-.6,.15)+.8*dnorm(x,0,.15)+.1*dnorm(x,.6,.15), hist(x, br=50, main="Frequency Histogram with Scaled Density Function")

• +1 You don't have to guess the scale factor. In R, first call hist with the argument plot=FALSE and compute the width from the breaks attribute of the resulting "histogram" object; you can use that to scale the density reliably. – whuber Oct 8 '19 at 13:16