Search code examples
graphicsrlattice

Lattice problems: lattice objects coming from JAGS, but device can't be set


I ran JAGS with runjags in R and I got a giant list back (named results for this example).

Whenever I access results$density, two lattice plots (one for each parameter) pop up in the default quartz device.

I need to combine these with par(mfrow=c(2, 1)) or with a similar approach, and send them to the pdf device.

Nothing I tried is working. Any ideas?

I've tried dev.print, pdf() with dev.off(), etc. with no luck.


Solution

  • Here's a way to ditch the "V1" panels by manipulation of the Trellis structure:

    p1 <- results$density$c
    p2 <- results$density$m
    
    p1$layout <- c(1,1)
    p1$index.cond[[1]] <- 1   # remove second index
    p1$condlevels[[1]] <- "c"   # remove "V1"
    class(p1) <- "trellis"   # overwrite class "plotindpages"
    
    p2$layout <- c(1,1)
    p2$index.cond[[1]] <- 1   # remove second index
    p2$condlevels[[1]] <- "m"   # remove "V1"
    class(p2) <- "trellis"   # overwrite class "plotindpages"
    
    library(grid)
    layout <- grid.layout(2, 1, heights=unit(c(1, 1), c("null", "null")))
    grid.newpage()
    pushViewport(viewport(layout=layout))
    pushViewport(viewport(layout.pos.row=1))
    print(p1, newpage=FALSE)
    popViewport()
    pushViewport(viewport(layout.pos.row=2))
    print(p2, newpage=FALSE)
    popViewport()
    popViewport()
    

    pot of c.trellis() result http://img142.imageshack.us/img142/3272/ctrellisa.png