Map other variable to fill in R ggridges

18 Views Asked by At

I would like to create geom_density_ridges_gradient plot, but use different fill then x variable. Is it possible? I have not seen any example on internet

Example of plot:

The only difference, I would like to map different variable to fill. It could be speed, compared to speed limit enter image description here

Example data:

df <- data.frame(
    road = c('a', 'b'),
    speed = runif(100, 1, 100)
  ) %>%
  mutate(limit = ifelse(road == 'a', 50, 30),
         overlimit = speed - limit)

What works:

ggplot(df, aes(x = speed, y = road,  fill = stat(x))) +
  geom_density_ridges_gradient() 

What does not work:

ggplot(df, aes(x = speed, y = road,  fill = overlimit)) +
  geom_density_ridges_gradient()

enter image description here

Error message:

The following aesthetics were dropped during statistical transformation: fill
i This can happen when ggplot fails to infer the correct grouping structure in the data.
i Did you forget to specify a `group` aesthetic or to convert a numerical variable into a
  factor?
0

There are 0 best solutions below