简体   繁体   English

R-为时间序列绘制法线X轴

[英]R - plot normal X axis for time series

I have a time series data in an hourly format. 我有一个小时格式的时间序列数据。

Time    traffic
6/7/2005 7:00   56718587433
6/7/2005 8:00   76456162968
6/7/2005 9:00   82534038485
6/7/2005 10:00  88796995092

 ...

7/28/2005 10:00 51528036132
7/28/2005 11:00 69610584123
7/28/2005 12:00 76364975533
7/28/2005 13:00 81281257078

To plot this time series what I do is the following: 要绘制此时间序列,我需要执行以下操作:

data<-read.csv("my_file.csv")
data<-ts(data[,2],frequency = 24*365, start=c(2005,6,7,7))
plot(data, xlab="Date", ylab = "Value")

And this is the plot I'm getting: 这是我得到的情节: 时间序列图

My question is how can I get normal x- axis values in my figure? 我的问题是如何在图中获得正常的x轴值? (ie with 6-2005 7-2005 or even in a daily format like 7-6-2005 10-6-2005 etc.) (即使用6-2005 7-2005或什至以每日格式(例如7-6-2005 10-6-2005等))

You can try with xts directly on your original dataframe instead of using ts (the following figure shows how it looks on a randomly generated data): 您可以直接在原始数据帧上尝试使用xts而不是使用ts(下图显示了它在随机生成的数据上的外观):

data <- data.frame(Time=seq(strptime('6/7/2005 7:00', '%m/%d/%Y %H:%M'), strptime('6/7/2006 7:00', '%m/%d/%Y %H:%M'), 3600), traffic=rnorm(8761, 56718587433, 100000))

head(data)
        Time     traffic
1 2005-06-07 07:00:00 56718605033
2 2005-06-07 08:00:00 56718675864
3 2005-06-07 09:00:00 56718662521
4 2005-06-07 10:00:00 56718525435
5 2005-06-07 11:00:00 56718604095
6 2005-06-07 12:00:00 56718411539

class(data$Time)
[1] "POSIXct" "POSIXt" 

library(xts)
data <- xts(data$traffic, as.POSIXct(data$Time, format="%m%d%Y %H:%M"))
plot(data, major.format = "%m-%d-%Y", xlab="Date", ylab = "Value")

在此处输入图片说明

Perhaps the date format is numeric? 也许日期格式是数字? check typeof(Time). 检查typeof(Time)。 Perhaps you have to transform it: 也许您必须对其进行转换:

 as.date(x, format = "%m-%d-%Y")

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

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