0

Hi everyone I was looking at some ridgeline plots in R, more specifically in the post "Introduction to ggridges", and my goal is to make this plot but without the overlap:

library(ggplot2)
library(ggridges)
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = 0.5 - abs(0.5 - stat(ecdf)))) +
  stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) +
  scale_fill_viridis_c(name = "Tail probability", direction = -1)

By reading the same post, the overlap can be added with:

+ geom_density_ridges(scale = 0.9)

but for some reason I got this error:

Error in 0.5 - ecdf : non-numeric argument to binary operator

Thank you

Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
mauron
  • 9
  • 2
  • Follow this article :The grouping aesthetic does not need to be provided if a categorical variable is mapped onto the y axis, but it does need to be provided if the variable is numerical. (https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html) – Sang won kim Jun 18 '20 at 03:54
  • So you need transform categorical variable to numeric value. you can see example this article. – Sang won kim Jun 18 '20 at 03:55

1 Answers1

0

To get the dsired result get rid of the second geom_density_ridges (this caused the error) and simply add scale = 0.9 to the first layer:

library(ggplot2)
library(ggridges)
ggplot(iris, aes(x = Sepal.Length, y = Species, fill = 0.5 - abs(0.5 - stat(ecdf)))) +
  stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE, scale = 0.9) +
  scale_fill_viridis_c(name = "Tail probability", direction = -1)
#> Picking joint bandwidth of 0.181

stefan
  • 90,330
  • 6
  • 25
  • 51