如何使用 scatterplot/xyplot 和 POSIX 时间对象创建多面板图?

发布于 2024-09-06 16:18:02 字数 1277 浏览 4 评论 0原文

一个新手问题。 (添加新信息)

我有一组随机收集的带时间戳的数据。我喜欢创建绘图矩阵,但无法使用散点图或 xyplot & 进行创建。时间对象。

我的数据

dataset$Time  #POSIX time objects (no set sampling period)
              #i built POSIX time objects by dataset$Time<-strptime(tt, "%H:%M:%OS")
              #the origial string was formated like this 12:12:12.234 (HH:MM:SS:msec)
dataset$d1, dataset$d2 #integers
dataset$d3 #factor with 10 levels

我可以做到这些

plot( dataset$Time, dataset$d1)
scatterplot(dataset$d1 ~ dataset$d2 | dataset$d3, data=dataset)
xyplot(dataset$d1 ~ dataset$d2 | dataset$d3, data=dataset)

但是,我不能做到这些(x 轴中的 POSIX 时间对象)

scatterplot(dataset$d1 ~ dataset$Time | dataset$d3, data=dataset)
xyplot(dataset$d1 ~ dataset$Time | dataset$d3, data=dataset)

新信息

结构错误(.Internal(as.POSIXct(x, tz)), class = c("POSIXt", "POSIXct"),:无效的“x”参数。

新信息)但是这个 与(y 轴中的 POSIX 时间对象)

xyplot(dataset$Time ~ dataset$d1 | dataset$d3, data=dataset)

相关,但不同的问题是 hexbin。当时间对象添加到 hexbin 时,hexbin 中的图在单位上不显示正确的时间格式,

bin<-hexbin(dataset$Time, dataset$d1) 
plot(bin))

该怎么办?

我 它!!

A newbie question. (ADDED NEW INFO)

I have a set of time stamped data that were collected randomly. I like to create a matrix of plots, but I could not create using either scatterplot or xyplot & time objects.

my data

dataset$Time  #POSIX time objects (no set sampling period)
              #i built POSIX time objects by dataset$Time<-strptime(tt, "%H:%M:%OS")
              #the origial string was formated like this 12:12:12.234 (HH:MM:SS:msec)
dataset$d1, dataset$d2 #integers
dataset$d3 #factor with 10 levels

.

i can do these

plot( dataset$Time, dataset$d1)
scatterplot(dataset$d1 ~ dataset$d2 | dataset$d3, data=dataset)
xyplot(dataset$d1 ~ dataset$d2 | dataset$d3, data=dataset)

However, i can NOT do these (POSIX time object in x-axis)

scatterplot(dataset$d1 ~ dataset$Time | dataset$d3, data=dataset)
xyplot(dataset$d1 ~ dataset$Time | dataset$d3, data=dataset)

(NEW INFO)

Error in structure(.Internal(as.POSIXct(x, tz)), class = c("POSIXt", "POSIXct"), : invalid 'x' argument.

(NEW INFO) but this works (POSIX time object in y-axis)

xyplot(dataset$Time ~ dataset$d1 | dataset$d3, data=dataset)

related, but different question is hexbin. When time objects are added to hexbin, the plot from the hexbin does not show correct time format on the units.

bin<-hexbin(dataset$Time, dataset$d1) 
plot(bin))

What should I do?

Thanks for looking into it!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

拧巴小姐 2024-09-13 16:18:02

这是一个工作示例。您可能弄错了数据格式——需要注意精确的数据格式。

首先,一个简单的数据框:

R> X <- data.frame(pt=Sys.Date()+0:4, x1=100+cumsum(rnorm(5)),
+                                     x2=90+cumsum(rt(5,4)))
R> X
          pt     x1    x2
1 2010-06-22  98.73 90.33
2 2010-06-23  99.43 89.56
3 2010-06-24  98.85 86.95
4 2010-06-25  99.08 88.52
5 2010-06-26 100.30 94.08
R> 

这是所谓的形式,格子不使用。您需要将其转换为格式。我在这里使用 stack() ,您也可以使用 重塑
包:

R> Y <- data.frame(pt=rep(X$pt,2), stack(X, select=c(x1,x2)))
R> Y
           pt values ind
1  2010-06-22  98.73  x1
2  2010-06-23  99.43  x1
3  2010-06-24  98.85  x1
4  2010-06-25  99.08  x1
5  2010-06-26 100.30  x1
6  2010-06-22  90.33  x2
7  2010-06-23  89.56  x2
8  2010-06-24  86.95  x2
9  2010-06-25  88.52  x2
10 2010-06-26  94.08  x2
R> 

现在 xyplot 调用很简单:

R> xyplot(values ~ pt | ind, data=Y, panel=panel.lines)

您当然可以使用更复杂的条件表达式。

Here is a working example. You may have gotten your data formats wrong -- one needs to be careful about the precise data formats.

First, a simple data frame:

R> X <- data.frame(pt=Sys.Date()+0:4, x1=100+cumsum(rnorm(5)),
+                                     x2=90+cumsum(rt(5,4)))
R> X
          pt     x1    x2
1 2010-06-22  98.73 90.33
2 2010-06-23  99.43 89.56
3 2010-06-24  98.85 86.95
4 2010-06-25  99.08 88.52
5 2010-06-26 100.30 94.08
R> 

This is so-called wide form which lattice does not use. You need to transform it into long format. I use stack() here, you can also use cast() and melt() from the reshape
package:

R> Y <- data.frame(pt=rep(X$pt,2), stack(X, select=c(x1,x2)))
R> Y
           pt values ind
1  2010-06-22  98.73  x1
2  2010-06-23  99.43  x1
3  2010-06-24  98.85  x1
4  2010-06-25  99.08  x1
5  2010-06-26 100.30  x1
6  2010-06-22  90.33  x2
7  2010-06-23  89.56  x2
8  2010-06-24  86.95  x2
9  2010-06-25  88.52  x2
10 2010-06-26  94.08  x2
R> 

Now the xyplot call is simply:

R> xyplot(values ~ pt | ind, data=Y, panel=panel.lines)

and you can of course use much more complicated conditioning expressions.

自由如风 2024-09-13 16:18:02

显然 R 在处理 POSIX 时代的成瘾问题时遇到了一些问题......它给了我错误:

+.POSIXt(x[floor(d)], x[ceiling(d)]) 中的错误:未为“POSIXt”对象定义二进制“+”

只需将它们转换为数字,它应该工作。您可以隐藏相应的轴并稍后使用正确的日期重新绘制

,例如:

scatterplot(dataset$d1 ~ as.numeric(dataset$Time) | dataset$d3, data=dataset, xaxt="n")
axis(1, at=as.numeric(dataset$Time), labels=dataset$Time, cex.axis=0.5)

Apparently R has some problem dealing with addiction of POSIX times... it gives me the error:

Error in +.POSIXt(x[floor(d)], x[ceiling(d)]) : binary '+' is not defined for "POSIXt" objects

just cast them to numeric and it should work. You can hide the corresponding axis and redraw it later with the correct dates

E.g.:

scatterplot(dataset$d1 ~ as.numeric(dataset$Time) | dataset$d3, data=dataset, xaxt="n")
axis(1, at=as.numeric(dataset$Time), labels=dataset$Time, cex.axis=0.5)
国产ˉ祖宗 2024-09-13 16:18:02

对我来说它只是有效,所以你可能有一个格式错误的时间向量。当你调用class(dataset$Time)时你会得到什么?它应该包含“POSIXct”才能正常工作。

另一方面,如果您提供 data=dataset,则无需将 dataset$ 放入公式中。

For me it just works, so you probably has a time vector in a bad format. What do you get when you call class(dataset$Time)? It should contain "POSIXct" for this to work.

On the other hand, you don't need to put dataset$ in formula if you supply data=dataset.

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