Save Graphs to Files in R Programming – R Graphic Devices
In our previous R tutorial, we have discussed Input-Output features in R. Now in this blog we are going to discuss how to save graphs to files in R programming? Before going to this we will discuss what is Graphics Device in R then we will cover various methods to save graphs to files in R.
So, let’s start the tutorial on how to Save Graphs to Files in R.
2. What is a Graphics Device?
A graphics device is something where we can make a plot to appear. When we make a plot in R, it has to be “sent” to a specific.
- Window on your computer (screen device)
- PDF file (file device)
- PNG or JPEG file (file device)
- Scalable vector graphics (SVG) file (file device)
The most common place for a plot to be “sent” is the screen device.
- A Mac, the screen device is launched with the quartz()
- Windows, the screen device is launched with windows()
- Unix/Linux, the screen device is launched with x11()
3. R Graphic Devices
The following devices are currently available:
- PDF – Write PDF graphics commands to file.
- xfig – Device for XFIG graphics file format.
- pictex – Writes TeX/PicTeX graphics commands to a file (of historical interest only).
- postscript – Writes PostScript graphics commands to a file.
- bitmap – bitmap pseudo-device via Ghostscript (if available).
The following devices will be functional if R was compiled to use them:
- png – PNG bitmap device
- jpeg – JPEG bitmap device
- bmp – BMP bitmap device
- tiff – TIFF bitmap device
- X11 – The graphics device for the X11 windowing system.
- svg – SVG device based on cairo graphics.
- cario.pdf, – cairo_ps PDF and PostScript devices based on cairo graphics.
- quartz – The graphics device for the macOS native Quartz 2d graphics system.
- How to save Graphs in R
R runs on so many different operating systems. It supports so many different graphics formats.
Format Driver Notes
|JPEG jpeg is been used anywhere but doesn’t resize|
|PNG png is been used anywhere but doesn’t resize|
|WMF win.metafile Windows only; best choice with Word; easily resizable|
|PDF pdf pdflatex; easily resizable|
Postscript postscript latex and Open Office; easily resizable
4. Methods to Save Graphs to Files in R
Below, are the methods to Save Graphs to Files in R
i. A General Method
Here’s a general method that will work on any computer with R, regardless of operating system or the way that we are connecting.
If we have to save a plot as a JPG file, so we will use the jpeg driver. If we want to save a jpg file called “rplot.jpg” containing a plot of x andy, we would type the following commands:
When we are done with your plotting commands, enter the dev.off() command. This is very important – without it, we will get a partial plot or nothing at all.
jpeg('rplot.jpg') > plot(x,y) > dev.off()
ii. Another Approach
In R, the dev.copy command is used to copy the contents of the graph window to a file without having to re-enter the commands.
To create a png file called myplot.png from a graph that is displayed by R, type
> dev.copy(png,'myplot.png') > dev.off()
iii. Local Sessions with Windows or OS X
We are actually sitting in front of a Windows or Mac computer, the graphical user interface makes it easy to save files. Under Windows, right-click inside the graph window, and choose either “Save as metafile …” or “Save as postscript …” If using Word, make sure to save as a metafile.
On a 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 to another format.
So, this was all in Save Graphs to Files in R. Hope you like our explanation
Hence, we are now aware of 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 thus R command are very helpful in this case to plot and graphs and to save them in a file. Still, if you have any doubt regarding Save Graphs to Files in R, ask in the comment tab. We will definitely help you.