简体   繁体   English

使用 plot.ts() 的时间序列的日期转换问题

[英]Date conversion issue for time series using plot.ts()

I have a dataframe with date in yyyy-mm-dd format and share prices but I can't see the date in the x axis when plotting it using plot.ts() .我有一个 dataframe 日期为yyyy-mm-dd格式和股价,但在使用plot.ts()绘制时,我无法在 x 轴上看到日期。 I have tried a few alternatives mentioned below but they did not work.我尝试了下面提到的一些替代方案,但它们不起作用。

在此处输入图像描述

I converted the date using我使用转换日期

  1. data$Date<-as.Date(data$Date, "%Y-%m-%d") OR data$Date<-as.Date(data$Date, "%Y-%m-%d")
  2. data$Date<-ymd(data$Date) using the lubridate package data$Date<-ymd(data$Date)使用润滑 package

and then did然后做了

bby <- ts(data=data$Share_price, frequency=2, start=c(data[1,"Date"]))
plot.ts(bby))

It was unsuccessful.这是不成功的。

I also tried我也试过

bby <- ts(data=data$Share_price, frequency=2,
     start=as.Date("2017-10-05"), end=as.Date("2019-10-04"))`

and then plot.ts(bby)然后plot.ts(bby)

but again it did not work.但它又没有用。 I always end up with the below graph:我总是得到下图:

在此处输入图像描述

Thanks for your help.谢谢你的帮助。

The ts setup is a bit clunky and its plots are not always the friendliest. ts 设置有点笨拙,而且它的情节并不总是最友好的。 You could set up your data as follows.您可以按如下方式设置数据。 Note that you need to specify the 'start' for the time series object below in a rather painful manner, and then (because it only accepts regular time intervals) you lose the day gap in the original series:请注意,您需要以一种相当痛苦的方式为下面的时间序列 object 指定“开始”,然后(因为它只接受固定的时间间隔)您失去了原始系列中的天间隔:

z <- seq.Date(as.Date('2017-10-05'), by = 1, length.out = 8)
data <- data.frame(Date = z[-(3:4)],
                   Share_price = c(1708.84, 1718.40, 1724.14, 1762.39, 1766.21, 1813.07))
myts <- ts(data$Share_price,
           start = c(2017, as.numeric(format(data$Date[1], "%j"))),
           frequency = 365)
plot(myts)

ts 对象的绘图

The x-axis now has decimal offsets from the year 2017. x 轴现在具有 2017 年的小数偏移量。

Perhaps the xts package is better here?也许 xts package 在这里更好? It can handle irregular time indices.它可以处理不规则的时间索引。

library(xts)
myxts <- xts(data$Share_price, data$Date)
plot(myxts)

在此处输入图像描述

See the plot.xts() documentation for all sorts of bells and whistles to embellish your graph.请参阅 plot.xts() 文档,了解用于美化您的图表的各种花里胡哨。

Normally ts class is used for monthly, quarterly or yearly but not daily data.通常 ts class 用于每月、每季度或每年的数据,但不用于每日数据。

Assuming that DF has Date and numeric columns as per reproducible form shown in the Note at the end, convert to zoo class.假设DF具有按照末尾注释中所示的可重现形式的日期和数字列,则转换为 zoo class。 Then we can easily plot using classic graphics, ggplot2 or lattice graphics.然后我们可以很容易地使用经典图形 plot,ggplot2 或点阵图形。

library(zoo)
z <- read.zoo(DF)

plot(z, main = "classic")

library(ggplot2)
autoplot(z) + 

library(lattice)
xyplot(z)

Plotting all three together using cowplot使用cowplot将所有三个一起绘制

library(gridGraphics)
library(cowplot)

plot(z)
p1 <- recordPlot()

library(ggplot2)
p2 <- autoplot(z)

library(lattice)
p3 <- xyplot(z)

plot_grid(p1, p2, p3, labels = c("classic", "ggplot2", "lattice"), nrow = 1)

giving:给予:

截屏

Note笔记

DF <- structure(list(Date = structure(c(17444L, 17445L, 17448L, 17449L, 
17450L, 17451L), class = "Date"), Share_price = c(1708.84, 1718.4, 
1724.14, 1762.39, 1766.21, 1813.07)), class = "data.frame", row.names = c(NA, 
-6L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM