【问题标题】:error when adding lm to datetime data in ggplot2将lm添加到ggplot2中的日期时间数据时出错
【发布时间】:2022-01-28 04:42:53
【问题描述】:

当尝试绘制数据的线性回归时,输出会返回一条水平线,并警告拟合等级不足。有人可以解释一下如何显示正确的线性回归吗?

样本数据

ToPlot <- structure(list(Time = structure(c(1643126476.049, 1643126476.099, 
1643126476.149, 1643126476.199, 1643126476.249, 1643126476.299, 
1643126476.349, 1643126476.399, 1643126476.449, 1643126476.499, 
1643126476.549, 1643126476.599, 1643126476.649, 1643126476.699, 
1643126476.749, 1643126476.799, 1643126476.849, 1643126476.899, 
1643126476.949, 1643126476.999), class = c("POSIXct", "POSIXt"
), tzone = ""), LIN = c(112.05611159876, 112.66549348, 113.37316276144, 
113.86459976244, 114.59192652392, 115.29959580536, 115.88932020656, 
116.61664696804, 117.20637136924, 117.93369813072, 118.52342253192, 
119.13280441316, 119.8404736946, 120.54814297604, 121.15752485728, 
121.86519413872, 122.47457601996, 123.1822453014, 123.79162718264, 
124.49929646408)), row.names = 314:333, class = "data.frame")

代码:

library("ggplot2")
library("scales")

ggplot(aes(x = Time, y = LIN), data = ToPlot) + 
  geom_point() + 
  geom_line() + 
  scale_x_datetime(labels = date_format("%H:%m:%S"))+
  geom_smooth(method='lm')

【问题讨论】:

标签: r ggplot2


【解决方案1】:

通常,包会自动选择方向(x 或 y)。但是,当它变得不确定时,您会看到该消息。您无需指定公式,只需指定方向即可。

library("ggplot2")
library("scales")

ggplot(aes(x = Time, y = LIN), data = ToPlot) + 
  geom_point() + 
  geom_line() + 
  scale_x_datetime(labels = date_format("%H:%m:%S"))+
  geom_smooth(method='lm', orientation = "y")

【讨论】:

  • 这是因为您正在执行回归 Time ~ LIN 而不是 LIN ~ Timelm 管理第一个而不是第二个。
【解决方案2】:

这不是ggplot 问题,而是lm 问题:

lm(LIN ~ Time, data = ToPlot)
#> 
#> Call:
#> lm(formula = LIN ~ Time, data = ToPlot)
#> 
#> Coefficients:
#> (Intercept)         Time  
#>      118.2           NA  

我认为这里的问题不在于 POSIXct 格式,而只是因为某种原因它似乎会阻塞大量数字(如果将 Time 转换为数字,则会得到相同的结果)。

解决这个问题的一种方法是简单地减去 formula 参数中 POSIXct 中的大常数秒数:

ToPlot <- structure(list(Time = structure(c(1643126476.049, 1643126476.099, 
1643126476.149, 1643126476.199, 1643126476.249, 1643126476.299, 
1643126476.349, 1643126476.399, 1643126476.449, 1643126476.499, 
1643126476.549, 1643126476.599, 1643126476.649, 1643126476.699, 
1643126476.749, 1643126476.799, 1643126476.849, 1643126476.899, 
1643126476.949, 1643126476.999), class = c("POSIXct", "POSIXt"
), tzone = ""), LIN = c(112.05611159876, 112.66549348, 113.37316276144, 
113.86459976244, 114.59192652392, 115.29959580536, 115.88932020656, 
116.61664696804, 117.20637136924, 117.93369813072, 118.52342253192, 
119.13280441316, 119.8404736946, 120.54814297604, 121.15752485728, 
121.86519413872, 122.47457601996, 123.1822453014, 123.79162718264, 
124.49929646408)), row.names = 314:333, class = "data.frame")

library("ggplot2")
library("scales")

datemin <- floor(as.numeric(min(ToPlot$Time)))

ggplot(aes(x = Time, y = LIN), data = ToPlot) + 
  geom_point() + 
  geom_line() + 
  scale_x_datetime(labels = date_format("%H:%m:%S"))+
  geom_smooth(formula = y ~ I(x - datemin), method='lm')

reprex package 创建于 2022-01-27 (v2.0.1)

【讨论】:

  • 谢谢,如果您可以自动获取最大值,我会认为它已解决。
  • 我收到错误object 'y' not found
  • 奇怪。上面的代码是一个完整的表示,包括根据要求自动生成最小值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多