简体   繁体   English

绘制R中的时间序列

[英]Plotting time series in R

I'm new in R. I have a set of daily data in this link dropbox . 我是R的新手。我在这个链接dropbox中有一组每日数据。 My code is: 我的代码是:

#plotting CLOSE
plot.ts(datatest$CLOSE,ylab="Close")

And this is result: 这是结果:

[图] [2]。

Problem is the xlab , I would like to show DATE variable, but it seems to show the order of CLOSE variable. 问题是xlab ,我想显示DATE变量,但它似乎显示了CLOSE变量的顺序。 I also try out other commands like ggplot , xyplot etc. but they require converting to data.frame and it's so hard to me. 我还尝试了其他命令,如ggplotxyplot等,但它们需要转换为data.frame ,这对我来说太难了。

I would be grateful for some guidance on how to get DATE variable in the Graph. 关于如何在Graph中获取DATE变量的一些指导,我将不胜感激。

Thank you so much. 非常感谢。

试着用

dataset$DATE<-.as.POSIXct(dataset$DATE)

Something like this will do it: 像这样的东西会这样做:

# load data:
theData = read.csv2(file = "sample.csv",header = TRUE,
                    stringsAsFactors = FALSE,sep = ",",
                    colClasses = c("character",rep("numeric",5)),dec=".")

# We want to plot a custom x-axis, so stop the default
# x-axis being drawn usign xaxt="n":
plot(theData$CLOSE,type="l",xaxt="n")

# Lets say you want to put a date label in 8 different locations:
locations = floor(seq(from=1,to=nrow(theData),by=nrow(theData)/8))

# Now draw the x-axis on your plot like this:
axis(side = 1, at = locations, labels=theData$DATE[locations],las=2)

In the above, side=1 means drawing the axis at the bottom. 在上面, side=1表示在底部绘制轴。 at=locations means we want to show tick labels at the locations given in the locations vector we created earlier. at=locations表示我们希望在我们之前创建的位置向量中给出的位置显示刻度标签。 labels=theData$DATE[locations] provides the labels we want to place at the locations where we are putting a label. labels=theData$DATE[locations]提供我们想要放置标签的位置的标签。 las=2 means you want to rotate the tick labels. las=2表示您要旋转刻度标签。 Try las=1 as well for a different rotation. 尝试使用las=1进行不同的旋转。

However, those dates are kind of long, so you may want to create smaller dates like this: 但是,这些日期有点长,所以你可能想要创建这样的较小日期:

# Convert your long dates to smaller dates like YYYY-MM-DD, and stick
# the results to the end of your data frame.
theData = cbind(theData,
                "FormattedDate"=as.Date(theData$DATE,"%A, %B %e, %Y"))

# Replot and use your smaller dates. (ces.axis=0.8 makes
# the font smaller)
plot(theData$CLOSE,type="l",xaxt="n")
axis(side = 1, at = locations,
     labels=theData$FormattedDate[locations],las=1,cex.axis=0.8)

Finally, you can also use some nice time series packages to create much nicer plots easily: 最后,您还可以使用一些不错的时间序列包来轻松创建更好的图:

install.packages("xts")
library(xts)
xtsData = xts(theData[,"OPEN"],order.by = theData[,"FormattedDate"])
plot.zoo(xtsData)
# or
plot.xts(xtsData)

左边是动物园。右边是xts

   dataset$DATE <- as.Date(dataset$DATE, format = "%A, %B %d, %Y")
  • %A - long weekday %A - 工作日很长
  • %B - Full month name %B - 完整月份名称
  • %d - two digit date %d - 两位数的日期
  • %Y - four digit year %Y - 四位数年份

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

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