0% found this document useful (0 votes)
62 views

3.how To Save A Plot (As Bitmap, Vector Image, PDF, PostScript) in R

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

3.how To Save A Plot (As Bitmap, Vector Image, PDF, PostScript) in R

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

10/16/2019 How to save a plot (as Bitmap, Vector Image, PDF, PostScript) in R?


Interested
Search in Data Science? Get Started With Data Science in R.
tutorials...
Sign Up Now

Saving a Plot in R
In this article, you’ll learn to save plots in R programming. You’ll learn to save plots as
bitmap and vector images.

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 le on disk with the help of built-in functions.
It is important to know that plots can be saved as bitmap image (raster) which are
xed size or as vector image which are easily resizable.

How to save plot as a bitmap image?


Most of the image we come across like jpeg or png are bitmap image. They have a
xed resolution and are pixelated when zoomed enough.
Functions that help us save plots in this format are jpeg() , png() , bmp() and tiff() .
We will use the temperature column of built-in dataset airquality for the remainder of
this section as example.

> Temperature <- airquality$Temp


> Temperature
[1] 67 72 74 62 56 66 65 59 61 69 74 69 66 68 58 64 66 57 68 62 59 73 61 61
...
[136] 77 71 71 78 67 76 68 82 64 71 81 69 63 70 77 75 76 68

Save as Jpeg image

https://www.datamentor.io/r-programming/saving-plot/ 1/7
10/16/2019 How to save a plot (as Bitmap, Vector Image, PDF, PostScript) in R?

To save a plot as jpeg image we would perform the following steps.


Please note that we need to call the function dev.off() after all the plotting, to save
the le and return control to the screen.

jpeg(file="saving_plot1.jpeg")
hist(Temperature, col="darkgreen")
dev.off()

This will save a jpeg image in the current directory. The resolution of the image by
default will be 480x480 pixel.

Save as png image


We can specify the resolution we want with arguments width and height .
We can also specify the full path of the le we want to save if we don’t want to save
it in the current directory.

https://www.datamentor.io/r-programming/saving-plot/ 2/7
10/16/2019 How to save a plot (as Bitmap, Vector Image, PDF, PostScript) in R?

The following code saves a png le with resolution 600x350 .

png(file="C:/Datamentor/R-tutorial/saving_plot2.png",
width=600, height=350)
hist(Temperature, col="gold")
dev.off()

Save as bmp image


Similarly, we can specify the size of our image in inch, cm or mm with the argument
units and specify ppi with res .

The following code saves a bmp le of size 6x4 inch and 100 ppi.

bmp(file="saving_plot3.bmp",
width=6, height=4, units="in", res=100)
hist(Temperature, col="steelblue")
dev.off()

https://www.datamentor.io/r-programming/saving-plot/ 3/7
10/16/2019 How to save a plot (as Bitmap, Vector Image, PDF, PostScript) in R?

Save as tiff image


Finally, if we want to save in the tiff format, we would only change the rst line to
tiff(filename = "saving_plot3")

tiff(file="saving_plot3.tiff",
width=6, height=4, units="in", res=100)
hist(Temperature, col="steelblue")
dev.off()

How to save plot as a vector image?


We can save our plots as vector image in pdf or postscript formats.
The beauty of vector image is that it is easily resizable. Zooming on the image will not
compromise its quality.

Save as pdf le
To save a plot as pdf we do the following.

https://www.datamentor.io/r-programming/saving-plot/ 4/7
10/16/2019 How to save a plot (as Bitmap, Vector Image, PDF, PostScript) in R?

pdf(file="saving_plot4.pdf")
hist(Temperature, col="violet")
dev.off()

Save as postscript le
Similarly, to save the plot as a postscript le, we change the rst line to
postscript(file="saving_plot4.ps") .

 postscript(file="saving_plot4.ps")
hist(Temperature, col="violet")
 dev.off()

« PREVIOUS
R MULTIPLE PLOTS
NEXT
R PLOT COLOR »
R TUTORIAL

R Introduction

R Flow Control

R Functions

R Data Structure

R Object & Class

R Graphs & Charts

R Advanced Topics

-- R Programming Plot Function

-- R Programming Subplot

-- R Programming Saving Plot

https://www.datamentor.io/r-programming/saving-plot/ 5/7
10/16/2019 How to save a plot (as Bitmap, Vector Image, PDF, PostScript) in R?

-- R Programming Color

-- R Programming 3D Plot

Get started in Data Science With R.


 Learn more

DATAMENTOR

Your goto place for everything data.

COURSES

Data Science With R

RESOURCES

R Tutorials

R Examples

What Is Data Science?

COMPANY

About Us

Adver tising
Contact Us

https://www.datamentor.io/r-programming/saving-plot/ 6/7
10/16/2019 How to save a plot (as Bitmap, Vector Image, PDF, PostScript) in R?

LEGAL

Privacy Policy

Terms And Conditions

Copyright © DataMentor. All rights reserved.

https://www.datamentor.io/r-programming/saving-plot/ 7/7

You might also like