0

I would like to color a specific area, where col_bol = 1 in a ggridges density curve plot. I know its possible to do this using stat_density_ridges() providing quantiles. If I dont have the quantiles I calculate the percent. This works if it's only one plot without facets.

How could I achieve it using the data below?

library(tidyverse)
library(ggridges)

plot_data <- iris %>% gather(key = 'key', value = 'value', -Species) %>% mutate(col_bol =  case_when(
  Species == 'setosa' & (value < 1.5 | value > 6) ~ 1,
  Species != 'setosa' & (value < 1.3 | value > 6.2) ~ 1,
  TRUE ~ 0))


ggplot(data = plot_data, mapping = aes(y = key, x = value)) +
  geom_density_ridges() +
  facet_wrap(~Species, scales = 'free_x') + 
  theme_ridges()
Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
MLEN
  • 2,162
  • 2
  • 20
  • 36

1 Answers1

0

I am not meeting any particular trouble to plot it with a facet.

If I take your examples with using quantiles :

ggplot(data = plot_data,
       mapping = aes(y = key, x = value, fill = factor(stat(quantile)))) +
  stat_density_ridges(
    geom = "density_ridges_gradient",
    calc_ecdf = TRUE, 
    quantiles = c(0.025, 0.975)) +
  facet_wrap(.~Species, scales = 'free_x') +
  theme_ridges()

it returns a facets plots with the wished cols.

enter image description here

Jrm_FRL
  • 1,394
  • 5
  • 15
  • Yes with quantiles there is not a problem. But how would you color according to the column col_bol ? – MLEN May 18 '20 at 17:35
  • Or how much would color the facets according to different quantiles? – MLEN May 19 '20 at 06:06