【问题标题】:Plotting Time-series with POSIXct Date and Time Labels on x-axis在 x 轴上绘制带有 POSIXct 日期和时间标签的时间序列
【发布时间】:2014-06-30 15:54:52
【问题描述】:

我有以下格式的数据:

my.XYZ$datetime

头部(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 轴不正确。

有人知道如何处理吗?

谢谢。

朱莉娅

【问题讨论】:

    标签: r plot time-series axis-labels posixct


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-06-18
      • 1970-01-01
      • 2014-06-02
      • 2021-10-14
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 2015-08-13
      • 2021-05-18
      相关资源
      最近更新 更多