【问题标题】:multiple density plot with slope line added in r在 r 中添加斜线的多密度图
【发布时间】:2021-08-10 15:23:44
【问题描述】:

我想创建包含多个组的密度图并为均值添加斜线。情节如下所示:

library(tidyverse)
library(ggridges)
data1 <- data.frame(x1 = c(rep(1,50), rep(2,50), rep(3,50), rep(4,50), rep(5,50)),
                    y1 = c(rnorm(50,10,1), rnorm(50,15,2), rnorm(50,20,3), rnorm(50,25,3), rnorm(50,30,4)))
data1$x1 <- as.factor(data1$x1)
ggplot(data1, aes(x = y1, y = x1, fill = 0.5 - abs(0.5 - stat(ecdf)))) +
  stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) +
  scale_fill_viridis_c(name = "Tail probability", direction = -1)

【问题讨论】:

    标签: r ggplot2 base density-plot


    【解决方案1】:

    有两种方法可以构建红线。您可以 (1) 通过表示组均值的点使用 geom_line,或者 (2) 通过数据拟合回归。

    (1) 将被截断以适应数据,(2) 可以扩展到数据之外,但只有在 x 和 y 之间存在整体线性关系时才会看起来正确。

    (1) 的代码

    means <- aggregate(y1 ~ x1, data=data1, FUN=mean)
    
    ggplot(data1, aes(x = y1, y = x1, fill = 0.5 - abs(0.5 - stat(ecdf)))) +
      stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) +
      scale_fill_viridis_c(name = "Tail probability", direction = -1) +
      geom_line(aes(x=y1, y=as.numeric(x1), fill=1), data=means, colour="red")
      // NB: need to override the fill aesthetic or you get an error
    

    (2) 的代码

    regressionLine <- coef(lm(as.numeric(x1) ~ y1 , data=data1))
    ggplot(data1, aes(x = y1, y = x1, fill = 0.5 - abs(0.5 - stat(ecdf)))) +
      stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE) +
      scale_fill_viridis_c(name = "Tail probability", direction = -1) +
      geom_abline(intercept=regressionLine[1], slope=regressionLine[2], colour="red")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 2020-08-21
      • 2011-11-30
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 2021-07-08
      • 2018-09-17
      相关资源
      最近更新 更多