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 ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.