格子问题:格子对象来自JAGS,但设备无法设置

时间:2009-10-29 03:38:43

标签: graphics r lattice

我在JAGS中使用runjags运行了R,我收到了一个巨大的列表(此示例的命名结果)。

每当我访问results$density时,默认石英设备中会弹出两个lattice plots(每个参数一个)。

我需要将这些与par(mfrow=c(2, 1))或类似的方法结合起来,并将它们发送到pdf device

我没有尝试过任何工作。有什么想法吗?

我已尝试dev.printpdf()dev.off()等,但没有运气。

3 个答案:

答案 0 :(得分:2)

这是一种通过操纵Trellis结构来抛弃“V1”面板的方法:

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

答案 1 :(得分:1)

例如,对于run.jags中包含的示例,请使用

检查列表的结构
sink("results_str.txt")
str(results$density)
sink()

然后您将看到名为 layout 的组件。可以使用

设置每个变量的两个图的布局
results$density$m$layout <- c(1,2)
print(results$density$m)

可以使用c.trellis包中的latticeExtra方法合并不同参数的图表。

class(results$density$m) <- "trellis"   # overwrite class "plotindpages"
class(results$density$c) <- "trellis"   # overwrite class "plotindpages"
library("latticeExtra")
update(c(results$density$m, results$density$c), layout=c(2,2))

output of c.trellis http://img88.imageshack.us/img88/6481/ctrellis.png

另一种方法是使用grid视口:

library("grid")
results$density$m$layout <- c(2,1)
results$density$c$layout <- c(2,1)
class(results$density$m) <- "trellis"
class(results$density$c) <- "trellis"
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(results$density$m, newpage=FALSE)
popViewport()
pushViewport(viewport(layout.pos.row=2))
print(results$density$c, newpage=FALSE)
popViewport()
popViewport()

grid output http://img88.imageshack.us/img88/5967/grida.png

答案 2 :(得分:1)

组合图的最简单方法是使用存储在结果$ 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"))