不重叠的山脊线图R

时间:2020-06-18 02:59:35

标签: r ggplot2 ggridges iris-dataset ridgeline-plot

大家好,我正在看R中的一些山脊线图,更具体地说是在“ 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)

通过阅读同一篇文章,可以添加重叠:

+ geom_density_ridges(scale = 0.9)

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

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

谢谢

1 个答案:

答案 0 :(得分:0)

要获得所需的结果,请去除第二个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