4

If I want to add a point estimate to a ggridge object, but I keep getting an error:

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

Output I am hoping to achieve: enter image description here

Hanjo Odendaal
  • 1,395
  • 2
  • 13
  • 32

1 Answers1

4

The problem can be solved by specifying inherit.aes = F in the geom_point call:

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)

produces just he following message:

Picking joint bandwidth of 0.181

enter image description here

EDIT: Another approach (thanks to @Axeman's comment) would be to move the the fill aesthetic to stat_density_ridges layer.

missuse
  • 19,056
  • 3
  • 25
  • 47