Very partial answer.
Checking the source code of geom_density_ridge()
, there is an entry for:
# vline aesthetics, all inherited
vline_colour = NULL, #vline_color = NULL,
vline_size = NULL, vline_linetype = NULL
And indeed, it seems to work:
library(ggridges)
combined_sla <- tibble(SLA = c(rnorm(20,15,20), rnorm(20,20,20),
rnorm(20,10,20), rnorm(20,22,20)),
Data = rep(c("California","South Africa"), each = 40),
Soil = rep(c("A","B"), each = 20) |> rep(times=2))
ggplot(combined_sla, aes(x=SLA,y=Data,fill=Soil, vline_linetype = Soil)) +
geom_density_ridges(scale = 1, show.legend = TRUE, alpha = 0.4,
quantile_lines=TRUE, quantile_fun=function(x,...)mean(x),
) + theme_ridges() +
scale_y_discrete(expand = c(0.1, 0)) +
scale_x_continuous(expand = c(0.1, 0)) +
labs(x = expression(paste("SLA (", mm^{2},mg^-1,")")),y = "", fill="") +
xlim(0,42) +
scale_fill_manual(values = c('lightblue','gray45','lemonchiffon1'))
However:
- I'm not sure the package authors mean to expose this to users (as it's not in the documentation), so it could stop working in future versions
- I can't think of a way to change the linetype scale (to use e.g. dashed and dotted)
Another way is defining these lines in a different stat/geom:
combined_sla_means <- combined_sla |>
group_by(Data,Soil) |>
summarize(mean = mean(SLA),
.groups = "drop")
ggplot(combined_sla) +
geom_density_ridges(aes(x=SLA,y=Data,fill=Soil),
scale = 1, show.legend = TRUE, alpha = 0.4,
quantile_lines=FALSE) +
geom_vline(aes(xintercept = mean, group = Data, linetype = Soil),
data = combined_sla_means) +
theme_ridges() +
scale_y_discrete(expand = c(0.1, 0)) +
scale_x_continuous(expand = c(0.1, 0)) +
labs(x = expression(paste("SLA (", mm^{2},mg^-1,")")),y = "", fill="") +
xlim(0,42) +
scale_fill_manual(values = c('lightblue','gray45','lemonchiffon1'))
But the vertical lines span all the Data
locations, so probably not what you want.