如何为时间序列数据绘制非线性趋势线

时间:2019-01-21 02:25:08

标签: r ggplot2 time-series

这是我的数据

  time               dprice
2018-03-05 09:00:00 113.34000
2018-03-05 09:05:00   0.00000
2018-03-05 09:10:00  98.47778
2018-03-05 09:15:00 127.85833
2018-03-05 09:20:00  42.33333

我想将非线性线绘制到该数据中,我的代码是:

library(ggplot2)

ggplot(df1_all, aes(df1_all$Time, df1_all$dprice)) + 
    geom_point() + 
    geom_smooth()

,结果如下:

enter image description here

没有非线性趋势线

1 个答案:

答案 0 :(得分:0)

假设输入在末尾的注解中可重复显示,则生成一个图形:

library(ggplot2)

ggplot(df1_all, aes(time, dprice)) + 
    geom_point() + 
    geom_smooth()

screenshot

或者尝试这种方式:

library(ggplot2)
library(zoo)

z <- read.zoo(df1_all)
autoplot(z) + 
  geom_smooth()

注意

假定输入为可复制形式。请注意,time被假定为POSIXct。

Lines <- "time,dprice
2018-03-05 09:00:00,113.34000
2018-03-05 09:05:00,0.00000
2018-03-05 09:10:00,98.47778
2018-03-05 09:15:00,127.85833
2018-03-05 09:20:00,42.33333"
df1_all <- read.csv(text = Lines)
df1_all$time <- as.POSIXct(df1_all$time)