繁体   English   中英

在x轴上用POSIXct日期和时间标签绘制时间序列

[英]Plotting Time-series with POSIXct Date and Time Labels on x-axis

我有以下格式的数据:

my.XYZ $ datetime <-as.POSIXct(myXYZ $ datetime,format =“%Y-%m-%d%H:%M:%S”)

头(my.XYZ)

units            datetime
1     1 0012-08-03 07:49:02
2     1 0012-08-05 12:27:21
3     1 0012-08-17 06:55:07
4     1 0012-08-20 10:56:49
5     1 0012-08-08 10:14:36
6     1 0012-08-20 16:12:26

我想创建一个时序图,其中x轴表示时间和日期,y轴表示单位。 这是我到目前为止所做的:

> # make it a zoo object
> zoo.XYZ<- zoo(my.XYZ)

> # make it a ts
> ts.XYZ<- ts(zoo.XYZ)

> #plotting the ts object
> plot(ts.XYZ, plot.type="single")

>Warning messages:
> 1: In xy.coords(x = matrix(rep.int(tx, k), ncol = k), y = x, log = log) :
> NAs introduced by coercion
> 2: In xy.coords(x, y) : NAs introduced by coercion

在这里,我确实得到了正确的图形,但是我的x轴不是由日期而是由变量的数字给出(变量1、2、3 ... 430)

我还尝试了以下方法:

># plot as plot.ts
> plot.ts(ts.XYZ)

>Error in plot.window(...) : need finite 'ylim' values
>In addition: Warning messages:
>1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
>2: In min(x) : no non-missing arguments to min; returning Inf
>3: In max(x) : no non-missing arguments to max; returning -Inf

> plot.ts(ts.XYZ, xaxt = "n")

>Error in plot.window(...) : need finite 'ylim' values
>In addition: Warning messages:
>1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
>2: In min(x) : no non-missing arguments to min; returning Inf
>3: In max(x) : no non-missing arguments to max; returning -Inf

> axis(1, my.XYZ$datetime, format(my.XYZ$datetime, "%Y-%m-%d %H:%M:%S"), cex.axis = .7)

同样,该图是正确的,但x轴却不正确。

有人知道如何处理吗?

谢谢。

朱利亚

创建zoo()对象时,需要使用order.by=参数指定哪一列具有日期/时间值。 尝试

zoo.XYZ <- zoo(my.XYZ[,-2], order.by=my.XYZ[,2])

此外,看起来您的格式字符串可能不正确。 我怀疑您的观察发生在公元12年。 如果只有两位数字年份将这些值解释为2012,则将字符串更改为%y-%m-%d %H:%M:%S"

暂无
暂无

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

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