将geom_point添加到ggridges

4

如果我想向 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

我希望实现的输出结果如下图所示:

enter image description here
1个回答

4
问题可以通过在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)

只会产生以下信息:

选择联合带宽为0.181

输入图像描述

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


1
或将“fill”美学移至“stat_density_ridges”。 - Axeman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接