【问题标题】:How to add geom_segment to geom_density_ridges_gradient?如何将 geom_segment 添加到 geom_density_ridges_gradient?
【发布时间】:2019-06-18 00:33:05
【问题描述】:

我想将垂直线段添加到直方图显示自定义分位数的山脊线图中。

如果我使用..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

如果我将填充颜色映射为fill = ..x..,则代码有效,我得到三条垂直线代表每个密度图的平均值;但是,如果我将填充颜色映射为fill = ..quantile..,则会出现以下错误:

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 1, 3

【问题讨论】:

  • 欢迎来到 SO。从您的评论到下面的答案,我认为答案很有帮助。请花点时间通过投票和/或将其标记为“正确”答案来将答案标记为有用。

标签: r ggplot2 ridgeline-plot


【解决方案1】:

不错的图表!

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


编辑:

OP 在评论中询问选择性地标记某些元素并为中线添加标签。这是一种方法,可能不是最简洁的。

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

【讨论】:

  • 乔恩,我被困了几天,非常感谢你帮助我解决这个问题!,我不知道inherit.aes。只是一个关于标签的快速问题。是否可以只显示标签的前三个元素并为垂直红线添加第四个元素?
  • 有可能,但我不知道一个简单的方法。有关使用手动比例的方法,请参阅编辑后的答案。请注意,填充和色标需要共享一个名称才能组合显示。
猜你喜欢
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 2015-08-15
相关资源
最近更新 更多