Continuous by Categorical

Often you want to compare the distribution of a continuous variable across the levels of a categorical variable, for example, does highway mileage differ by drive train?

Boxplots

Do: run the code below.

See: one box-and-whisker summary is drawn for each level of drv.

Predict: a boxplot has several distinct visual features: a box, a thick line inside the box, whiskers, and sometimes isolated points. Before reading on, try to recall (or guess) what each feature represents.

Explain: the features of a boxplot are:

  • The lower edge of the box: the 25th percentile.
  • The thick middle line: the median (50th percentile).
  • The upper edge of the box: the 75th percentile.
  • The whiskers: typically extending to the most extreme point within 1.5 × the interquartile range (IQR).
  • Isolated points beyond the whiskers: potential outliers.

Flipping coordinates

coord_flip() swaps the x- and y-axes, turning a vertical plot into a horizontal one. This is especially useful when category labels are long and don’t fit neatly under vertical bars or boxes.

An alternative: violin plots

A boxplot only shows a handful of summary statistics. A violin plot shows the full estimated shape of the distribution (like a sideways density plot) for each category.

Do: run this violin plot of hwy by drv.

Predict: based on what you now know about boxplots and density plots, what information does a violin plot show that a boxplot does not?

Explain: a violin plot reveals the full shape of the distribution, for example, whether it is unimodal or bimodal (has one or two “bumps”), which the five numbers summarised by a boxplot cannot show. The trade-off is that a violin plot doesn’t mark the median or quartiles as explicitly as a boxplot does, unless you add extra layers.

Adding a second categorical variable to a boxplot

Do: run this boxplot of cty by drv alone first.

See: three boxes appear, one per drive type, each summarising the spread of cty within that group.

Predict: if you add colour = fl (fuel type) inside the aesthetic mapping, how many boxes do you expect to see in total? Think about what colouring by a second categorical variable did for scatterplots, and what that might mean here.

Explain: adding colour = fl further splits each drive-train category into sub-groups by fuel type, so you get one box per combination of drv and fl that actually occurs in the dataset (not every combination necessarily occurs). This is the same idea as colouring points by a third variable: mapping a second categorical variable doesn’t just recolour the existing boxes, it creates a separate box for every group defined by the combination of both variables.

Practice exercise

Using mpg, build a violin plot of hwy (y-axis) by drv (x-axis), then add fill = fl so that fuel type is represented as well. Before running the code, predict how many violins you’ll see in total.

Each violin represents one combination of drv and fl that actually occurs in the data. Not every combination is necessarily present in the data. To count them, cross-tabulate the two variables first, e.g. table(mpg$drv, mpg$fl). Also think about what a violin actually needs to be drawn: it’s a smoothed density curve, so consider whether every combination has enough observations for that to be possible.

ggplot(mpg) +
  geom_violin(aes(x = drv, y = hwy, fill = fl))

There are 3 levels of drv (4, f, r) and 5 levels of fl (c, d, e, p, r), giving 15 possible combinations. Only 12 of them actually occur in the data (checked with table(mpg$drv, mpg$fl)), but geom_violin() needs at least two observations per group to estimate a density curve. Three of those 12 combinations (front-wheel-drive with c, front-wheel-drive with e, and rear-wheel-drive with e) have only a single car each, so those groups get dropped. That leaves 9 violins actually drawn, not 12 or 15.

You’ve now covered the main geometries for one variable, two continuous variables, and continuous-by-categorical combinations. Time for the knowledge quiz.

Back to top