在 R 中使用 Zoo 绘图时,在 x 轴上使用小时

发布于 2024-12-14 10:56:45 字数 1714 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤带着心事 2024-12-21 10:56:45

目前,您仅将 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")

在此处输入图像描述

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:

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

enter image description here

才能让你更想念 2024-12-21 10:56:45

看一下中间数据。 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)

Take a look at the intermediate data. m is a number, but you use it in the axis function like you expect it to be a string. Even if it were a string, it wouldn't work because at= 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:

# 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文