从 geom_smooth() 中提取多条趋势线的斜率

Extract slope of multiple trend lines from geom_smooth()

我正在尝试使用 ggplot 在时间序列中绘制多条趋势线(每十年一次)。

这是数据:

dat <- structure(list(YY = 1961:2010, a = c(98L, 76L, 83L, 89L, 120L, 
107L, 83L, 83L, 92L, 104L, 98L, 91L, 81L, 69L, 86L, 76L, 85L, 
86L, 70L, 81L, 77L, 89L, 60L, 80L, 94L, 66L, 77L, 85L, 77L, 80L, 
79L, 79L, 65L, 70L, 80L, 87L, 84L, 67L, 106L, 129L, 95L, 79L, 
67L, 105L, 118L, 85L, 86L, 103L, 97L, 106L)), .Names = c("YY", 
"a"), row.names = c(NA, -50L), class = "data.frame")

这是脚本:

p <- ggplot(dat, aes(x = YY))
p <- p + geom_line(aes(y=a),colour="blue",lwd=1)
p <- p + geom_point(aes(y=a),colour="blue",size=2)

p <- p + theme(panel.background=element_rect(fill="white"),
         plot.margin = unit(c(0.5,0.5,0.5,0.5),"cm"),
         panel.border=element_rect(colour="black",fill=NA,size=1),
         axis.line.x=element_line(colour="black"),
         axis.line.y=element_line(colour="black"),
         axis.text=element_text(size=15,colour="black",family="serif"),
         axis.title=element_text(size=15,colour="black",family="serif"),
         legend.position = "top")

p <- p + scale_x_discrete(limits = c(seq(1961,2010,5)),expand=c(0,0))

p <- p + geom_smooth(data=dat[1:10,],aes(x=YY,y=a),method="lm",se=FALSE,color="black",formula=y~x,linetype="dashed")

p <- p + geom_smooth(data=dat[11:20,],aes(x=YY,y=a),method="lm",se=FALSE,color="black",formula=y~x,linetype="dashed")

p <- p + geom_smooth(data=dat[21:30,],aes(x=YY,y=a),method="lm",se=FALSE,color="black",formula=y~x,linetype="dashed")

p <- p + geom_smooth(data=dat[31:40,],aes(x=YY,y=a),method="lm",se=FALSE,color="black",formula=y~x,linetype="dashed")

p <- p + geom_smooth(data=dat[41:50,],aes(x=YY,y=a),method="lm",se=FALSE,color="black",formula=y~x,linetype="dashed")

p <- p + labs(x="Year",y="Number of Days")
outImg <- paste0("test",".png")
ggsave(outImg,p,width=8,height=5)

这是生成的图像:

我WANT/PROBLEMS

  1. 我想提取斜率并将它们添加到图中的趋势线上。如何从 geom_smooth()?

  2. 中提取每条线的斜率
  3. 目前,我正在一条一条地绘制趋势线。我想知道是否有一种有效的方法可以调整时间 window。例如,假设我想绘制每 5 年的趋势线。上图中时间window为10.

  4. 假设,我只想绘制重要的趋势线(即 p 值 < 0.05,空值:没有趋势或斜率等于 0),是否可以使用 geom_smooth()?

如有任何帮助,我将不胜感激。

因此,最好在将数据传输到 ggplot2 之前处理好这些任务中的每一个,但是使用 tidyverse 中的其他一些包,它们都变得相当容易。

从问题 1 和 2 开始:

虽然 ggplot2 可以绘制回归线,但要提取估计的斜率系数,您需要明确地使用 lm() 对象。使用 group_by()mutate(),您可以添加一个分组变量(例如,我下面的代码针对 5 年组执行此操作),然后计算斜率估计并将其提取到现有数据框中的列中。然后可以使用 geom_text() 调用在 ggplot 中绘制这些斜率估计值。我在下面以一种快速而肮脏的方式完成了此操作(将每个标签放置在它们回归的 x 和 y 值的平均值处),但您可以在数据框中指定它们的确切位置。

分组变量和数据准备也让问题 2 变得轻而易举:既然你在数据框中明确地有了分组变量,就不需要一个一个地绘制了,geom_smooth() 接受 group审美的。

此外,为了回答问题 3,您可以从 lm 对象的摘要中提取 pvalue,并仅过滤掉那些对您关心的级别重要的对象。如果您将这个现在完整的数据框传递给 geom_smooth()geom_text(),您将得到您正在寻找的图!

library(tidyverse)

 # set up our base plot
 p <- ggplot(dat, aes(x = YY, y = a)) +
  geom_line(colour = "blue", lwd = 1) +
  geom_point(colour = "blue", size = 2) +
  theme(
    panel.background = element_rect(fill = "white"),
    plot.margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"),
    panel.border = element_rect(colour = "black", fill = NA, size = 1),
    axis.line.x = element_line(colour = "black"),
    axis.line.y = element_line(colour = "black"),
    axis.text = element_text(size = 15, colour = "black", family = "serif"),
    axis.title = element_text(size = 15, colour = "black", family = "serif"),
    legend.position = "top"
  ) +
  scale_x_discrete(limits = c(seq(1961, 2010, 5)), expand = c(0, 0))

# add a grouping variable (or many!)
 prep5 <- dat %>%
  mutate(group5 = rep(1:10, each = 5)) %>%
  group_by(group5) %>%
  mutate(
    slope = round(lm(YY ~ a)$coefficients[2], 2),
    significance = summary(lm(YY ~ a))$coefficients[2, 4],
    x = mean(YY),   # x coordinate for slope label
    y = mean(a)     # y coordinate for slope label
  ) %>%
  filter(significance < .2)   # only keep those with a pvalue < .2 

p + geom_smooth(
  data = prep5, aes(x = YY, y = a, group = group5),  # grouping variable does the plots for us!
  method = "lm", se = FALSE, color = "black",
  formula = y ~ x, linetype = "dashed"
) +
  geom_text(
    data = prep5, aes(x = x, y = y, label = slope),
    nudge_y = 12, nudge_x = -1
  )

现在您在指定文本标签的位置时可能需要比我在这里更加小心。我使用方法和 geom_text()nudge_* 参数来做一个简单的例子,但请记住,因为这些值显式映射到 x 和 y 坐标,所以您可以完全控制!

reprex 创建于 2018-07-16 包 (v0.2.0).