One Continuous Variable

Continuous variables can take (in principle) infinitely many values, so we can’t just count occurrences of each exact value the way we did for categories. Instead, we summarise the shape of the distribution.

Histograms

Do: run the code below to see the distribution of cty (city miles per gallon).

See: R groups the x-axis into equally-spaced bins (each 3 units wide) and draws a bar whose height is the count of observations falling in that bin.

Predict: what do you expect to change if you set binwidth = 1 instead of binwidth = 3? Will the overall shape of the distribution look the same?

Explain: the general shape doesn’t change dramatically, but with narrower bins you can now see fine detail, notably, a couple of unusually high values (outliers) around 30+ mpg that were smoothed away by the wider bins. Choosing a binwidth is always a trade-off between hiding noise (too wide) and hiding the overall shape in noise (too narrow).

Density plots

A density plot is a smoothed alternative to a histogram.

Do: run this density plot of cty.

See: a single smooth, filled curve rising to one main peak and tapering off on either side, with no individual bars or bins visible.

Predict: compare this shape to a histogram of cty you made earlier. What does the density plot make easier to see, and what does it hide that a histogram would have shown you?

Explain: because the curve is smoothed, it makes the overall shape of the distribution easier to read at a glance. For example, you see quickly how many peaks there are, how skewed it is or where most of the data sits. Also, the shape does not depend on a chosen bin width. What it hides is the raw counts: a density plot’s y-axis shows density, not the number of observations, so you lose the sense of exactly how much data you have or how many points fall in any particular range.

You can overlay the raw data points on top of a density plot or histogram using geom_rug(), which draws a small tick mark for every individual observation along an axis. This gives you a sense of exactly where the raw data points sit underneath the smoothed curve and thus makes the density plot more informative.

Practice exercise

You want to show a colleague, at a glance, roughly how many cars get between 15 and 20 city miles per gallon. Would a histogram or a density plot make that number easier to read off directly? Why?

Run both plots below and compare them before answering.

Look at the y-axis of each plot. What does a value on the y-axis actually represent in the histogram, and what does it represent in the density plot?

A histogram is more direct here: the height of each bar corresponds to an actual count of observations, so you can read off (or closely approximate) a count for a given range. A density plot’s y-axis is a density (the area under the whole curve integrates to 1), which is useful for comparing the shape of distributions but doesn’t give you a literal observation count without extra calculation.

Back to top