top of page

How to Create Ridgeline Plot in ggplot2 with ggridges

hands typing on a laptop

Overview

Ridgeline plots are a type of data visualization used to display the distribution of a numerical variable across multiple groups or categories. They are particularly effective for visualizing the density or frequency of data in a way that emphasizes trends and comparisons across groups.

A ridgeline plot combines multiple smoothed density plots, one for each group, stacked or overlaid in a way that they resemble ridges in a landscape—hence the name. It's commonly used when you have a large dataset with grouped variables and want to observe patterns or differences in distributions.

Example of a Ridgeline Plot

example of a ridgeline plot

Creating Ridgeline Plots in ggplot2 with ggridges

To create ridgeline plots in R, install and load the required libraries as shown below.

loading packages in r

Set your working directory and load your data. In this example, I am using the Sample – Superstore dataset to build a ridgeline plot showing distribution of Discounts by Weekday.

setting working directory and loading dataset

Using the numerical variable Discount and the categorical variable Weekday, create a ridgeline plot using the geom_density_ridges function from the ggridges package as shown below.

creating a basic ridgeline plot in r

Executing the above code generates the view below.

a basic example of ridgeline plot in r

Note: You can use the argument rel_min_height to cut the trailing tails and the argument scale to control the scaling of the ridgelines between the categories.

code customizing ridgeline plot in r

Color Customization

Using the fill argument you can change the color of the ridgeline. Note: You can specify the level of transparency using alpha as shown below.

Adding color to ridgeline plot in r

Executing the above generates the view below.

example of ridgeline plot formatted with color

You can use the color argument to control the color of lines, while linetype and lwd are used to control the line type and the line width respectively.

code on customizing ridgeline plots in r

Executing the above generates the view below.

example of formatted ridgeline plot in r

You can apply color based on the categorical variable “Weekday” by passing it to the fill argument under aes as shown below.

customizing ridgeline plot by adding color based on a group

Executing the above generates the view below.

example of ridgeline plot with color formatted by group category

Note: You can change the color using scale_fill_paleteer_d. Use paletteer R package to choose the appropriate color for your viz.

customizing ridgeline plot using paletteer package

Executing the above generates the view below.

example of ridgeline formatted using paletteer package

Applying Color Gradient

Using the function stat(x) to the fill argument of aes, and geom_density_ridges_gradient and a continuous fill color scale – you can fill each ridgeline with a gradient as shown below.

formatting ridgeline by applying color gradient

Executing the above generates the view below.

example of ridgeline plot formatted by applying color gradient

Conclusion

Ridgeline plots in R are a powerful and visually appealing method for exploring and comparing the distribution of data across multiple categories. Utilizing the ggridges package, they provide an efficient way to stack and overlay density plots, enabling users to identify patterns, trends, and differences between groups at a glance.

Ridgeline plots not only enhance the visual storytelling of data but also streamline the process of analyzing and presenting complex distributions. When used effectively, they serve as invaluable tools in data analysis, helping researchers and analysts turn raw data into meaningful, actionable insights.

If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!

Thank you for reading!

Tags:

 
 
Original.png

We Support You Deliver Business-Focused Solutions That Enable Data-Driven Decision Making.

  • Tableau profile
  • YouTube
  • White LinkedIn Icon
  • Facebook
  • X

QUICK LINKS

CONTACT US

WhatsApp: +254 738 307 495

East Gate Mall, Donholm

3rd Floor Suite No. 3i

Nairobi, Kenya

Join our mailing list

bottom of page