Site icon DataFlair

Learn to Save Graphs to Files in R Programming with R Graphical Devices

Save Graphs to Files in R Programming

FREE Online Courses: Dive into Knowledge for Free. Learn More!

In our previous R tutorial, we have discussed about R Lattice Package, now, it’s time for learning how to save graphs to files in R programming. We will discuss its different methods along with various R graphic devices.

So, let’s start the tutorial.

What is a Graphics Device?

A graphics device is something where we can delineate a plot. When we make a plot in R, it has to be “sent” to a specific:

In order for the plot to be “sent”, the most common place is the screen device.

You must definitely check the tutorial on Data Visualization in R

R Graphical Devices

The following devices are currently available:

The following devices will be functional if R is compiled to use them:

R runs on many different operating systems and also supports different graphics formats.

Format             Driver                              Notes

JPEG                  jpeg                                 It is used everywhere but it does not facilitate resizing.
PNG                   png                                  similar to JPEG and it does not resize.
WMF                  win.metafile                 It is based on the Windows platform only. It is best suited for                                                                           MS word and it facilitates flexible resizing.
PDF                   pdf                                   pdflatex; easily resizable

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Postscript           postscript                 latex and Open Office; easily resizable

Wait! Have you checked – Graphical Data Analysis with R Programming

Method to Save Graphs to Files in R

In order to save graphics to an image file, there are three steps in R:

  1. You can create a graphics device of PNG format using png(), JPG format using jpg() and PDF format using pdf().
  2. Plot your data.
  3. Closing the graphics device and saving the image using dev.off.
setwd("~/")
getwd()
data("mtcars")
scatter <- mtcars[,c('wt','mpg')]
#Naming the file
png(file = "DataFlair_plot.png")
#Creating our Scatterplot
plot(x = scatter$wt,y = scatter$mpg,
xlab = "Weight",
ylab = "Mileage",
xlim = c(2.5,5),
ylim = c(15,30), 
main = "Weight vs Mileage"
)
dev.off() #Saving the file

Output:

Local Sessions with Windows or OS X

The graphical user interface makes it easy to save files. Right-click inside the graph on Windows platform. Then you can select either “Save as metafile …” or “Save as postscript …”. If you are working on MS Word, then be sure to save the file as a metafile.

On Mac, click on the graphics window to make sure it’s the active one, then go to File -> Save in the menubar, and choose a location to save the file. It will save as a PDF file, which we can double-click to open in Preview, and then use the File -> Save as menu choice to convert it to another format.

Summary

We learned the different methods and functions which we use to save graphs to files. By using these different functions and methods, it’s easier to plot graphs and save files. As R runs on many operating systems, the R commands are very helpful in the above case to plot graphs and to save them in a file.

Now, it’s the turn for R Performance Tuning Techniques

Still, if you have any doubts regarding the tutorial, ask in the comment section. We will be glad to help you.

Exit mobile version