在 R 中绘制时间序列时的日期格式

date format when plotting a time series in R

我的数据框 df 是一个包含 DatumOpbrengst 变量的每日时间序列。 Datum 变量在 2016010120170521 之间。

      Datum  Opbrengst
1   20160101  40609276
2   20160102  79381098
3   20160103 114653269
4   20160104 126044535
5   20160105 180472785
...

我想做预测,所以我做的第一件事就是绘制序列以查看序列是否平稳(如果它具有季节性)。

但是,日期变量是 numeric,所以当我绘制系列时,

 ggplot(data=df, aes(x=Datum , y=Opbrengst, group=1)) +
    geom_line()+
    geom_point()

变成这样:

问题在于该系列跨越了数年,这就是为什么 R 只将其视为 numeric series,而不是 time series

我尝试使用 this website

中的方法将其转换为日期
 df$Datum = as.Date(df$Datum)

但结果不正确:

 "57166-06-26" "57166-06-27" "57166-06-28" "57166-06-29" "57166-06-30" "57166-07-01"

我的问题是:

  1. 如何将基准变量更改为日期格式,以便在绘制图形时不会出现问题?因为稍后我确实需要同时进行 dailyweekly 预测。

  2. 我知道如果我用plot.ts(),那我就不用改时间格式了。我也可以在 ggplot 中绘制时间序列图吗?

[编辑]

这是数据样本:

df <- structure(list(Datum = 20160101:20160120, Opbrengst = c(40609276, 
79381098, 114653269, 126044535, 180472785, 169286880, 149272135, 
133645566, 70171285, 150029065, 149172032, 107843808, 138196732, 
136460905, 133595660, 61716435, 137309503, 193201850, 140766980, 
129859068)), .Names = c("Datum", "Opbrengst"), row.names = c(NA, 
20L), class = "data.frame")

[编辑]

已将 %M 更改为 %m

有很多方法可以做到这一点。三个简单的:

df <- structure(list(Datum = 20160101:20160120, Opbrengst = c(40609276, 79381098, 114653269, 126044535, 180472785, 169286880, 149272135, 133645566, 70171285, 150029065, 149172032, 107843808, 138196732, 136460905, 133595660, 61716435, 137309503, 193201850, 140766980, 129859068)), .Names = c("Datum", "Opbrengst"), row.names = c(NA, 20L), class = "data.frame")

# 1. Using the as.Date function (as sugges5ted by @SBista) to create a date object: 
df$Datum <- as.Date.character(df$Datum, format = "%Y %m %d")

# 2. Or create a POSIXct object:
# df$Datum <- strptime(df$Datum, format = "%Y %m %d")  

# 3. Using 'lubridate' to create a Date or POSIXct object (see 'tz' argument in ?ymd):
# df$Datum <- lubridate::ymd(df$Datum, tz = NULL)

ggplot(data=df, aes(x=Datum , y=Opbrengst)) +
  geom_line()+
  geom_point()

结果:

你的例子的问题在于你没有提供 'format' 参数,所以 R 不知道它是年-月-日。

这里的问题是 df$Datum 到 class Date 的转换。与ggplot2

无关

正在创建示例数据 integer,包括新年:

(Datum <- c(20151224:20151231, 20160101:20160107))
 [1] 20151224 20151225 20151226 20151227 20151228 20151229 20151230 20151231 20160101
[10] 20160102 20160103 20160104 20160105 20160106 20160107

anytime::anydate()lubridate::ymd() 似乎能够将整数 Datum 直接转换为 character.

anytime::anydate(Datum)
# [1] "2015-12-24" "2015-12-25" "2015-12-26" "2015-12-27" "2015-12-28" "2015-12-29"
# [7] "2015-12-30" "2015-12-31" "2016-01-01" "2016-01-02" "2016-01-03" "2016-01-04"
#[13] "2016-01-05" "2016-01-06" "2016-01-07"

lubridate::ymd(Datum)
# [1] "2015-12-24" "2015-12-25" "2015-12-26" "2015-12-27" "2015-12-28" "2015-12-29"
# [7] "2015-12-30" "2015-12-31" "2016-01-01" "2016-01-02" "2016-01-03" "2016-01-04"
#[13] "2016-01-05" "2016-01-06" "2016-01-07"

as.Date() 在这里抛出错误:

as.Date(Datum)
#Error in as.Date.numeric(Datum) : 'origin' must be supplied

as.Date(Datum, "%Y%m%d")
#Error in charToDate(x) : 
#  character string is not in a standard unambiguous format

Datum需要先强制转换为character

as.Date(as.character(Datum), "%Y%m%d")
# [1] "2015-12-24" "2015-12-25" "2015-12-26" "2015-12-27" "2015-12-28" "2015-12-29"
# [7] "2015-12-30" "2015-12-31" "2016-01-01" "2016-01-02" "2016-01-03" "2016-01-04"
#[13] "2016-01-05" "2016-01-06" "2016-01-07"

请注意,格式字符串是 "%Y%m%d" 小写 m 而不是 "%Y%M%d" 大写 M。有趣的是,"%Y %m %d" 中间穿插的空格似乎也有效,在这里。


完整示例

# create data
df <- data.frame(
  Datum = c(20151220:20151231, 20160101:20160108),
  Opbrengst = c(40609276, 79381098, 114653269, 126044535, 180472785, 169286880, 
                149272135, 133645566, 70171285, 150029065, 149172032, 107843808, 
                138196732, 136460905, 133595660, 61716435, 137309503, 193201850, 
                140766980, 129859068))

# coerce to class Date
df$Datum <- anytime::anydate(df$Datum)

library(ggplot2)
ggplot(df, aes(Datum, Opbrengst)) + geom_line() + geom_point()

请注意,新年的差距已经消失。