【问题标题】:How to set the x-ticks as dates in a time series in R如何将x-ticks设置为R中时间序列中的日期
【发布时间】:2021-02-03 09:03:06
【问题描述】:

我正在使用 R 的预测包,我想显示一个时间序列。当我这样做时,x 刻度从 1 开始并始终以 1 递增。如何将 x 刻度定义为日期?所以我有两个选择:

  1. 从文件中读取时间戳数据
  2. 手动为时间序列分配时间戳

基本上我想知道如何实现这两个选项。到目前为止,这是我的代码。

autoplot(ts(generationData$Demand))

generationData 是一个数据帧,它还包含不同时间序列的时间戳,但它的显示格式不好(“2019-01-01 01:00:00+01:00”、“2019-01-01 02:00” :00+01:00”等)。所以我认为2选项更好。例如,如何将刻度定义为 2019 年(1 月、2 月等)中的月份?

感谢您的每一条评论。

【问题讨论】:

  • 也许stackoverflow.com/questions/4843969/…有你的问题的答案
  • 感谢里卡多的评论。由于我是 R 新手,所以我不理解这些帖子中给出的答案,也不知道如何根据我的情况调整它们。在 autoplot 命令中没有直接的方法吗?
  • 好的,您可以发布几行数据吗?尝试从dput(generationData[1:20,]) 复制并粘贴输出。
  • 另外,你想要什么格式的日期? x 轴应该按月或其他休息时间? @PeterBe
  • 感谢里卡多的回答。由于 generationData 有 20 行和 8700 列,因此命令 dput(generationData[1:20,]) 创建了很多我无法发布的条目。在这里查看它的摘录

标签: r timestamp time-series


【解决方案1】:

由于您希望休息是每月一次,因此您不需要小时部分,因此您可以指定您希望采用以下格式的日期:

format = "%Y-%m-%d" #which means "year, then a "-", then month, then "-", then day"
generationData$Date = as.Date(generationData$Date, format)

假设日期列称为“日期”。然后我们创建ggplot(再次假设具有时间序列值的列称为“Value”):

ggplot(generationData, aes(x=Date, y=Value)) +
#We want the x axis to be Date and y to be the value of the ts
  geom_line() + #Creates a line graph
  scale_x_date(date_breaks="1 month", #Sets the breaks to be monthly
               date_labels="%m") #Sets that every break, the tick should contain only the month value

您可以轻松地将休息时间更改为“3 周”,例如将标签更改为“%m/%d”,以获得 mm/dd 格式。

但是这种方法给出的月份数不太漂亮,要获取月份的名称,您可以使用months 函数创建一个新列:

generationData$Date2 = months(generationData$Date, abbreviate=TRUE)

然后只需将scale_x_date 上的标签更改为这个新列:

scale_x_date(date_breaks="1 month", #Sets the breaks to be monthly
               date_labels=Date2)

可能有一种更简单的方法,只使用自动绘图功能,所以我鼓励你尝试理解只使用它的答案。我希望我没有把它弄得太复杂:)。

【讨论】:

  • 非常感谢里卡多的回答。你能想出一种更简单的方法(也许使用自动绘图)吗?
【解决方案2】:

Quick dislaymer:最后我认为ggplot 更容易。我将尝试以一种可以概括它的方式进行解释,这可能会使它看起来很复杂,但这并不难。另外,我不是自动绘图的天才,所以也许有一种我不知道的更简单的方法。最后,我使用“y”作为时间序列列,使用“date”作为日期。

即使对于“更简单”的自动绘图方法,将您的日期作为日期对象读取也很好,而且并不难:

format = "%Y-%m-%d %H:%M:S"
df$date = as.POSIXct(df$date, format, tz="your time zone code here")

限制值

d = which(df$date=="2019-10-01 00:00:00") #First date you want
e = which(df$date=="2019-12-01 00:00:00") #Last date you want

x 轴的值中断。现在您只想将中断应用于限制中的数据,因此请记住在设置 a 和 n 时。

a = 1 #The date you wish to start the ticks. If you wanted to be the 1st of oct, for example:
a = which(df$date=="2019-10-01 00:00:00")
n = 12 #The number of months there will be in the ticks
k = 720 #The conversion factor, in this case is months-->hours

autoplot(df$y) + xlim(d, e) +
  scale_x_continuous(breaks=seq(a,n*k,k),
                     labels=months(df$date,TRUE))
#Set FALSE to not use abbreviations, Set labels=1:n to use numbers

如您所见,由于您没有将 df$date 作为参数传递给 autoplot,因此您必须“咀嚼”有关中断和限制的信息,而在 ggplot 中不需要这些信息。如果您暂时不需要了解 ggplot 的所有选项,则无需了解此结构:

ggplot(df,aes(x=date,y=y)) + #Pass the data frame, then the x and y column names inside "aes()"
  geom_line() + #For time series, you'll probably always want a line graph
  scale_x_date(breaks="1 month", labels="%m", #Set labels=month(date,TRUE/FALSE) to get month names
               limits=as.POSIXct(c("first date", "last date"), format))

我们只是说“1 个月”,而不是定义 a、b、c 并创建一个中断序列,而是在我们的 df 中寻找限制日期的位置,我们只是说它们是什么时候。更改比例也更容易,如果您想每周进行一次,只需将breaks 更改为"1 week" 并将标签更改为%W,而使用自动绘图则需要重新计算a,n,k。对不起,如果它看起来又复杂了。

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2020-12-07
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多