如何在晶格中给定条件下绘制叠加多个时间序列?

发布于 2025-01-03 00:58:51 字数 889 浏览 3 评论 0原文

假设我有一个数据框 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 技术交流群。

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

发布评论

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

评论(2

月下伊人醉 2025-01-10 00:58:51

我添加了一个 ID 变量并与 package:reshape2 融为一体,

 dat
  f t1 t2 t3 ID
1 h  1  3  4  1
2 h  2  4  3  2
3 t  3  4  5  3
4 t  5  6  8  4
datm <- melt(dat, id.vars=c("ID","f"), measure.vars=c("t1", "t2", "t3"))
> datm
   ID f variable value
1   1 h       t1     1
2   2 h       t1     2
3   3 t       t1     3
4   4 t       t1     5
5   1 h       t2     3
6   2 h       t2     4
7   3 t       t2     4
8   4 t       t2     6
9   1 h       t3     4
10  2 h       t3     3
11  3 t       t3     5
12  4 t       t3     8

因为您要求将其“覆盖”,所以我使用组参数来保持 ID 的独立和“|”运算符为您提供“h”和“t”的两个面板:

xyplot(value~variable|f, group=ID, data=datm, type="b")

在此处输入图像描述

I added an ID variable and melted with package:reshape2

 dat
  f t1 t2 t3 ID
1 h  1  3  4  1
2 h  2  4  3  2
3 t  3  4  5  3
4 t  5  6  8  4
datm <- melt(dat, id.vars=c("ID","f"), measure.vars=c("t1", "t2", "t3"))
> datm
   ID f variable value
1   1 h       t1     1
2   2 h       t1     2
3   3 t       t1     3
4   4 t       t1     5
5   1 h       t2     3
6   2 h       t2     4
7   3 t       t2     4
8   4 t       t2     6
9   1 h       t3     4
10  2 h       t3     3
11  3 t       t3     5
12  4 t       t3     8

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":

xyplot(value~variable|f, group=ID, data=datm, type="b")

enter image description here

2025-01-10 00:58:51

(1) 这可以使用 xyplot.zoo 紧凑地完成。第一个语句将数据框转换为动物园系列(系列存储在动物园对象的列中),第二个语句对其进行绘制,以便屏幕参数定义每个系列显示在哪个面板中:

library(zoo)
library(lattice)

z <- zoo(t(df[-1]))

xyplot(z, screen = df$f, type = "o")

(2)或者如果需要在 X 轴上显示 df 的列名称,然后将 z 定义如下(然后发出上面的 xyplot 命令)

z <- zoo(t(df[-1])), factor(names(df[-1])))

: >xyplot 使用第一个点中的 z 看起来像这样(第二个点除了 X 轴标签之外是相同的):

xyplot.zoo 输出

编辑:简化 (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:

library(zoo)
library(lattice)

z <- zoo(t(df[-1]))

xyplot(z, screen = df$f, type = "o")

(2) or if it were desired to show df's column names on the X axis instead then define z as the following (and then issue the xyplot command above):

z <- zoo(t(df[-1])), factor(names(df[-1])))

xyplot using the z in the first point looks like this (and the second is the same except for the X axis labels):

xyplot.zoo output

EDIT: simplified (2)

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