非重叠脊线图R

Non-overlapping ridgeline plot R

大家好我正在看 R 中的一些脊线图,更具体地说是在 post“ggridges 简介”中,我的目标是制作这个图但没有重叠:

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)

通过读取相同的post,可以添加重叠:

+ geom_density_ridges(scale = 0.9)

但由于某种原因我得到了这个错误:

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

谢谢

要得到想要的结果,去掉第二个 geom_density_ridges(这导致了错误)并简单地将 scale = 0.9 添加到第一层:

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