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