Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Exporting graphs in vector formats – SVG, PDF, and PS


In this recipe, we will learn how to save graphs in vector formats such as PDF, SVG, and PostScript (PS), which are resolution-independent.

Getting ready

Once again we will use the basic graph functions. So, just make sure that you have started R and type in the code at the R prompt.

How to do it...

Let's use the same scatter plot example from the previous recipe and save it in different vector formats, starting with PDF:

pdf("cars.pdf")

plot(cars$dist~cars$speed,
main="Relationship between car distance and speed",
xlab="Speed (miles per hour)",ylab="Distance travelled (miles)",
xlim=c(0,30),ylim=c(0,140),
xaxs="i",yaxs="i",
col="red",pch=19,cex=0.5)

dev.off()

Similarly, we can save the graph as SVG or PS using the svg() and postscript() functions, respectively:

svg("3067_10_03.svg")
#plot command here
dev.off()

postscript("3067_10_03.ps")
#plot command here
dev.off()

How it works...

The vector format export commands are similar to the image...