Possible Duplicate:
How to print R graphics to multiple pages of a PDF and multiple PDFs?
I am new to R and have 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
}
Save Multiple Plots to Same Page in PDF: To save multiple plots to the same page in the PDF file, we use the par() function to create a grid and then add plots to the grid. In this way, all the plots are saved on the same page of the pdf file. We use the mfrow argument to the par() function to create the desired grid.
Under Windows, right click inside the graph window, and choose either "Save as metafile ..." or "Save as postscript ..." If using Word, make sure to save as a metafile.
The par() function is used to set or query graphical parameters. We can divide the frame into the desired grid, add a margin to the plot or change the background color of the frame by using the par() function. We can use the par() function in R to create multiple plots at once.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With