Ridgeline plot for multiple columns (instead of multiple groups of the same column)

113 Views Asked by At

I want to build ridgeline plot where each density corresponds to a different column (not a different group of the same column). Is there a way to do this in ggplot?

1

There are 1 best solutions below

0
Marti On

By doing it I realised it does not make much sense unless you have variables with a similar domain. This is not true in the below solution and hence, while being correct, it produces an ugly plot.

library(ggridges)
library(ggplot2)
library(tidyr) 

# Sample data
data(diamonds)

diamonds$treat <- sample(c(0, 1), nrow(diamonds), replace = TRUE)
# Reshape the data
diamonds_long <- diamonds %>%
  pivot_longer(cols = c("carat", "depth", "price"), names_to = "variable")

ggplot(diamonds_long, aes(x = value, y = variable)) +
  geom_density_ridges()

For such cases, better to take a facet approach.