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.
精彩评论