添加自定义垂直线joyplots ggridges

sda*_*aza 5 r ggplot2 ggridges ridgeline-plot

我想使用ggridges.

# toy example
ggplot(iris, aes(x=Sepal.Length, y=Species, fill=..x..)) +
geom_density_ridges_gradient(jittered_points = FALSE, quantile_lines = 
FALSE, quantiles = 2, scale=0.9, color='white') +
scale_y_discrete(expand = c(0.01, 0)) +
theme_ridges(grid = FALSE, center = TRUE)
Run Code Online (Sandbox Code Playgroud)

我想在 7 处为 virginica 添加一条垂直线,为 versicolor 添加 4 条,为 setosa 添加 5 条垂直线。关于如何做到这一点的任何想法?

Cla*_*lke 2

由于您的密度不重叠,因此最简单的方法可能是添加额外的段。

iris_lines <- data.frame(Species = c("setosa", "versicolor", "virginica"),
                         x0 = c(5, 4, 7))

ggplot(iris, aes(x=Sepal.Length, y=Species, fill=..x..)) +
  geom_density_ridges_gradient(jittered_points = FALSE, quantile_lines = 
                                 FALSE, quantiles = 2, scale=0.9, color='white') +
  geom_segment(data = iris_lines, aes(x = x0, xend = x0, y = as.numeric(Species),
                                      yend = as.numeric(Species) + .9),
               color = "red") +
  scale_y_discrete(expand = c(0.01, 0)) +
  theme_ridges(grid = FALSE, center = TRUE)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 这在 2022 年行不通 (3认同)