# [R] superimposing different plot types in lattice panel.superpose

genghis jcpayne at uw.edu
Mon Jul 4 03:52:02 CEST 2011

```I would like to plot 3 best-fit models in a single panel of a lattice plot,
superimposed on 3 corresponding datasets in the same panel.  My goal is to
show the models as lines of 3 different colors, and the data as points whose
colors correspond to the model colors.  In essence, I have two levels of
grouping: 1) model vs. data, and 2) model number.  Since there is only one
“groups” variable, I have tried to deal with the additional grouping level
by subsetting the data inside a custom panel function (basically a hack),
but something about the way parameters are passed in panel.superpose (I
think) is making it hard to show both points and lines.

My question is very similar to a previous post:
, but the questioner in that case was using bwplot, which automatically
makes a separate plot for every level of the categorical variable, so they
didn’t face the two-level grouping problem, and I have been unable to figure
out how to adapt their answer.  Another approach I tried was to put my model
function inside of my custom panel function so that the analysis occurred
there, but I couldn’t get it to subset the x and y data appropriately.

In the toy problem below I want to plot each inverted V (“model”) as LINES,
with a single POINT (“data”) in the center of each inverted V, the same
color as the inverted V.  The code runs, but I can’t seem to mix lines and
points.  In my real problem (stable isotopes with ellipses superimposed on
data) there will be additional panels but I am creating only one panel here,
for simplicity.

#generate test "model results"
x<-1:9
y<-rep(c(1,3,1),3)
model<-c(rep("a",3),rep("b",3),rep("c",3))
modelresults<-data.frame(x,y,model)

#generate test "data"
x<-c(2,5,8)
y<-rep(1.5,3)
model<-c("a","b","c")
data<-data.frame(x,y,model)

#combine them into one data set
combined<-make.groups(modelresults,data)

#custom panel function
panel.dualplot <- function(...) {
panel.xyplot(x[which="modelresults"],y[which="modelresults"],...)
panel.points(x[which="data"],y[which="data"],...)
}

#main call to xyplot
xyplot(y ~ x, data=combined,
type = "l",
panel = panel.superpose,
groups = model,
panel.groups = panel.dualplot,
)

I’d be very grateful for any suggestions.

Thanks!

John

--
View this message in context: http://r.789695.n4.nabble.com/superimposing-different-plot-types-in-lattice-panel-superpose-tp3642808p3642808.html
Sent from the R help mailing list archive at Nabble.com.

```