Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mccarthy-m-g/80ce14c631f9e090ca8b26b1379124f7 to your computer and use it in GitHub Desktop.
Save mccarthy-m-g/80ce14c631f9e090ca8b26b1379124f7 to your computer and use it in GitHub Desktop.

Question

Okay folks how alive is the #rstats #dataviz crowd here? I want to create ridgeline plots with ggridges::geom_density_ridges, but with the trim=T option of ggplot::geom_density. There's an open feature request at wilkelab/ggridges#57 so I don't think @clauswilke has been able to implement this

Interested in ideas! Main reason to want ridges instead of vanilla density plots is multifaceted data — see example plot.

https://scholar.social/@dingemansemark/109659218719054960

Code

This can be done nicely with ggdist. See this article in the documentation for more info: https://mjskay.github.io/ggdist/articles/slabinterval.html#creating-ridge-plots

library(ggplot2)
library(ggdist)

set.seed(126)

group_n <- 30

facet_df <- data.frame(
  group = rep(rep(rep(letters[1:3], 3), each = group_n), each = 2),
  facet = rep(letters[1:2], group_n * 3),
  x = rep(rep(c(
    rnorm(group_n, 0, 0.25),
    rbeta(group_n, 1, 5, 4),
    rbeta(group_n, 1, 5, 4)
  ), 3), each = 2)
)

ggplot(facet_df, aes(x = x, y = group)) +
  stat_slab(height = 2, colour = "black") +
  facet_wrap(vars(facet))

Created on 2023-01-09 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment