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 high-resolution image formats – PNG, JPEG, BMP, and TIFF


In this recipe, we will learn how to save graphs in high-resolution image formats for use in presentations and publications.

Getting ready

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.

How to do it...

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