簡體   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