添加自定义垂直线joyplots ggridges

时间:2018-01-23 02:38:29

标签: r ggplot2 ggridges

我想使用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)

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

1 个答案:

答案 0 :(得分: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)

enter image description here