views:

176

answers:

3

Hi All,

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.

Vince

+1  A: 
rcs
This works - but I don't want the V1 and V2 plots - they are redundant. How can I get rid of these?
Vince
+1  A: 

The easiest way to combine the plots is to use the results stored in results$mcmc:

# prepare data, see source code of "run.jags"
thinned.mcmc <- combine.mcmc(list(results$mcmc),
                             collapse.chains=FALSE,
                             return.samples=1000)
print(densityplot(thinned.mcmc[,c(1,2)], layout=c(1,2),
                  ylab="Density", xlab="Value"))
rcs
Clean and effective! Thanks. I'd still be curious (for general lattice knowledge) how to ditch those V1 and V2 plots in your other example.
Vince
+1  A: 

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

rcs