在 R 中使用 Zoo 绘图时,在 x 轴上使用小时
我在 R 中有以下代码:
z = read.zoo(filename, sep=',', header=T, index = 1:2, FUN=f)
plot(z[,1], col='red', lty=1, xaxt="n")
它生成数据时间序列的线图,没有任何 x 轴。然后我想添加一个带有小时标签的 x 轴,所以我尝试以下操作(我对动物园常见问题解答中的示例进行转换):
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")
I have the following code in R:
z = read.zoo(filename, sep=',', header=T, index = 1:2, FUN=f)
plot(z[,1], col='red', lty=1, xaxt="n")
Which produces a line plot of the time series of my data, without any x-axis. I want to then add an x-axis with labels in hours, so I try the following (my conversion of the examples in the zoo FAQ):
tt = time(z)
m = unique(hours(tt))
axis(side = 1, at = m, labels=substr(m, 1, 2))
But no axis appears. What am I doing wrong here? I'm following the examples as closely as possible, but changing it from getting months to getting hours. Any ideas?
In case it helps, here are the first few lines of the output of 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
Update: As requested - the output of 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")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,您仅将 y 值赋予绘图方法。我发现时间计算相当混乱,因此决定使用没有轴的绘图方法。只需从给出的时间中减去截断的时间即可得到小时,plot.zoo 就会很好地为您计算出来:
At the moment you are only giving the y values to the plot-method. I found the time calculations to be quite a mess and decided to instead use the plot method without the axis. Just subtract the truncated time from the time to give hours and plot.zoo figures it out nicely for you:
看一下中间数据。
m
是一个数字,但您在 axis 函数中使用它,就像您期望它是一个字符串一样。即使它是一个字符串,它也不起作用,因为at=
需要与您正在绘制的对象的索引(在本例中为 chron)属于同一类。下面的代码做了我认为你想做的事情:
Take a look at the intermediate data.
m
is a number, but you use it in theaxis
function like you expect it to be a string. Even if it were a string, it wouldn't work becauseat=
needs to be the same class as the index of the object you're plotting (chron in this case).The code below does what I think you were trying to do: