• 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 Add Mean Line to Ridgeline Plot in R with ggridges?

datavizpyr · May 25, 2020 ·

In this post, we will learn how to add mean line to ridgeline plot. We can make ridgeline plot in R with ggridges R package developed Claus Wilke. Ridgeline plot is useful when you have multiple distributions or a distribution that changes in distributions over time or space. The name “ridgeline” comes from the way it looks like, a overlapping mountain range.

Let us load the packages needed for making ridgeline plots. To make ridgeline plot, we need ggridges package in addition to ggplot2. We also load lubridate to parse date variable in the data.

library(tidyverse)
library(ggridges)
library(lubridate)
theme_set(theme_bw(base_size=16))

We will use Broadway data set from TidyTuesday project to make ridgeline plot and then see how to add mean line to the ridgeline plot.

grosses <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-04-28/grosses.csv', guess_max = 40000)
## Parsed with column specification:
## cols(
##   week_ending = col_date(format = ""),
##   week_number = col_double(),
##   weekly_gross_overall = col_double(),
##   show = col_character(),
##   theatre = col_character(),
##   weekly_gross = col_double(),
##   potential_gross = col_double(),
##   avg_ticket_price = col_double(),
##   top_ticket_price = col_double(),
##   seats_sold = col_double(),
##   seats_in_theatre = col_double(),
##   pct_capacity = col_double(),
##   performances = col_double(),
##   previews = col_double()
## )

The Broadway dataset contains data over multiple years. Here we subset the data to a fewer number of years.

df <- grosses %>% 
  mutate(Year=factor(year(week_ending))) %>%
  filter(Year %in% seq(1985,2020, by=5))

We have data ready to make ridgeline plot. Let us start with making a simple ridgeline plot using geom_density_ridges() function from ggridges package. We will make a ridgeline plot to visualize how weekly gross in broadway shows change over time.

df %>% 
  ggplot(aes(y=Year,x=weekly_gross,fill=Year)) +
  geom_density_ridges()+
  theme(legend.position = "none")

We get a nice ridgeline plot filled with colors by “Year” variable.

RIdgeline Plot with ggridges in R
RIdgeline Plot with ggridges in R

Adding Vertical line at mean in ridgeline plot

The ridgeline plot with vertical mean line for each group, in this case “year”, would help easily understand the mean trend over years. To add mean line to ridgeline plot with ggridges, we need to use quantile_lines and quantile_fun arguments inside geom_density_ridges() function from ggridges package.

df %>%
  ggplot(aes(y=Year,x=weekly_gross,fill=Year)) +
  geom_density_ridges(quantile_lines=TRUE,
                      quantile_fun=function(x,...)mean(x))+
  theme(legend.position = "none")

Now we get a ridgeline plot with vertical mean lines.

Add Mean Line to RIdgeline Plot with ggridges in R
Add Mean Line to RIdgeline Plot with ggridges in R

Thanks to @ClausWilke for ggridges package and the nice solution.

Related posts:

How to Order Ridgeline Plot?How To Make Ridgeline Plot with ggridges in R? Customizing Labels on Bars in Side by side Stacked BarplotHow To Add Labels to Grouped Barplot with Bars Side-By-Side in R? Visualizing Missing Data with Barplot in R ggplot2Visualizing Missing Data with Barplot in R Annotate Clusters with Ellipse with Labels ggforceHow To Annotate Clusters with Circle/Ellipse by a Variable in R

Filed Under: Add Mean Line to Ridgeline plot, ggplot2, R, Ridgeline Plot ggridges Tagged With: ggplot2, 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