In this recipe, we will learn how to save graphs in high-resolution image formats for use in presentations and publications.
We will only use the base graphics functions for this recipe. So, just run the R code at the R prompt. You might wish to save the code as an R script so that you can use it again later.
Let's re-create a simple scatter plot example from Chapter 1, R Graphics, and save it as a PNG file 600 px high and 600 px wide with a resolution of 200 dots per inch (dpi):
png("cars.png",res=200,height=600,width=600) 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) dev.off()
The resulting cars.png
file looks like the following figure:
The pictured graph has a high resolution but the layout and formatting has been lost. So, let...