1

这是我正在努力绘制的数据类型的一个最小示例:

这些曲线来自两个过程。

library("lattice")
x0<-matrix(NA,350,76)
for(i in 1:150)     x0[i,]<-arima.sim(list(order=c(1,0,0),ar=0.9),n=76)
for(i in 151:350)   x0[i,]<-arima.sim(list(order=c(1,0,0),ar=-0.9),n=76)

我想将它们绘制为由两个框组成的格子中的线图。位于上方的框将包含前 150 条曲线(橙色),下方的框应显示接下来的 200 条曲线(应为蓝色)。我不需要标签或图例。我尝试使用手册页上显示的示例:

aa<-t(x0)
colnames(aa)<-c(paste0("v0_",1:150),paste0("v1_",1:200))
aa<-as.ts(aa)
xyplot(aa,screens=list(v0_="0","1"),col=list(v0_="orange",v1_="blue"),auto.key=FALSE)

但不知何故它不起作用。

4

2 回答 2

2

这将在没有其他因素的情况下完成(但 agstudy 的解决方案并不是像这样的黑客):

# This is equivalent to your for-loops, use whatever you prefer
x0 <- do.call(rbind, lapply(1:350, function(i) {
  arima.sim(list(order=c(1,0,0), ar=ifelse(i <= 150, 0.9, -0.9)), n=76)
}))

plotStuff <- function(indices, ...) {
  plot.new()
  plot.window(xlim=c(1, ncol(x0)), ylim=range(x0[indices,]))
  box()
  for (i in indices)
    lines(x0[i,], ...)
}

par(mfrow=c(2,1), mar=rep(1,4)) # two rows, reduced margin
plotStuff(1:150,   col="orange")
plotStuff(151:350, col="blue")

在此处输入图像描述

于 2013-11-03T14:42:20.970 回答
1

你应该把你的数据放在这样的长格式中:

      Var1   Var2     value group
1        1   v0_1 2.0696016    v0
2        2   v0_1 1.3954414    v0
      ..... ..........
26599   75 v1_200 0.3488131    v1
26600   76 v1_200 0.2957114    v1

例如使用reshape2

library(reshape2)
aa.m <- melt(aa)
aa.m$group <- gsub('^(v[0-9])_(.*)','\\1',aa.m$Var2)
xyplot(value~Var1|group,data=aa.m,type='l',groups=group)

在此处输入图像描述

于 2013-11-03T14:35:49.580 回答