[英]Order of dates when plotting time series in R
I would like to know if the order of dates matter when plotting a time series in R. 我想知道在R中绘制时间序列时日期的顺序是否重要 。
For example, the dataframe below has it's date starting from the year 2010 onwards increasing as it goes down, for example till 2011: 例如,下面的数据框是从2010年开始的日期,随着它的下降而增加,例如到2011年:
Date Number of visits
2010-05-17 13
2010-05-18 11
2010-05-19 4
2010-05-20 2
2010-05-21 23
2010-05-22 26
2011-05-13 14
and below where the year are jumbled up. 在这一年混乱的地方下面。
Date Number of visits
2011-06-19 10
2009-04-25 5
2012-03-09 20
2011-01-04 45
Would i be able to plot a time series in R for the second example above? 我能在上面的第二个例子中用R绘制时间序列吗? Is it required that in order to plot a time series, the dates must be sorted?
是否需要为了绘制时间序列,必须对日期进行排序?
Assuming the data shown reproducibly int he Note at the end create an ordering vector o
and then plot the ordered data: 假设数据以可重复的方式显示在最后,注意最后创建一个排序向量
o
,然后绘制有序数据:
o <- order(dat$Date)
plot(dat[o, ], type = "o")
or convert the data to a zoo series, which will automatically order it, and then plot. 或者将数据转换为动物园系列,它将自动订购它,然后绘制。
library(zoo)
z <- read.zoo(dat)
plot(z, type = "o")
The data in reproducible form: 可重复形式的数据:
Lines <- "Date Number of visits
2010-05-17 13
2010-05-18 11
2010-05-19 4
2010-05-20 2
2010-05-21 23
2010-05-22 26
2011-05-13 14"
dat <- read.csv(text = gsub(" +", ",", readLines(textConnection(Lines))),
check.names = FALSE)
dat$Date <- as.Date(dat$Date)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.