Search code examples
rggplot2ggridges

How to add geom_point to stat_density_ridges


I am able to draw density_ridge using this code. I want to add geom_point at percentile 0.50 without changing the current design. Any help would be much appreciated.

library(ggplot2)
library(ggridges)

 ggplot(iris, aes(x=Sepal.Length, y=Species, fill = factor(stat(quantile)))) +
  stat_density_ridges(
    geom = "density_ridges_gradient", calc_ecdf = TRUE,
    quantiles = 4, quantile_lines = TRUE
  )

enter image description here


Solution

  • Try

    p + geom_point(data = aggregate(Sepal.Length ~ Species, iris, median),
                   aes(x = Sepal.Length, y = Species),
                   color = "red",
                   size = 5,
                   inherit.aes = FALSE)
    

    (along the way you must have called viridis color palette it seems)

    enter image description here

    data

    library(ggplot2)
    library(ggridges)
    
    p <- ggplot(iris, aes(x=Sepal.Length, y=Species, fill = factor(stat(quantile)))) +
      stat_density_ridges(
        geom = "density_ridges_gradient", calc_ecdf = TRUE,
        quantiles = 4, quantile_lines = TRUE
      )