R Save Plot: A Comprehensive Tutorial

All the graphs (bar plot, pie chart, histogram, etc.) we plot in R programming are displayed on the screen by default.

We can save these plots as a file on disk with the help of built-in functions.

It is important to know that plots can be saved as bitmap images (raster) which are fixed size or as vector images which are easily resizable.

We will use the temperature column of built-in dataset airquality to demonstrate how the plots are saved in R.

To demonstrate how the plots are saved, we will create and save a histogram plot.


Save Plot as bitmap Image

Most of the images we come across like jpeg or png are bitmap images. They have a fixed resolution and are pixelated when zoomed enough.

Functions in R that help us save plots in this format are jpeg() and png().

Note: All the functions worked the same way, they just return different file types.


1. Save as jpeg Image

In R, to save a plot in jpeg format, we use the jpeg() function. For example,

# save histogram in jpeg format in current directory
jpeg(file="histogram1.jpeg")

# a histogram we want to save
hist(airquality$Temp)

# call this function to save the file 
dev.off()

In the above example, we have used the jpeg() function to save a histogram in our current directory.

  • "histogram1.jpeg" - name of the histogram we will save in our directory.
  • hist(airquality$Temp) - a histogram we want to save
  • dev.off() - function call to save the file

Note: We can also specify the full path of the file we want to save if we don't want to save it in the current directory as: jpeg(file="C:/Programiz/R-tutorial/histogram1.jpeg").


2. Save as png Image

We use the png() function in R to save plot in png format. For example,

# save as png image in specific directory with 600*350 resolution
png(file="C:/Programiz/R-tutorial/histrogram2.png",
width=600, height=350)

# a histogram we want to save
hist(airquality$Temp)

# a function call to save the file
dev.off()

In the above example, we have used the png() function to save a histogram in png format.

Here, we have also specified the full path of the file we want to save. We have also specified the width and height of the image to 600 and 350 respectively.


Save Plot as Vector Image

We can save our plots as vector images in pdf and postscript formats using the pdg() and postscript() function respectively.

The beauty of vector images is that it is easily resizable. Zooming on the image will not compromise its quality.


Save as pdf Image

In R, to save a plot in pdf format, we use the pdf() function. For example,

# save histogram in pdf format in current directory
pdf(file="histogram1.pdf")

# a histogram we want to save
hist(airquality$Temp)

# call this function to save the file 
dev.off()

Here, we have used the pdf() function to save the histogram in our current directory.

Using pdf() saves the plot in a high quality format.

Note: To save a plot as a postscript file, change the first line to postscript(file="histogram1.ps").


#r 

R Save Plot: A Comprehensive Tutorial
1.00 GEEK