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

时间:2017-05-31 11:09:32

标签: r date ggplot2 time-series prediction

我的数据框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()

它变成这样:

graph

问题在于该系列跨越多年,这就是为什么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

  3. 中进行时间序列图

    [编辑]

    这是数据样本:

    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")
    

2 个答案:

答案 0 :(得分:1)

<强> [编辑]

%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()

结果:

enter image description here

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

答案 1 :(得分:1)

此处的问题是将df$Datum转换为类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()

enter image description here

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

相关问题