简体   繁体   中英

suppress interactive plotting in R

I would like to send a plot to a file using pdf() , but plot.MCMCglmm() is attempting to act interactively, which interferes with dev.off() .

pdf(file="model.pdf")
plot(model, random=FALSE)
Hit <Return> to see next plot: dev.off()

And the file is not closed. Adding another dev.off() closes the file. Is there a way to suppress the interactive plotting?

EDIT example:

require(MCMCglmm)

mod_dat <- data.frame( Name = rep(letters[1:3], each=10),
                 Group = rep(letters[1:3], 10),
                 Age = rep(letters[1:5], each=6),
                 Happy = rep(letters[1:2], 15),
                 x = rnorm(30),
                 y = rnorm(30) )

mod_out <- MCMCglmm( y~x, random=~Name+Group+Age+Happy, 
  data=mod_dat, verbose=FALSE )

pdf( file="model out.pdf" )
plot(mod_out)
dev.off()
dev.off()

You could modify the plot function for plot.MCMCglmm to turn off the new page prompt. You can get the code for the function by typing plot.MCMCglmm in the console.

myPlotGLMM = function (x, random = FALSE, ...) 
{
  nF <- x$Fixed$nfl
  #devAskNewPage.orig <- devAskNewPage()
  if (random) {
    nF <- sum(rep(x$Random$nrl, x$Random$nfl)) + nF
    if (nF != dim(x$Sol)[2]) {
      stop("random effects not saved and cannot be plotted")
    }
  }
  plot(x$Sol[, 1:nF, drop = FALSE], ...)
  #devAskNewPage(TRUE)
  if (is.null(x$Lambda) == FALSE) {
    plot(x$Lambda, ...)
    #devAskNewPage(TRUE)
  }
  plot(x$VCV, ...)
  #devAskNewPage(devAskNewPage.orig)
}

myPlotGLMM(model)

Another option is to plot everything onto one page. Without a reproducible example I can't test this but this should work:

pdf(file="model.pdf")
par(mfrow=c(2,2))
plot(model, random=FALSE)
dev.off()

So if this generated four plots, they would be arranged on one page in 2 x2 grid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM