• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / ggridges package / How To Plot Ridgeline Plots in R?

How To Plot Ridgeline Plots in R?

March 16, 2018 by cmdlinetips

Ridgeline plots is a great way to visualize changes in multiple distributions/histogram either over time or space. It was initially called as joyplots, for a brief time. ggridges package from UT Austin professor Claus Wilke lets you make ridgeline plots in combinaton with ggplot. Here is how Claus describes the ridgeline plot with a brief histroy

Ridgeline plots are partially overlapping line plots that create the impression of a mountain range. They can be quite useful for visualizing changes in distributions over time or space. These types of plots have also been called “joyplots”, in reference to the iconic cover art for Joy Division’s album Unknown Pleasures. However, given the unfortunate origin of the name Joy Division, the term “joyplot” is now discouraged.

Let us get started plotting a few examples of ridgeline plots with ggridges.

How to Install ggridges?

# install stable version
install.packages("ggridges")

To install latest development version,

library(devtools)
install_github("clauswilke/ggridges")

The ggridges package has two types of geoms geom_ridgeline and geom_density_ridges. geom_ridgeline() draws ridgelines() by taking the heights directly from the data. geom_density_ridges() estimates densities from the data and then draws those using ridgelines.

We will using gapminder data to make ridgeline plots. Let us get the data from Software Carpentry URL.

data_url = 'http://bit.ly/2cLzoxH'
# read data from url as dataframe
gapminder = read.csv(data_url)

The gapminder data has for 12 years. Let us plot a ridgeline plot between year and lifeExp using ggridges and ggplot. So we will first specify data and the aesthetics for the plot. And then we can add the layer for ridgeline plot by letting it estimate the density using geom_density_ridges().

ggplot(gapminder, aes(y=as.factor(year),
                      x=lifeExp)) +
  geom_density_ridges(alpha=0.5) +
  scale_y_discrete(expand = c(0.01, 0)) +  
  scale_x_continuous(expand = c(0, 0))+
  theme(axis.text=element_text(size=20))

We get a really nice looking plot with mountain ranges or ridge lines of life expectancy over the years. We can clearly see the pattern of increase in life expectancy over time. We can also see that life expectancy varies widely within a year and also see multiple modes within a year.

Ridgeline Plots in R with ggridges
Ridgeline Plots in R with ggridges

Note that the above ridgeline plot simply plots life expectancy over years. One of the reasons for multiple modalities, is that within a year different continents could have different distributions of life expectancy. We can see that, by coloring each continent separately within aesthetics.

ggplot(gapminder, aes(y=as.factor(year),
                      x=lifeExp,
                      fill=continent)) +
  geom_density_ridges(alpha=0.25) +
  scale_y_discrete(expand = c(0.01, 0)) +  
  scale_x_continuous(expand = c(0, 0))

Here is the ridgeplot by coloring continents and we can see the different distributions of life expectancy with in a year.


Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Boxplot with jittered Data Points in RHow to Make Boxplot in R with ggplot2? How To Specify Colors to Scatter Plots in Python Catplot: Boxplot with jitter SeabornCatplot Python Seaborn: One Function to Rule All Plots With Categorical Variables Adjusting Transparency in Scatter PlotHow To Make Scatter Plot in Python with Seaborn?

Filed Under: ggridges package, R Tips, Ridgeline Plots Tagged With: ggplot, ggridges package, R Tips, Ridgeline Plots

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version