将 geom_point 添加到 ggridges

Adding geom_point to ggridges

如果我想向 ggridge 对象添加点估计,但我一直收到错误消息:

library(ggplot2)
library(ggridges)

iris_med <- iris %>% group_by(Species) %>% summarise(Sepal.Length = median(Sepal.Length))

ggplot(iris, aes(x = Sepal.Length, y = Species, fill = 0.5 - abs(0.5-..ecdf..))) +
  stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) +
  geom_point(aes(x = Sepal.Length, y = Species, color = "red"), data = iris_med)

Picking joint bandwidth of 0.181
Error in eval(expr, envir, enclos) : object 'ecdf' not found

我希望实现的输出:

geom_point调用中指定inherit.aes = F即可解决问题:

ggplot(iris, aes(x = Sepal.Length, y = Species, fill = 0.5 - abs(0.5-..ecdf..))) +
  stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) +
  geom_point(aes(x = Sepal.Length, y = Species, color = "red"), data = iris_med, inherit.aes = F)

仅生成以下消息:

Picking joint bandwidth of 0.181

编辑:另一种方法(感谢@Axeman 的评论)是将 fill 美学移动到 stat_density_ridges 层。