20 C
London
Monday, July 21, 2025
HomeRImport & Export Data in RHow to Save Multiple Plots to PDF in R

How to Save Multiple Plots to PDF in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to save multiple plots to a PDF in R:

#specify path to save PDF to
destination = 'C:\Users\Bob\Documents\my_plots.pdf'

#open PDF
pdf(file=destination)

#specify to save plots in 2x2 grid
par(mfrow = c(2,2))

#save plots to PDF
for (i in 1:4) {   
  x=rnorm(i)  
  y=rnorm(i)  
  plot(x, y)   
}

#turn off PDF plotting
dev.off() 

The following examples show how to use this syntax in practice.

Example 1: Save Multiple Plots to Same Page in PDF

The following code shows how to save several plots to the same page in a PDF:

#specify path to save PDF to
destination = 'C:\Users\Bob\Documents\my_plots.pdf'

#open PDF
pdf(file=destination)

#specify to save plots in 2x2 grid
par(mfrow = c(2,2))

#save plots to PDF
for (i in 1:4) {   
  x=rnorm(i)  
  y=rnorm(i)  
  plot(x, y)   
}

#turn off PDF plotting
dev.off() 

Once I navigate to the PDF in the specified location on my computer, I find the following one-page PDF with four plots on it:

Example 2: Save Multiple Plots to Different Pages in PDF

To save multiple plots to different pages in a PDF, I can simply remove the par() function:

#specify path to save PDF to
destination = 'C:\Users\Bob\Documents\my_plots.pdf'

#open PDF
pdf(file=destination)

#save plots to PDF
for (i in 1:4) {   
  x=rnorm(i)  
  y=rnorm(i)  
  plot(x, y)   
}

#turn off PDF plotting
dev.off() 

Once I navigate to the PDF in the specified location on my computer, I find the a four-page PDF with one plot on each page.

Additional Resources

How to Use the par() Function in R
How to Overlay Plots in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories