Open In App

Saving Graphs as Files in R

Last Updated : 03 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Graphs are descriptive R objects which facilitate the visual representation of data in R. The data points become more understandable and readable when expressed in the form of plots. The plots can also be saved within the local directory. They can then be accessed without opening the R editor.

1. Saving graph as a pdf object

The graph can be saved as a pdf object in the local working space using pdf() function. The directory followed by the .pdf extension name is supplied as an input argument to the pdf() function. 

Syntax:

pdf(pdf-path)

Arguments : 

  • pdf-path - Path of folder location where pdf file to be stored.

Example Usage:

We are creating a dataframe that is used to make a bar graph which is then saved as a pdf file using pdf() function. Plotting the bar chart using barplot() function giving the color of column blue as a parameter. The processing of the current cycle on the device can then be closed by dev.off() method.

R
data_frame <- data.frame(col1=c(1: 5),
          col2=c(20, 32, 12, 57, 33))

print("Data Frame")
print(data_frame)

pdf("/path_to_your_directory/saving_pdf.pdf")

barplot(data_frame$col2, col="blue")

dev.off()

Output:

baraspdf
Sample Data
Saving Graphs to Files in R

2. Saving graph as a PNG image 

The graph can be saved as a png image object in the local working space. The directory followed by the .png extension name with the path of location supplied as an input argument to the png() function.

Syntax:

png(png-path)

Arguments : 

  • png-path - Path of folder location where pdf file to be stored.

Example Usage:

We are creating a sample data frame for plotting. The graph is plotted using the barplot() method, which takes vector of values as input. The png() function saves the barplot into a .png file and saves it into a directory of our choice. The processing of the current cycle on the device can then be closed by dev.off() method.

R
data_frame <- data.frame(col1=c(1: 5),
           col2=c(20, 32, 12, 57, 33))

print("Data Frame")
print(data_frame)

png("/example_path/saving_png.png")

barplot(data_frame$col2)
dev.off()

Output:

sample_data
DataFrame
Saving Graphs to Files in R
R Saving Graphs as Files

3. Saving graph as a .JPEG image 

The graph can be saved as a jpeg image object in the local working space.  The directory followed by the .jpeg extension name with path of location supplied as an input argument to the jpeg function.

Syntax:

jpeg(jpeg-path)

Arguments : 

  • jpeg-path - Path of folder location where pdf file to be stored.

Example Usage:

We are creating a sample data frame for plotting. The graph is plotted using the barplot() method, which takes vector of values as input. The jpeg() function saves the barplot into a .jpeg file and saves it into a directory of our choice. The processing of the current cycle on the device can then be closed by dev.off() method.

R
data_frame <- data.frame(col1=c(1: 5),
             col2=c(20, 32, 12, 57, 33)
             )

print("Data Frame")
print(data_frame)

jpeg("/example_path/saving_jpeg.jpeg")

barplot(data_frame$col2)
dev.off()

Output:

sample_data
Sample Data
Saving Graphs to Files in R

Exporting graph on own local device

The graph plotted can be then exported to our own device by clicking the "Export" icon above the image in case you are using the R studio editor. The following image shows the export icon and the options available on opening it :

Saving Graphs to Files in R

If you click on "Save as Image" the following popup opens :

Saving Graphs to Files in R

If you click on "Save as PDF" the following popup opens and after selecting the directory click on save. It will be saved at the selected location:

Saving Graphs to Files in R

Next Article
Article Tags :

Similar Reads