Comparing Distributions
Update
Updated on December 22nd, 2021. I made the generated data simpler so that the resulting distributions would be less confusing to look at.
When we want to see how something varies across categories, the trellis or small multiple plot is a good friend. We repeatedly draw the same graph once for each category, lining them up in a way that makes them comparable. Here’s an example from my book, using the gapminder
data, which provides a cross-national time series of GDP per capita for many countries.
|
|

A Gapminder small multiple.
Sometimes, we’re interested in comparing distributions across categories in something like this way. In particular, I’m interested in cases where we want to compare a distribution to some reference category, as when we look at subpopulations in comparison to an overall distribution.
Generate some population and subgroup data
Say we have some number of observed units, e.g., three thousand “counties” or whatever. Each county has some population made up of three groups. Across all counties, each group has a measure of interest. The measures are all distributed normally but vary in their means and standard deviations.
|
|
In the tibble we’ve just made up, unit
is our county, pop_a
, pop_b
, and pop_c
are our groups, measured on whatever we are measuring, and pop_total
is the mean value of pop_a
, pop_b
, and pop_c
for each unit. We make a vector of 3,000 populations using rnorm
. Each group is distributed normally but with a slightly different mean and standard deviation in each case. The unit names here are just to make things explicit, we don’t need them for the rowwise()
operation to work.
Single panels
Now we can plot the group-level population distributions across counties. Again, we
want to compare group distributions to one another and to the overall average
distribution by county. A single-panel histogram showing all four distributions isn’t very satisfactory. Even though we’re using alpha
to make the columns semi-transparent, it’s still very muddy.
|
|

Histogram of all groups.
If we use a geom_density()
rather than geom_histogram()
we’ll generate kernel density estimates for each group. These look a little better, but not great.
|
|

A single panel of kernel densities.
That’s better, but still not great. A very serviceable compromise that has many of the virtues of a small multiple but has the advantage of keeping things in one panel is a ridgeline plot, courtesy of geom_ridgeline()
from Claus Wilke’s ggridges
package:
|
|

A ridgeline plot.
Ridgeline plots look good and scale pretty well when there are larger numbers of categories to put on the vertical axis, especially if there’s a reasonable amount of structure in the data, such as a trend or sequence in the distributions. They can be slightly inefficient in terms of space with smaller numbers of categories. When the number of groups gets large they work best in a very tall and narrow aspect ratio that can be hard to integrate into a page.
Histograms with a reference distribution
Like with the Gapminder plot, we can facet our plot so that every subgroup gets
its own panel. But instead of having the Total population be its own panel, we
will put it inside each group’s panel as a reference point. This allows us to
compare the group to the overall population, and also makes eyeballing
differences between the group distributions a little easier. To do this, we’re
going to need to have some way to put the total population distribution into
every panel. The trick is to hold on to the total population by only pivoting
the subgroups to long format. That leaves us with repeated values for the total
population, pop_total
, like this:
|
|
When we draw the plot, we first call on geom_histogram()
to draw the
distribution of the total population, setting the color to gray. Then we
call it again, separately, to draw the subgroups. Finally we facet on
the subgroup names. This leaves us with a faceted plot where each panel
shows one subpopulation’s distribution and, for reference behind it, the
overall population distribution.
|
|

Histograms with a reference distribution in each panel.
This is a handy trick. We’ll use it repeatedly in the remaining figures, as we look at different ways of drawing the same comparison.
While putting the reference distribution behind the subgroup distribution is nice, the way the layering works produces an overlap that some viewers find difficult to read. It seems like a third distribution (the darker color created by the overlapping area) has appeared along with the two we’re interested in. We can avoid this by taking advantage of the underused geom_step()
and its direction
argument. We can tell geom_step()
to use a binning method (stat = "bin"
) that’s the same as geom_histogram()
. Here we’re also using the computed ..density..
value rather than ..count..
, but we could use ..count..
just fine as well.
|
|
With geom_step()
, we get a histogram with just its outline drawn. This works quite well, I think. Because we’re just drawing the outline, we call it after we’ve drawn our histograms, so that it sits in a layer on top of them.

Group histograms and overall distribution in outline.
We can also scale the counts within bins, if we like, using ..ncount..
, which is one of the statistics that stat_bin()
computes along with the default ..count..
.
|
|

Group histograms and overall distribution in outline, with scaled counts.
Frequency polygons
A final option, half way between histograms and smoothed kernel density estimates, is to use filled and open frequency polygons. Like geom_histogram()
, these use stat_bin()
behind the scenes but rather than columns they draw filled areas (geom_area
) or lines (geom_freqpoly
). The code is essentially the same as geom_histogram
otherwise. We switch back to counts on the y-axis here.
|
|