Here is a minimal example of the type of data I'm strugling to plot:

These curves are drawn from two processes.

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)

I'd like to plot them as line plots in a lattice made of two boxes. The box located above would contain the first 150 curves (in orange) and the box below should display the next 200 curves (which should be in blue). I don't need a label or legend. I've tried to use the example shown on the man-page:

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)

but somehow it doesn't work.

有帮助吗?

解决方案 2

You should put your data in the long format like this:

      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

For example using 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)

enter image description here

其他提示

This will do without additional factors (yet agstudy's solution is not so much of a hack like this one):

# 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")

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top