To export multiple plots to PDF in R, you can use pdf() function and dev.off() function. The pdf() function opens a graphics device to save plots to a PDF file, while dev.off() closes the graphics device and saves the file.
The following method shows how you can do it with syntax.
Method 1: Use pdf() and dev.off() Function
pdf(file)
for (i in data) {
x=rnorm(i)
y=rnorm(i)
plot(x, y)
}
dev.off()
The following example shows how to export multiple plots to pdf in R using pdf() and dev.off() function.
Using pdf() and dev.off() Function
Let’s see how we can use pdf() and dev.off() function to export multiple plots to pdf in r:
# Create dataframe
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H"),
Pressure1=c(78.2, 28, 71.7, 80.21, 72.7, 30, 84.21, 76.2),
Temperature1=c(31, 33, 36, 37, 36, 33, 37, 31),
Status=c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE,TRUE,TRUE))
# Specify path to save PDF
destination = 'R_plots1.pdf'
# Open PDF file
pdf(file=destination)
# Save plots to PDF
for (i in df$Pressure1) {
x=rnorm(i)
y=rnorm(i)
plot(x, y)
}
# Turn off PDF plotting file
dev.off()
Here the above code shows how to export plots to pdf file using pdf() and dev.off() function.