rggridges

How to fill color in ggridges based on height aesthetics?


I am trying to create ggridges plot with Covid19 Cases counts for various States. I would like to fill the color based on Cases counts & Not based on States but unable to do so.

I have attempted below code so far:

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

working code:

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)

enter image description here

Issue: When I try to fill color based on height aesthetics then its not working

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)

Solution

  • The issue is the value you used for rel_min_height. Not sure what's the reason but you could make your code work by reducing the value of rel_min_height to e.g. 0.001 or making use of the default value of 0 as I do below:

    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)