如何根据高度美学在 ggridges 中填充颜色?

How to fill color in ggridges based on height aesthetics?

我正在尝试创建 ggridges 图,其中包含各种 的 Covid19 病例数。我想 填充 颜色 基于 案例计数 而不是基于 美国但无法这样做。

到目前为止,我已尝试使用以下代码:

library(tidyverse)
library(lubridate)
library(ggridges)


df_ind_stacked_daily <- read.csv(url("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/df_ind_stacked_daily.csv")) %>% 
  mutate(Date = ymd(Date))

工作代码:

df_ind_stacked_daily %>%
  filter(Daily_cases_type == "Daily_confirmed",
         State.UnionTerritory != "India") %>% 
  ggplot(aes(x = Date, y = State.UnionTerritory,
             height = Daily_cases_counts, group = State.UnionTerritory)) +
  geom_density_ridges(stat = "identity", alpha = .8,) + # , scale = 2
  
  coord_cartesian(clip = "off") +
  theme_ridges(grid = FALSE)

问题: 当我尝试根据 高度 美学 填充颜色 时,它不是工作

df_ind_stacked_daily %>%
  filter(Daily_cases_type == "Daily_confirmed",
         State.UnionTerritory != "India") %>% 
  ggplot(aes(x = Date, y = State.UnionTerritory, fill = Daily_cases_counts,
             height = Daily_cases_counts, group = State.UnionTerritory)) +
  geom_density_ridges_gradient(stat = "identity", scale = 2, rel_min_height = 0.01, gradient_lwd = 1.) +
  coord_cartesian(clip = "off") +
  theme_ridges(grid = FALSE)

问题是您用于 rel_min_height 的值。不确定是什么原因,但您可以通过将 rel_min_height 的值减少到例如0.001 或使用 0 的默认值,如下所示:

library(tidyverse)
library(ggridges)

df_ind_stacked_daily %>%
  filter(Daily_cases_type == "Daily_confirmed",
         State.UnionTerritory != "India") %>% 
  ggplot(aes(x = Date, y = State.UnionTerritory, fill = Daily_cases_counts,
             height = Daily_cases_counts, group = State.UnionTerritory)) +
  geom_density_ridges_gradient(stat = "identity", scale = 2, gradient_lwd = 1) +
  coord_cartesian(clip = "off") +
  theme_ridges(grid = FALSE) +
  #theme(legend.position = "bottom") +
  labs(fill = NULL)