-1

I have several xyplot objects that I have saved as .RDATA files. I am now interested in being able to look at their differences. I have tried things like

plot1-plot2

but this does not work (I get the "non-numeric argument to binary operator error).

I would also be able to do this if I knew how to extract the timeseries data stored within the lattice xyplot object, but I have looked everywhere and can't figure out how to do this either.

Any suggestions?

EDIT: just to make it perfectly clear what I mean for MrFlick, by "taking the difference of two plots" I mean plotting the elementwise difference of the timeseries from each plot, assuming it exists (i.e. assuming that the plots have the same domain). Graphically, I might want to take the following two plots, stored as xyplot objects:

plot1 plot2

and end up with something that looks like this:

enter image description here

-Paul

Paul
  • 1,106
  • 1
  • 16
  • 39
  • If you examine the plot objects with `str()` you will probably see where the actually data being plotted it. – joran Jul 06 '15 at 18:18
  • I'm not sure I understand what it means to subtract plots. Can you make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired out to make it clear what you mean? – MrFlick Jul 06 '15 at 18:38
  • thanks I will check it out! that worked. the variables are stored in a list of lists called panel.args – Paul Jul 06 '15 at 19:28

1 Answers1

0

Here is a little function I wrote to plot the difference of two 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)
}
Paul
  • 1,106
  • 1
  • 16
  • 39