Adding multiple mean bars to ridgeline plot.

I was able to add mean bars to my ridgeline plots using the code below. However, I'd like to differentiate the line type to distinguish between the means for NU and U on each line. I've attached an image of another plot type to show what I would like the lines to look like.

fig2 <- ggplot(combined_sla, aes(x=SLA,y=Data,fill=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')) +
  theme_classic2()
print(fig2)

What I'd like mean lines to look like (one solid, one dashed):

Thanks!

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.

Awesome! That works pretty well. The only issue is there are these two keys. Can you think of a way to combine the right line types with the right fill key? See below:
image

Actually at this point I feel it's easier to just forgo geom_density_ridge() and use a standard geom_density() with the appropriate faceting:

library(tidyverse)

combined_sla <- tibble(SLA = c(rnorm(20,15,20), rnorm(20,20,20), rnorm(20,20,20),
                               rnorm(20,15,20), rnorm(20,20,20), rnorm(20,20,20),
                               rnorm(20,10,20), rnorm(20,22,20), rnorm(20,20,20)),
                       Data = rep(c("California","South Africa","Puerto_Rico"), each = 60),
                       Soil = rep(c("BIEN","NU","U"), each = 20) |> rep(times=3))


combined_sla_means <- combined_sla |>
  group_by(Data,Soil) |>
  summarize(mean = mean(SLA),
            .groups = "drop")

ggplot(combined_sla) +
  geom_density(aes(x=SLA,fill=Soil),
                      alpha = 0.4) +
  geom_vline(aes(xintercept = mean, group = Data, linetype = Soil),
             data = combined_sla_means) +
  facet_grid(rows = vars(Data)) +
  theme_minimal()

Created on 2022-07-06 by the reprex package (v2.0.1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.