Home > Enterprise >  Save the multiple plot in loop in R to pdf
Save the multiple plot in loop in R to pdf

Time:08-31

I have two different plots. I want to put them together, I use them to save in pdf. However, the pdf can not open. I am using the following simple template to save the figures in pdf, can you please help me with that? Thanks.

for(i in 1:4){

   par(mfrow=c(2, 1))
   # plot 1 
   layout(matrix(c(rep(1,6),rep(2,4)),nrow=2,ncol=5))

    plotModelFit(dataBlock,fit)
    plotMarginal(extract(fit,"beta[1]"),predictor="dataset age",col="blue")

    #plot 2
    plotModelFit(dataBlock,fit)
    plotMarginal(extract(fit,"beta[1]"),predictor="dataset age",col="blue")

    # save to pdf
    pdf(file=sprintf("/Users/test.pdf",responseVars[i]),width=10,height=6)
    dev.off()
    }

CodePudding user response:

  1. you need only one of par(mfrow=c(2, 1)) and layout(matrix(c(rep(1,6),rep(2,4)),nrow=2,ncol=5)).

  2. you need to place pdf() call before par() or layout().

  3. you need something like sprintf("/Users/test-%s.pdf",responseVars[i]) for the file name.

  •  Tags:  
  • r
  • Related