如何在晶格中给定条件下绘制叠加多个时间序列?
假设我有一个数据框 df,如下所示:
f t1 t2 t3
h 1 3 4
h 2 4 3
t 3 4 5
t 5 6 8
f 是一个因子,$t 属性是与时间排序事件相关的数值。 我可以使用 par(new=T) 覆盖时间序列 t1 到 t3 并手动按因子隔离。 但我想知道是否有某种方法可以用格子来做到这一点,其中重叠的时间序列 受因素制约。因此,我们将有两个面板,其中重叠的时间序列对应于条件因子 f。我见过的大多数例子每个因素只使用一个时间序列(向量)。我也考虑过使用平行图,但时间信息丢失了。 我也尝试过类似的方法
xyplot(df$t1+df$t2+df$t3 ~seq(3) | factor(df$f))
,但它丢失了行序列连接。有人知道这是否可能吗?
这是使用非格方法的非常粗略的说明。
x<-matrix(seq(12),4,3)
f<-c('a','a','b','b')
df<-data.frame(f,x)
layout(1:2); yr<-c(0,12); xr<-c(1,3);
plot(as.numeric(df[1,2:4])~seq(3),type='o',ylim=yr,xlim=xr,ylab='A')
par(new=T)
plot(as.numeric(df[2,2:4])~seq(3),type='o',ylim=yr,xlim=xr,ylab='A')
plot(as.numeric(df[3,2:4])~seq(3), type='o',ylim=yr,xlim=xr,ylab='B')
par(new=T)
plot(as.numeric(df[4,2:4])~seq(3),type='o',ylim=yr,xlim=xr,ylab='B')
Suppose I have a data frame, df, that looks like:
f t1 t2 t3
h 1 3 4
h 2 4 3
t 3 4 5
t 5 6 8
with f being a factor and $t attributes being numerical values related to time ordered events.
I could overlay time series t1 to t3 using par(new=T) and isolate by factor manually.
But I wonder if there is some way to do this with lattice, where the overlaid time series
are conditioned by the factor. So we would have two panels, with overlaid time series corresponding to conditional factors, f. Most examples I've seen only use one time series (vector) per factor. I also thought about using a parallel plot, but time information is lost.
I've also tried something like
xyplot(df$t1+df$t2+df$t3 ~seq(3) | factor(df$f))
, but it loses row sequence connections. Anyone know if this is possible?
Here's a very crude illustration using non lattice approach.
x<-matrix(seq(12),4,3)
f<-c('a','a','b','b')
df<-data.frame(f,x)
layout(1:2); yr<-c(0,12); xr<-c(1,3);
plot(as.numeric(df[1,2:4])~seq(3),type='o',ylim=yr,xlim=xr,ylab='A')
par(new=T)
plot(as.numeric(df[2,2:4])~seq(3),type='o',ylim=yr,xlim=xr,ylab='A')
plot(as.numeric(df[3,2:4])~seq(3), type='o',ylim=yr,xlim=xr,ylab='B')
par(new=T)
plot(as.numeric(df[4,2:4])~seq(3),type='o',ylim=yr,xlim=xr,ylab='B')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我添加了一个 ID 变量并与 package:reshape2 融为一体,
因为您要求将其“覆盖”,所以我使用组参数来保持 ID 的独立和“|”运算符为您提供“h”和“t”的两个面板:
I added an ID variable and melted with package:reshape2
Since you asked to have it "overlayed" I used the group parameter to keep the ID's separate and the "|" operator to give you the two panels for "h" and "t":
(1) 这可以使用 xyplot.zoo 紧凑地完成。第一个语句将数据框转换为动物园系列(系列存储在动物园对象的列中),第二个语句对其进行绘制,以便屏幕参数定义每个系列显示在哪个面板中:
(2)或者如果需要在 X 轴上显示 df 的列名称,然后将 z 定义如下(然后发出上面的 xyplot 命令)
: >xyplot 使用第一个点中的
z
看起来像这样(第二个点除了 X 轴标签之外是相同的):编辑:简化 (2)
(1) This can be done compactly using
xyplot.zoo
. The first statement converts the data frame to a zoo series (series are stored in columns in zoo objects) and the second statement plots it such that the screen argument defines which panel each series is shown in:(2) or if it were desired to show
df
's column names on the X axis instead then definez
as the following (and then issue thexyplot
command above):xyplot
using thez
in the first point looks like this (and the second is the same except for the X axis labels):EDIT: simplified (2)