R语言ggplot绘制时间序列回归直线方程异常问题排查
时间序列回归直线绘制异常问题排查
问题背景
在R语言中使用ggplot2为时间序列数据绘制带回归方程的回归直线时,发现回归直线相关显示存在异常,相关代码、模型摘要及示例数据如下:
相关代码
p1<-ggplot(df2,aes(x=date_his,y=his))+geom_line(lwd=0.8)+ scale_x_date(date_labels = "%Y",expand = c(0, 0))+ theme(plot.title = element_text(size = 14, hjust = 0.5))+ geom_smooth(method = "lm", se = FALSE, linetype = "dashed", color = "blue", size = 1.2)+ ggpubr::stat_regline_equation(label.x = -Inf, label.y = Inf, vjust = 1.5, hjust = -0.1, size = 5,color = "blue", formula = y ~ poly(x, 1), show.legend = FALSE )
回归模型摘要
summary(lm(his ~ date_his, data = df2)) Call: lm(formula = his ~ date_his, data = df2) Residuals: Min 1Q Median 3Q Max -9.5030 -3.7714 0.0661 4.0350 8.2860 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.682e+02 2.246e+00 74.889 < 2e-16 *** date_his -6.247e-04 2.138e-04 -2.922 0.00614 ** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 4.867 on 34 degrees of freedom Multiple R-squared: 0.2007, Adjusted R-squared: 0.1772 F-statistic: 8.538 on 1 and 34 DF, p-value: 0.00614
示例数据
df2 <- data.frame( date_his = as.Date(c( "1979-04-28", "1980-04-28", "1981-04-28", "1982-04-28", "1983-04-28", "1984-04-28", "1985-04-28", "1986-04-28", "1987-04-28", "1988-04-28", "1989-04-28", "1990-04-28", "1991-04-28", "1992-04-28", "1993-04-28", "1994-04-28", "1995-04-28", "1996-04-28", "1997-04-28", "1998-04-28", "1999-04-28", "2000-04-28", "2001-04-28", "2002-04-28", "2003-04-28", "2004-04-28", "2005-04-28", "2006-04-28", "2007-04-28", "2008-04-28", "2009-04-28", "2010-04-28", "2011-04-28", "2012-04-28", "2013-04-28", "2014-04-28" )), his = c( 166.008140637138, 169.867917428802, 157.525649715296, 172.567833065154, 170.267019131607, 168.725866057929, 166.718135941998, 158.34217326036, 157.493444169524, 164.212162698115, 161.140482761292, 161.851683272819, 158.688162091076, 159.249075294438, 153.373329948267, 170.934314928049, 164.557361076648, 169.910429608586, 163.399094199897, 161.238272986288, 166.19105244493, 162.451935740926, 157.307524920014, 164.886371717477, 153.843986514936, 157.656023562196, 164.433730558708, 165.485611515611, 157.622007235028, 165.006218026182, 161.901572985301, 152.963472898683, 154.013037508575, 156.507409368617, 157.244735400283, 161.214916194711 ) )
异常原因分析
模型公式不匹配
geom_smooth(method="lm")默认使用y ~ x拟合线性模型,和手动运行的lm(his ~ date_his)完全一致;但stat_regline_equation中指定了formula = y ~ poly(x, 1),poly(x,1)会对日期变量做正交中心化变换,拟合出的回归系数和原始模型差异极大,导致图表显示的回归方程与实际绘制的回归直线不对应,造成“异常”观感。日期变量的数值特性
R中日期类型会被转换为大整数(如1979-04-28对应数值7083),直接作为自变量拟合时,斜率极小(模型中为-6.247e-04)、截距极大(1.682e+02),回归方程会以科学计数法显示,和直观认知不符,也会让你觉得显示异常。标签位置设置不当
label.x = -Inf, label.y = Inf的设置会将回归方程标签放到绘图区域之外,可能导致看不到方程或位置奇怪,进而误以为是回归直线的问题。
解决办法
- 统一模型公式:将
stat_regline_equation的formula改为y ~ x,与geom_smooth保持一致,这样显示的方程就和手动运行的lm模型结果匹配:ggpubr::stat_regline_equation(label.x = as.Date("1980-01-01"), label.y = 170, vjust = 1, hjust = 0, size = 5,color = "blue", formula = y ~ x, show.legend = FALSE ) - 调整标签位置:将
label.x和label.y设置为绘图范围内的具体日期或数值(如label.x = as.Date("1980-01-01")、label.y = 170),确保方程在可见区域内。 - 优化自变量形式:将日期转换为年份数值,用年份作为自变量拟合,系数会更直观(代表每年的变化量),方程显示也更易读:
df2$year <- lubridate::year(df2$date_his) p1<-ggplot(df2,aes(x=year,y=his))+geom_line(lwd=0.8)+ scale_x_continuous(expand = c(0, 0))+ theme(plot.title = element_text(size = 14, hjust = 0.5))+ geom_smooth(method = "lm", se = FALSE, linetype = "dashed", color = "blue", size = 1.2)+ ggpubr::stat_regline_equation(label.x = 1980, label.y = 170, size = 5,color = "blue", formula = y ~ x, show.legend = FALSE )
内容的提问来源于stack exchange,提问作者CovetTachi
相关产品推荐
相关产品推荐

