Ridgeline plots
Description
If we want to plot density curves but retain the interpretability of the axes, consider comparing multiple distributions using the ggridges
package.
Getting set up
PACKAGES:
Install packages.
Code
install.packages("palmerpenguins")
library(palmerpenguins)
library(ggplot2)
DATA:
Remove missing island
from penguins
Code
<- filter(penguins, !is.na(island))
peng_ridges glimpse(peng_ridges)
Rows: 344
Columns: 8
$ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex <fct> male, female, female, NA, female, male, female, male…
$ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…
The grammar
CODE:
Create labels with labs()
Initialize the graph with ggplot()
and provide data
Map bill_length_mm
to the x
axis
Map island
to the y
axis and fill
Add the ggridges::geom_density_ridges()
layer (with alpha
set to 2/3
)
Remove the legend with show.legend = FALSE
Code
<- labs(
labs_ridges title = "Adult foraging penguins",
x = "Bill length (millimeters)",
y = "Island", fill = "Island")
<- ggplot(data = peng_ridges,
ggp2_ridges aes(x = bill_length_mm,
y = island,
fill = island)) +
::geom_density_ridges(alpha = 2/3,
ggridgesshow.legend = FALSE)
+
ggp2_ridges labs_ridges
GRAPH:
Ridgeline plots are excellent for comparing continuous distributions across groups.