0

我想col_bol = 1ggridges密度曲线图中为特定区域着色。stat_density_ridges()我知道使用提供分位数可以做到这一点。如果我没有分位数,我会计算百分比。如果它只是一个没有方面的情节,则此方法有效。

我怎样才能使用下面的数据来实现它?

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()
4

1 回答 1

0

我没有遇到任何特别的麻烦来用一个方面来绘制它。

如果我以使用分位数为例:

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()

它返回一个带有希望的列的构面图。

在此处输入图像描述

于 2020-05-18T14:21:33.973 回答