1

我在 R 中有以下代码:

z = read.zoo(filename, sep=',', header=T, index = 1:2, FUN=f)
plot(z[,1], col='red', lty=1, xaxt="n")

它生成了我的数据时间序列的线图,没有任何 x 轴。然后我想在几小时内添加一个带有标签的 x 轴,所以我尝试以下操作(我对 zoo FAQ 中示例的转换):

tt = time(z)
m = unique(hours(tt))
axis(side = 1, at = m, labels=substr(m, 1, 2))

但是没有轴出现。我在这里做错了什么?我尽可能地遵循这些示例,但将其从几个月改为几个小时。有任何想法吗?

如果有帮助,这里是输出的前几行z

                    AOT_500 Water.cm.
(04/27/10 06:53:26) 0.134940  1.403318
(04/27/10 07:01:26) 0.147529  1.425749
(04/27/10 07:07:15) 0.161837  1.411711
(04/27/10 07:12:05) 0.155312  1.415916
(04/27/10 07:21:36) 0.161960  1.438144
(04/27/10 07:34:29) 0.175276  1.426818
(04/27/10 07:50:08) 0.169452  1.435454
(04/27/10 08:09:39) 0.181987  1.437278
(04/27/10 08:50:58) 0.159755  1.372659
(04/27/10 09:04:12) 0.168336  1.348832
(04/27/10 09:10:04) 0.201690  1.383709

更新:根据要求 - 的输出dput(head(z,10))

structure(c(0.13494, 0.147529, 0.161837, 0.155312, 0.16196, 0.175276, 
0.169452, 0.181987, 0.159755, 0.168336, 1.403318, 1.425749, 1.411711, 
1.415916, 1.438144, 1.426818, 1.435454, 1.437278, 1.372659, 1.348832
), .Dim = c(10L, 2L), .Dimnames = list(NULL, c("AOT_500", "Water.cm."
)), index = structure(c(14726.2871064815, 14726.292662037, 14726.2967013889, 
14726.3000578704, 14726.3066666667, 14726.3156134259, 14726.3264814815, 
14726.3400347222, 14726.3687268519, 14726.3779166667), format = structure(c("m/d/y", 
"h:m:s"), .Names = c("dates", "times")), origin = structure(c(1, 
1, 1970), .Names = c("month", "day", "year")), class = c("chron", 
"dates", "times")), class = "zoo")
4

2 回答 2

3

目前,您仅将 y 值提供给绘图方法。我发现时间计算相当混乱,并决定改用不带轴的绘图方法。只需从时间中减去截断的时间即可得到小时数,plot.zoo 会为您很好地计算出来:

require(xts); require(chron)
mm <- times(tt)
plot((mm-trunc(mm) ), z[,1], col='red', lty=1, type="b", xlab="Time of Day")

在此处输入图像描述

于 2011-11-08T18:35:48.100 回答
2

看看中间数据。 m是一个数字,但你在axis函数中使用它就像你期望它是一个字符串一样。即使它是一个字符串,它也不起作用,因为它at=需要与您正在绘制的对象的索引(在本例中为 chron)的索引相同。

下面的代码做了我认为你想做的事情:

# find the hourly sections of the chron index
m <- trunc((tt-trunc(tt))*24)/(24)
# find the minimum index value for each hour
l <- tapply(tt, m, min)
# plot the series
plot(z[,1], col='red', lty=1, xaxt="n", xlab="")
# add the axis
axis(side=1, at=l, labels=unique(m), las=2)
于 2011-11-08T18:25:56.903 回答