wilkelab / ggridges

Ridgeline plots in ggplot2
https://wilkelab.org/ggridges
GNU General Public License v2.0
411 stars 31 forks source link

Alternative to `rel_min_height` to truncate density plots to range of data? #92

Open Aariq opened 9 months ago

Aariq commented 9 months ago

When rel_min_height = 0 (default) tails are drawn to the range for all datasets (I assume). I use rel_min_height primarily to make it so those lines are more representative of the range of the data, but why not just use the actual range of the data to truncate the tails instead of fiddling with rel_min_height? Would this be possible? I'm imagining something similar to the trim argument for geom_violin()

library(ggplot2)
library(ggridges)
#> Warning: package 'ggridges' was built under R version 4.3.2
a <- rnorm(1000, 0)
b <- rnorm(1000, 100)
df <- data.frame(
  group = rep(c("A", "B"), each = 1000),
  x = c(a, b)
)

ggplot(df, aes(x = x, y = group)) +
  geom_density_ridges()
#> Picking joint bandwidth of 0.215


#maybe this looks better?
ggplot(df, aes(x = x, y = group)) +
  geom_density_ridges(rel_min_height = 0.001)
#> Picking joint bandwidth of 0.215


#or maybe this?
ggplot(df, aes(x = x, y = group)) +
  geom_density_ridges(rel_min_height = 0.000001)
#> Picking joint bandwidth of 0.215


#but we know the actual range—can that be used to truncate the tails?
range(a)
#> [1] -3.121805  3.351301
range(b)
#> [1]  97.24957 103.07265

Created on 2024-02-09 with reprex v2.0.2