r - 取两个xyplots的差异?

时间:2015-07-06 18:09:55

标签: r lattice

我有几个我保存为.RDATA文件的xyplot对象。我现在有兴趣看看他们的分歧。我尝试过像

这样的事情
plot1-plot2

但这不起作用(我得到“二元运算符错误的非数字参数”。

如果我知道如何提取存储在格子xyplot对象中的时间序列数据,我也能做到这一点,但我已经到处查看,也无法弄清楚如何做到这一点。

有什么建议吗?

编辑: 只是为了明确我对MrFlick的意思,通过“取两个图的差异”我的意思是绘制每个图的时间序列的元素差异,假设它存在(即假设图具有相同的域)。从图形上看, 我可能想要采用以下两个图,存储为xyplot对象:

plot1 plot2

并最终得到如下内容:

enter image description here

-Paul

1 个答案:

答案 0 :(得分:0)

这是我写的一个小函数,用于绘制两个xyplots的区别:

getDifferencePlot = function(plot1,plot2){
  data1 = plot1$panel.args
  data2 = plot2$panel.args
  len1 = length(data1)
  len2 = length(data2)
  if (len1!=len2)
    stop("plots do not have the same number of panels -- cannot take   difference")
  if (len1>1){
  plotData = data.table(matrix(0,0,4))
  setNames(plotData,c("x","y1","y2","segment"))

  for (i in 1:len1){
    thing1 = data.table(cbind(data1[[i]]$x,data1[[i]]$y))
    thing2 = data.table(cbind(data2[[i]]$x,data2[[i]]$y))
    finalThing = merge(thing1, thing2,by = "V1")
    segment = rep(i,nrow(finalThing))
    finalThing = cbind(finalThing,segment)
    setNames(finalThing,c("x","y1","y2","segment"))

    plotData = rbind(plotData,finalThing)
  }

  }
  if (len1==1){
    plotData = data.table(matrix(0,0,3))
    setNames(plotData,c("x","y1","y2"))


      thing1 = data.table(cbind(data1[[i]]$x,data1[[i]]$y))
      thing2 = data.table(cbind(data2[[i]]$x,data2[[i]]$y))
      plotData = merge(thing1, thing2,by = "V1")



  }
  plotData$difference = plotData$y1-plotData$y2
  if (len1==1)
  diffPlot = xyplot(difference~x,plotData,type = "l",auto.key = T)
  if (len1>1)
  diffPlot = xyplot(difference~x|segment,plotData,type = "l",auto.key = T)
  return(diffPlot)
}