[R] Lattice fails to draw insets

Sebastian Weber sebastian.weber at physik.tu-darmstadt.de
Fri Oct 19 13:29:19 CEST 2007


Hi Deepayan!

For your info: I am using R 2.6.0 and lattice 0.17. Appended with this
email is a test case which aborts on my machine with the error message

Error in lattice.getStatus("current.cond.levels")[[current.row(),
current.column()]] : 
  subscript out of bounds

So, I hope to have helped you to pin down the error.

Ah, and while I am at it: Is there somewhere a documention about all
those spacings around a plot? I always go through the actual par-data
structures and try to guess which variable is responsible for what. But
a definite reference would be helpful.

Greetings,

Sebastian Weber

On Thu, 2007-10-18 at 14:01 -0700, Deepayan Sarkar wrote:
> On 10/18/07, Sebastian Weber <sebastian.weber at physik.tu-darmstadt.de> wrote:
> > Hello all lattice experts!
> >
> > I'm currently trying to plot inset figures in my lattice graphics. The
> > plot is composed out of two panels and in each panel I want to show an
> > inset figure which is a closeup of the data in each panel. Thus in each
> > panel I call xyplot again to plot the inset which I place in a grid
> > viewport. Nevertheless, once my first inset plot is drawn, lattice
> > aborts with an error message saying that some current.row() and
> > current.columns() function are not set any more. It looks to me that
> > lattice saves its current state while doing a plot in global variables
> > and this becomes messed up whenever I'm calling lattice while plotting
> > another plot as this resets those global variables.
> 
> Yes, I realized this during the freeze prior to R-2.6, and made some
> temporary fixes which are part of lattice 0.17-1 (uploaded a couple of
> days ago). This may or may not solve your problem. If not, please give
> a reproducible example, as it would help me put a more proper fix in
> place.
> 
> Note that in most situations, a whole new xyplot() is probably
> overkill; just set suitable xscale and yscale for your viewport, and
> call panel.xyplot().
> 
> -Deepayan
> 
> > Is there some good
> > workaround? My current solution is very crapy: I only setup the inset
> > viewports while doing the major plot and then insert the inset plots by
> > calling print(insetplot##, draw.in="insetvp##"). However there should be
> > a way to circumvent this very cumbersome way.
> >
> > Any ideas would be great. Thanks in advance.
> >
> > Greetings,
> >
> > Sebastian Weber
-------------- next part --------------

setInsetPar <- function() {
  ## get rid of some extra space ...
  par <- trellis.par.get()
  par$axis.components <- lapply(par$axis.components, function(p) { p[["pad2"]] <- 0.2; p[["pad1"]] <- 0.2; return(p) })
  par$layout.heights$axis.top <- 0
  par$layout.heights$axis.bottom <- 0.75
  par$layout.widths$axis.left <- 0.75
  par$layout.widths$axis.right <- 0
  trellis.par.set(par)
}

panel.inset <- function(inset,
                        drawRect=FALSE,
                        ixpos=unit(0, "mm"), iypos=unit(1, "npc"),
                        ijust=c("left","top"),
                        iwidth=unit(0.7, "npc"), iheight=unit(0.5, "npc")) {
  ## open our viewport ...
  vpName <- paste("insetvp", current.column(), current.row(), sep="/")
  pushViewport(viewport(x=ixpos, y=iypos, width=iwidth, height=iheight, just=ijust, name=vpname))
  
  if(drawRect)
    grid.rect(gp=gpar(lwd=1,col="black"))

  opar <- trellis.par.set()
  setInsetPar()

  ## ... and place the other plot inside there
  ##print(inset, draw.in="insetvp", prefix="insetPlot")
  print(inset, newpage=FALSE)
  upViewport()

  trellis.par.set(opar)

  grid.rect(gp=gpar())
}

## example code to demonstrate lattice flaws
x<-0:10;
y<-x^4;
pl1 <- xyplot(x ~y ,xaxs="i",yaxs="i")
dat <- rbind(data.frame(x=x, y=y, plot=1), data.frame(x=x, y=y, plot=2))
xyplot(y ~ x | factor(plot), data=dat, panel=function(...) { panel.xyplot(...); panel.inset(pl1) })



More information about the R-help mailing list