💾Saving Graphs in R
Create a Plot
Let's create a basic scatterplot by plot()
function in R.
# Load Data
data(mtcars)
# Create a basic graph
plot(x=mtcars$wt, y=mtcars$mpg, xlab = "wt", ylab = "mpg", frame = FALSE)
Output:

Saving Graphs
Generally, we can save R graphs by:
By RStudio
By R code
By RStudio
Plots panel –> Export –> Save as Image or Save as PDF

We can also choose other formats to save in RStudio.

By R code
It is also possible to use R code to save the graphs.
Utilize functions such as
jpeg(),
png(),svg()
orpdf()
to determine the files in which to save your image. You can also include additional arguments to specify the width and height of the image.Create the plot
Close the file with
dev.off()
# 1. Open jpeg file
jpeg("plotname.jpg", width = 300, height = "350")
# 2. Create a basic graph
plot(x=mtcars$wt, y=mtcars$mpg, xlab = "wt", ylab = "mpg", frame = FALSE)
# 3. close the file with dev.off()
dev.off()
Alternatively, we can first create the plot and then copy the plot to a graphics device.
# 1. Create a basic graph
plot(x=mtcars$wt, y=mtcars$mpg, xlab = "wt", ylab = "mpg", frame = FALSE)
# 2. Copy the plot
dev.copy(pdf,"plotname.pdf",width=7,height=5)
# 3. Close the graphics device
dev.off()
Last updated