如何使用R输入时间序列图中的日期?

问题描述 投票:1回答:1

我无法正确输入时间序列中的日期。我不知道怎么了。

days <- seq(from=as.Date("2020-02-26"), to=as.Date("2020-04-13"), by="day")
x1 <- rnorm(length(days),3,0.5)
x2 <- rnorm(length(days),6,1.2)
df <- data.frame(days,x1,x2)

df_ts <- ts(df$x1, start=c(2020,1), end=c(2020,48), frequency=365)
df_ts1

>Time Series:
>Start = c(2020, 2) 
>End = c(2020, 4) 
>Frequency = 365 
>[1] 2.682319 3.039276 2.201990

'Start'和'End'参数是正确的,但是输出是错误的[1] 2.682319 3.039276 2.201990然后我更改了start和end参数:

df_ts2 <- ts(df$x1, start=c(2020,1), end=c(2020,48), frequency=365)
df_ts2

>Time Series:
>Start = c(2020, 1) 
>End = c(2020, 48) 
>Frequency = 365 
>[1] 2.682319 3.039276 2.201990 3.708056.....................

在这种情况下,'开始'和'结束'参数是错误的,但是输出是正确的。

同样,当我绘制它时,X轴也是这样:2020.00 2020.04 2020.08 2020.12

我该如何解决?我想输出带有天数或常规日期的时间序列。

r date plot time-series frequency
1个回答
0
投票

如果您在对象类方面比较灵活,则可能要考虑使用xts包。

library(xts)

days <- seq(from=as.Date("2020-02-26"), to=as.Date("2020-04-13"), by="day")
x1 <- rnorm(length(days),3,0.5)
x2 <- rnorm(length(days),6,1.2)
df <- data.frame(days,x1,x2)

df_ts <- xts(df$x1, order.by = days)
plot(df_ts)
© www.soinside.com 2019 - 2024. All rights reserved.