O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Overlay a density plot on a histogram

Histograms are very sensitive to the number of bins used. Kernel density plots give a smoother and more accurate picture of the distribution. Usually, we overlay a density plot on a histogram using the density() function to visualize data distribution.

If invoked by itself, the density() function only produces the density plot. To overlay it on the histogram, we use the lines() function, which does not erase the current chart and instead overlays the existing plot. Since the density plot plots relative frequencies (approximating a probability density function), we need to ensure that the histogram also shows the relative frequencies. The prob=TRUE argument achieves this:

hist(mpg, prob=TRUE) lines(density(mpg)) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required