在 r 中添加了斜线的多密度图

multiple density plot with slope line added in r

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

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)

红线有两种构造方法。您可以 (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")