1 Goal


The goal of this tutorial is to learn how to create Density Ridgeline plots combining the ggplot and ggridges libraries. These plots partially overlap line plots to create the visual impression of a mountain range, so they can be quite useful for visualizing changes in distributions over time or space.


2 Data import


# First we load the libraries
library(ggplot2)
library(ggridges)

# In this tutorial we are going to use the iris dataset
data("iris")
iris <- as.data.frame(iris)
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

3 Plotting your first Density Ridgeline plot


# To create a Density Ridgeline plot, just use the geom_density_ridges() function
ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges()
## Picking joint bandwidth of 0.181


3.1 Overlapping ridges


# We can control the way the different densities overlap with the scale parameter.
# A setting of scale=1 means the tallest density curve just touches the baseline of the next higher one
ggplot(iris, aes(x = Sepal.Length, y = Species)) + geom_density_ridges(scale=1)
## Picking joint bandwidth of 0.181