A Guide to ggrigdes Plots in R

Recently, in my job as a Data Analyst, my project manager introduced me to ggridges plots in R. I think they look cool and are also pretty useful. Therefore, here is a quick guide on how to create ggridges plots in R.

The name ggridges comes from the band Joy Division. The package is inspired by the iconic cover art of their album Unknown Pleasures, which features a series of stacked ridgelines representing radio waves. This visual style has been adapted in the ggridges package for R to create "ridge plots" or "joy plots," which display overlapping density plots.

Why Use ggridges Plots?

Here are three key reasons to use ggridges plots:

  1. Visualizing Multiple Distributions
    Ridge plots are ideal for comparing distributions of a variable across different categories or groups. Their overlapping density curves make patterns and differences between groups easy to identify at a glance.

  2. Space-Efficient Design
    Unlike separate histograms or box plots for each category, ggridges plots save space by stacking distributions vertically, making them perfect for datasets with many groups.

  3. Aesthetic Appeal
    Inspired by Joy Division's Unknown Pleasures album cover, ggridges plots are visually striking and modern. They can make your visualizations stand out in reports and presentations while still being highly informative.

After a brief introduction, and 3 key reasons to use ggridges plots, let’s move on to the R code and learn, step by step, how to plot data using these types of plots.

Step 1. Install and Load the Required Packages

First, ensure you have the necessary packages installed. You’ll need ggridges and ggplot2.

#If not already installed
install.packages("ggridges")
install.packages("ggplot2")

#Load the packages
library(ggridges)
library(ggplot2)

Step 2. Prepare Example Data

We'll use the iris dataset for demonstration, which is built into R.

data(iris)

#View the structure of the dataset
str(data)

Step 3. Create a Basic ggridges Plot

We’ll plot the distribution of the Sepal.Length variable for each Species.

ggplot(data, aes(x = Sepal.Length, y = Species, fill = Species)) +
  geom_density_ridges() +
  theme_minimal() +
  labs(
    title = "Distribution of Sepal Length by Species",
    x = "Sepal Length",
    y = "Species"
  )

Step 4. Customize the Plot

You can further customize the appearance of your plot with additional options:

  • Adjust Transparency: Use the alpha argument to control transparency.

  • Change Fill Colors: Modify the fill aesthetic to use a custom palette.

  • Add Scaling: Adjust the scale parameter in geom_density_ridges() to control the overlap of the ridgelines.

Here’s an enhanced example:

ggplot(data, aes(x = Sepal.Length, y = Species, fill = Species)) +
  geom_density_ridges(alpha = 0.7, scale = 1.5) +
  scale_fill_manual(values = c("skyblue", "orange", "lightgreen")) +
  theme_minimal() +
  theme(legend.position = "none") +
  labs(
    title = "Enhanced Ridge Plot of Sepal Length by Species",
    x = "Sepal Length",
    y = "Species"
  )

Conclusion

ggridges plots are a powerful and visually appealing way to compare distributions across multiple groups. They save space, enhance readability, and add a modern aesthetic to your data visualizations. Whether you're exploring data or creating polished presentations, ggridges plots can elevate your analytical storytelling in R.

Do you find these types of plots interesting? If so, I’ve included two links where you can explore more about ggridges plots and find additional information.


Introduction to ggridges by cran.r-project - https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html

ggridges: Ridgeline plots in ggplot2 by wilkelab.org - https://wilkelab.org/ggridges/

Next
Next

Make Your (New Year's) Resolutions Count: My Personal Guide to Success