Here’s a new years tip: This year resolve to get your resolution right when exporting plots in R

Winston Chang’s Cookbook for R (http://wiki.stdout.org/rcookbook/) has become one of my most favorite R reference sites as of late. In large part, this is because he does a most excellent job of providing some easily digestible ggplot examples, and I’ve been trying to make a move away from base to ggplot for all of my plotting needs.

While there are many very useful nuggets to be found in perusing his site, one useful tip I use every time I export a plot in R is this hint on adjusting the resolution of plots regardless of any device I’m printing to.

His code is found on this page (http://wiki.stdout.org/rcookbook/Graphs/Output%20to%20a%20file/), but in essence the gist of it all goes like this:

# First try it without adjusting the ppi
png("plot1.png")
plot(mtcars$wt, mtcars$mpg, main="Scatterplot w/o adjustment", xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
attach(mtcars)
abline(lm(mpg~wt), col="red") # regression line (y~x) 
lines(lowess(wt,mpg), col="blue") # lowess line (x,y)
dev.off() 
#  Now try it with adjustment -- as you can see you have to feed the device output function the actual width and height
ppi <- 300
png("plot2.png", width=6*ppi, height=6*ppi, res=ppi)
plot(mtcars$wt, mtcars$mpg, main="Scatterplot w/adjustment", xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
attach(mtcars)
abline(lm(mpg~wt), col="red") # regression line (y~x) 
lines(lowess(wt,mpg), col="blue") # lowess line (x,y)
dev.off() 

I’ve been having quite a bit of fun with bubble plots and the googleVis package recently. The googleVis package, for one, is absolutely MIND-BLOWING! I’m hoping to carve out the time to do a post about that sometime in the very near future!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s