开发者

Save multiple graphs one after another in one pdf file [duplicate]

开发者 https://www.devze.com 2023-04-08 14:15 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How to print R graphics to multiple pages of a PDF and multiple PDFs?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to print R graphics to multiple pages of a PDF and multiple PDFs?

I am new to R and h开发者_JAVA技巧ave a quick question. The following code writes one .pdf file for each graph. I would like to add figures one after another in ONE pdf file. Thank you so much. Greatly appreciate any help.

i=5  
while (i<=10)   
{   
  name1="C:\\temp\\"  
  num=i   
  ext = ".pdf"  
  path3 = paste(name1,num,ext)  
  par(mfrow = c(2,1))  
  pdf(file=path3)  
  VAR1=rnorm(i)  
  VAR2=rnorm(i)  
  plot(VAR1,VAR2)  
  dev.off()  
  i=i+1  
}  


Just move your pdf() function call and your dev.off() call outside the loop:

somePDFPath = "C:\\temp\\some.pdf"
pdf(file=somePDFPath)  

for (i in seq(5,10))   
{   
  par(mfrow = c(2,1))
  VAR1=rnorm(i)  
  VAR2=rnorm(i)  
  plot(VAR1,VAR2)   
} 
dev.off() 

Note my use the the seq() function to loop instead of while() with a counter variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消