• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Make Ridgeline Plot with ggridges in R?

datavizpyr · February 28, 2020 ·

Ridgeline plot is one of the data visualization techniques that is suitable for visualizing multiple distributions or changes in distributions over time or space. Claus Wilke made created this plot and a package in R to make this easily. He defines it as a partially overlapping line plots that create the impression of a mountain range.

In this post we will learn how to make ridgeline plot with the R package ggridges. The R package ggridges is a must add-on for ggplot2.

library(tidyverse)
library(ggridges)
theme_set(theme_bw())

We will use food category and CO2 emission data set from the TidyTuesday project as before to learn to make ridgeline plots. Check out the post on how to make violin plots to learn more about the dataset.

food_consumption <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-18/food_consumption.csv')
head(food_consumption)

The ggridges package offeres types of geoms, geom_ridgeline() and geom_density_ridges(). geom_ridgeline() uses the height values directly to draw ridgelines and geom_density_ridges() estimates densities and then makes ridgeline plot.

We will use geom_density_ridges() function to make ridgeline plot. We can use the function like any other ggplot2 functions and add as another layer after specifying mapping.

food_consumption %>% 
  filter(co2_emmission>=10) %>%
  ggplot(aes(x=co2_emmission, y=food_category, fill=food_category))+
  geom_density_ridges()+
  theme(legend.position = "none")
How to Make Ridgeline Plot with R?
Ridgeline Plot with ggridges

How to Make Ridgeline plot with log-scale on x-axis?

We can make ridgeline plot with log-scale on axis using scale_x_continuous() function and specifying what type of transformation we need with “trans” argument. Since we want to transform the data log10, we use trans=”log10″ as argument.

food_consumption %>% 
  filter(co2_emmission>=10) %>%
  ggplot(aes(x=co2_emmission, y=food_category, fill=food_category))+
  geom_density_ridges()+
  theme(legend.position = "none")+
  scale_x_continuous(trans="log10")
How to Make Ridgeline Plot with log-scale?
Ridgeline plot with log-scale using ggridges in R

How to Order Ridgeline plot?

We will order the ridgeline plot or sort the mountains based on its mean/median using forcats’ fct_reoder() function.

food_consumption %>% 
  filter(co2_emmission>=10) %>%
  ggplot(aes(x=co2_emmission, 
             y=fct_reorder(food_category,co2_emmission), 
             fill=food_category))+
  geom_density_ridges()+
  theme(legend.position = "none")+
  scale_x_continuous(trans="log10")+
  labs(y="Food Category")
How to Order Ridgeline Plot?
How to Order Ridgeline Plot?

Related posts:

Add Mean Line to RIdgeline Plot with ggridges in RHow To Add Mean Line to Ridgeline Plot in R with ggridges? How to Make Heatmap with ggplot2?How To Make Simple Heatmaps with ggplot2 in R? Stacked Barplots Side By Side with ggplot2 in RHow to Make Horizontal Stacked Barplots with ggplot2 in R? Scatter Plot R: Fill color by variableHow To Color Scatter Plot by Variable in R with ggplot2?

Filed Under: R, Ridgeline Plot ggridges, Tidytuesday data Tagged With: ggplot2, ggridges, R

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version