R 如何将几何体线段添加到几何体密度梯度?

R 如何将几何体线段添加到几何体密度梯度?,r,ggplot2,ridgeline-plot,R,Ggplot2,Ridgeline Plot,我想在山脊线图中添加垂直段,其直方图显示定制的分位数 如果我用.x..映射填充颜色,我成功地获得了垂直线段。但我想在密度图中显示分位数。我编写了以下代码: library(datasets) library(ggplot2) data("iris") iris_lines <- data.frame(Species = c("setosa", "versicolor", "virginica"),

我想在山脊线图中添加垂直段,其直方图显示定制的分位数

如果我用
.x..
映射填充颜色,我成功地获得了垂直线段。但我想在密度图中显示分位数。我编写了以下代码:

library(datasets)
library(ggplot2)
data("iris")

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

Figure1 <- ggplot(iris, aes(x=Sepal.Length, y=Species, fill=(..quantile..))) +
  geom_density_ridges_gradient(jittered_points = FALSE, calc_ecdf = TRUE, quantile_lines = c(TRUE), quantiles =c(0.1,0.25,0.75,0.9),scale=0.9, color='white')+
  geom_segment(data = iris_lines, aes(x = x0, xend = x0, y = as.numeric(Species), yend = as.numeric(Species) + c(.9,.5,.5)), color = "red") + scale_y_discrete(expand = c(0.01, 0))
Figure1
很好的图表

inherit.aes=F
添加到第二个geom,这样它就不会试图将数据与
ggplot(aes()
调用)中的填充计算相匹配

Figure1 <- ggplot(iris, aes(x=Sepal.Length, y=Species, fill=(..quantile..))) +
      geom_density_ridges_gradient(jittered_points = FALSE, 
                                   calc_ecdf = TRUE, 
                                   quantile_lines = c(TRUE), 
                                   quantiles =c(0.1,0.25,0.75,0.9),
                                   scale=0.9, color='white') +
      geom_segment(data = iris_lines, 
                   aes(x = x0, xend = x0, 
                       y = as.numeric(Species), yend = as.numeric(Species) + c(.9,.5,.5)),
                   color = "red", inherit.aes = F) +   #### HERE ####
      scale_y_discrete(expand = c(0.01, 0))
Figure1

图1 Jon,我被困了几天,非常感谢你帮我解决这个问题!我不知道
inherit.aes
。只是一个关于标签的快速问题。是否可以只显示标签的前三个元素,并为垂直红线添加第四个元素?这是可能的,但我不知道简单的方法。请参阅使用手动标尺编辑答案。请注意,填充和颜色标尺需要共享一个名称,以便在组合基础上显示。欢迎使用SO。从您对下面答案的评论来看,我认为该答案很有用。请花点时间通过向上投票和/或将其标记为“正确”答案来将答案标记为有用。
Figure1 <- ggplot(iris, aes(x=Sepal.Length, y=Species, fill=(..quantile..))) +
      geom_density_ridges_gradient(jittered_points = FALSE, 
                                   calc_ecdf = TRUE, 
                                   quantile_lines = c(TRUE), 
                                   quantiles =c(0.1,0.25,0.75,0.9),
                                   scale=0.9, color='white') +
      geom_segment(data = iris_lines, 
                   aes(x = x0, xend = x0, 
                       y = as.numeric(Species), yend = as.numeric(Species) + c(.9,.5,.5)),
                   color = "red", inherit.aes = F) +   #### HERE ####
      scale_y_discrete(expand = c(0.01, 0))
Figure1
Figure1 <- ggplot(iris, aes(x=Sepal.Length, y=Species, 
                            fill = (..quantile..), 
                            color = (..quantile..))) +
  geom_density_ridges_gradient(jittered_points = FALSE, 
                               calc_ecdf = TRUE, 
                               quantile_lines = c(TRUE), 
                               quantiles =c(0.1,0.25,0.75,0.9),
                               scale=0.9, color='white') +
  geom_segment(data = iris_lines, 
               aes(x = x0, xend = x0, fill = "median",
                   y = as.numeric(Species), 
                   yend = as.numeric(Species) + c(.9,.5,.5),
                   color = "median")) +   #### HERE ####
  scale_y_discrete(expand = c(0.01, 0)) +

  scale_color_manual(name = "quantile",
                     limits = c(1:3, "median"),
                     values = alpha("firebrick1", c(0, 0, 0, 1)),
                     labels = c("<10%", "10-25%", "IQR", "median")) +
  scale_fill_manual(name = "quantile",
    limits = c(1:3, "median"),
    values = c("cadetblue", "coral", "orange", "white"), 
    na.value = "gray30",
    labels = c("<10%", "10-25%", "IQR", "median"))
Figure1