Skip to content
Prev 153416 / 398500 Next

Lattice: problem using panel.superpose and panel.groups

Dieter:

Thank you for your response.  As you requested, I created a self- 
running example, pasted below.  It may be a little wordier than I  
would like, but it runs.
The data I am creating assumes that there are two datasets ("calib"  
and "hold"), with actual values actual.calib and actual.hold.  I then  
simulate data for three models, A, B and C.  I want to plot a 3 x 2  
lattice that has bwplots for each model-dataset combination in each  
panel.  On top of that, I want to superimpose a line that plots the  
actual data for calib in all three panels in the calib column (and the  
same for hold).

The data.frame sim.data is just the simulated data, and actual.data is  
just the actual data.  all.data combines them both, and the group  
variable identifies whether a record is from "sim" or "actual."  Note  
that there are 50 simulations, but only one actual, for each model- 
dataset combination.  The presentation in all.data is kind of  
wasteful, since I replicated the same actual data for all 3 models.   
So, ideally, I would work with sim.data and actual.data separately.   
But I can use all.data if necessary.

I apologize that I was not more claear in my earlier post, but  
hopefully this gives you enough information.  Thanks again for helping  
with this.

Michael



library(lattice)

# creating datasets


models <- c("A","B","C")
datasets <- c("calib","hold")
counts <- 1:10
n.sims <- 50
sim.data <- NULL
all.data <- NULL
actual.calib <- seq(0,5,length=10)
actual.hold <- seq(5,0,length=10)
cols <- c("model","dataset","count","value","group")

actual.data <- as.data.frame(cbind(c(rep("calib",10),rep("hold",10)),
                                     
c(1:10,1:10),c(actual.calib,actual.hold)))

set.names <- c(rep("calib",n.sims),rep("hold",n.sims))

for (mod in models) {
   for (y in counts) {

     x.calib <- rnorm(n=n.sims,mean=actual.calib[y],sd=1)
     x.hold <- rnorm(n=n.sims,mean=actual.hold[y],sd=1)

     x <- as.data.frame(cbind(rep(mod,2*n.sims), set.names, y,
                              c(x.calib,x.hold),rep("sim",2*n.sims)))

     colnames(x) <- cols
     sim.data <- rbind(sim.data, x)
   }

   rep.actual <- as.data.frame(cbind(rep(mod,20),
                                     actual.data,rep("actual",20)))
   colnames(rep.actual) <- cols
   all.data <- rbind(all.data,sim.data,rep.actual)
}

colnames(actual.data) <- c("dataset","count","value")

#  for some reason, value is encoded as a factor.
#  This changes value to numeric.
#  (is there a better way to do this?)

sim.data[,"value"] <- as.numeric(levels(sim.data[,"value"]) 
[sim.data[,"value"]])
all.data[,"value"] <- as.numeric(levels(all.data[,"value"]) 
[all.data[,"value"]])
actual.data[,"value"] <- as.numeric(levels(actual.data[,"value"]) 
[actual.data[,"value"]])


# at this point, there are three data frames worth considering:
# sim.data - simulated data
# actual.data - actual data
# all.data - sim.data, and the actual.data replicated for each model


# create panel function

panel.ppc.plot <- function(...,group.number) {

   if (group.number==1) {
     panel.bwplot(...)
   } else {

     panel.lines(...)
   }
}

# create and plot lattice object

obj <- bwplot(as.numeric(value) ~ as.factor(count) | dataset + model,
               data = all.data,
               panel = panel.superpose,
               groups=group,
               panel.groups = panel.ppc.plot
               )

plot(obj)