
24 Ridgeline plots
theme_squeezit()
Graphs in this chapter use ggrewindr::theme_squeezit(), built from Squeezit drink bottles on gravel: a gravel background, a red frame and title, red, orange, green, pink, blue, and gold bottle marks, and a Marcellus title.
24.1 Description
If we want to plot density curves but retain the interpretability of the axes, consider comparing multiple distributions using the ggridges package.
24.2 Set up
PACKAGES
Install packages.
show/hide
install.packages("ggridges")
library(ggridges)
library(ggplot2)
library(dplyr) # for tibble() and data manipulationDATA
Squeezit was General Mills’ fruit drink in a squeezable plastic bottle shaped like a little character, and every flavor came in a different color. We’ll simulate how many seconds it took kids to squeeze out 60 bottles of each of six flavors. Each flavor gets its own average (avg_seconds), and rgamma() draws each bottle’s time from a gamma distribution with the same shape (2.5) for every flavor. Setting the seed with set.seed() means you’ll get the same 360 bottles every time you run the code below.
show/hide
set.seed(1998) # same 360 bottles every time the code runs
squeezit_drinks <- tibble(
bottle_id = 1:360, # 360 different bottles, one row each
flavor = rep(c("Tropical", "Blue raspberry", "Strawberry", "Green punch", "Orange", "Cherry"),
each = 60) # 60 bottles of each flavor
) |>
mutate(
flavor = factor(flavor, levels = c("Cherry", "Orange", "Green punch", "Strawberry",
"Blue raspberry", "Tropical")), # fastest to slowest
avg_seconds = c(Tropical = 40, `Blue raspberry` = 32, Strawberry = 26, `Green punch` = 22,
Orange = 18, Cherry = 12)[as.character(flavor)], # average seconds to finish a bottle
seconds_to_finish = pmax(1, # at least one second
round(rgamma(n(), shape = 2.5,
scale = avg_seconds / 2.5))) # skewed right around each average
) |>
select(bottle_id, flavor, seconds_to_finish)
glimpse(squeezit_drinks)
#> Rows: 360
#> Columns: 3
#> $ bottle_id <int> 1, 2, 3, 4, 5, 6, 7, 8…
#> $ flavor <fct> Tropical, Tropical, Tr…
#> $ seconds_to_finish <dbl> 55, 18, 48, 11, 23, 94…24.3 Distribution
Ridgeline plots stack one density curve per group, which makes them good for comparing many groups at once. All six flavors here share the same kind of distribution: most bottles are empty fairly quickly, and a few take a long time. What changes from flavor to flavor is the scale. Below we’ll move from the bottles, to a picture, to the formula.
COUNT IT
Start with the first five tropical bottles.
show/hide
head(squeezit_drinks, 5)These five bottles took anywhere from 11 to 55 seconds. The summary below shows that the mean is above the median for every flavor (a sign of right skew), and that the standard deviation grows with the mean. The ratio of the two (cv, the coefficient of variation) stays between about 0.54 and 0.72 for every flavor.
show/hide
squeezit_drinks |>
group_by(flavor) |>
summarise(mean = mean(seconds_to_finish),
median = median(seconds_to_finish),
sd = sd(seconds_to_finish), cv = sd / mean)DRAW IT
The ridgeline plot below adds a line at each flavor’s median (quantile_lines = TRUE with quantiles = 2). Every ridge has a hump on the left and a tail on the right. Moving up from cherry to tropical, the humps slide right and the tails generally get longer (strawberry’s reaches the farthest), but the overall shape stays the same. The extra bumps (like the one in the tropical ridge) are noise from having only 60 bottles per flavor. No bottle can take less than 0 seconds, so from = 0 starts each ridge at 0.
show/hide
ggplot(squeezit_drinks, aes(x = seconds_to_finish, y = flavor, fill = flavor)) +
ggridges::geom_density_ridges(alpha = 2/3, quantile_lines = TRUE,
quantiles = 2, color = "#F5F5F5", from = 0, show.legend = FALSE) +
labs(x = "Seconds to finish", y = NULL)
NAME IT
Each flavor’s times come from a gamma distribution with a shared shape (\(k\)) and its own scale (\(\theta\)):
\[ X_{\text{flavor}} \sim \text{Gamma}(k = 2.5, \; \theta_{\text{flavor}}), \quad \theta = \frac{\text{avg seconds}}{2.5} \]
The mean of a gamma distribution is \(k\theta\) and its standard deviation is \(\sqrt{k}\,\theta\), so their ratio is \(1 / \sqrt{k} = 1 / \sqrt{2.5} \approx 0.63\) no matter the flavor. That’s why the cv column stays near 0.63 and why the ridges all have the same shape: changing \(\theta\) stretches a ridge along the x axis without changing its shape. The Talkboy messages in Chapter 3 came from a gamma distribution too, with a larger shape (shape = 4). Try giving one flavor a larger shape in rgamma(), and its ridge becomes more symmetric.
24.4 Grammar
CODE
Create labels with
labs()Initialize the graph with
ggplot()and providedataMap
seconds_to_finishto thexaxisMap
flavorto theyaxis andfillAdd the
ggridges::geom_density_ridges()layer (withalphaset to2/3, andfrom = 0so no ridge starts below 0 seconds)Remove the legend with
show.legend = FALSE
show/hide
labs_ridges <- labs(
title = "Squeezit",
caption = "Simulated data",
subtitle = "Seconds to squeeze out a whole bottle",
x = "Seconds to finish",
y = "Flavor", fill = "Flavor")
ggp2_ridges <- ggplot(data = squeezit_drinks,
aes(x = seconds_to_finish,
y = flavor,
fill = flavor)) +
ggridges::geom_density_ridges(alpha = 2/3, from = 0,
show.legend = FALSE)
ggp2_ridges +
labs_ridgesGRAPH
Ridgeline plots are excellent for comparing continuous distributions across groups.
