How to Create Ridgeline Plot in ggplot2 with ggridges
- Bernard Kilonzo
- Apr 18
- 2 min read

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

Creating Ridgeline Plots in ggplot2 with ggridges
To create ridgeline plots in R, install and load the required libraries as shown below.

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.

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.

Executing the above code generates the view below.

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.

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.

Executing the above generates the view below.

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.

Executing the above generates the view below.

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

Executing the above generates the view below.

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

Executing the above generates the view below.

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.

Executing the above generates the view below.

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!