Some links to useful plot() tips

I’ve been working with some basic plots to visualize some time series data in one of my projects.  I’ve always had a hard time recalling exactly how to take things beyond the simple plot default settings so here are some useful links that I’ve found that were quite helpful.

First, the tried and true Quick-R site, for creating a y-axis on the right side of your plot (using side=):

http://statmethods.net/advgraphs/axes.html

Next this R-bloggers post on creating a multiple y-axes:

http://www.r-bloggers.com/multiple-y-axis-in-a-r-plot/

Then there is this link from Revolutions that contains some very useful information about resolution for output:

http://blog.revolutionanalytics.com/2009/01/10-tips-for-making-your-r-graphics-look-their-best.html

Lastly, synthesizing information from the r-bloggers link I have found VERY useful the use of the pretty function to help customize the tick marks on any axes (with the help also of ylim).  I’ll cut and paste some code directly, and hopefully you can get the gist (understanding that the code snippet is very much out of context):


par(mar=c(5, 5, 4, 4) + 0.1)

ylimit = c(0, (max(eventbymon$eventcount.sum))+0.25*(max(eventbymon$eventcount.sum)))

plot(eventbymon$Eligibility.Date, eventbymon$eventcount.sum, type="l",  yaxt="n", ylab="", lty=3, ylim = ylimit, xlab="Month", cex.lab=1) axis(4, <strong>at=pretty(ylimit))</strong> mtext("Events(n)", side=4, line=3, cex=1,las=0)

title(main=paste(pers,"Events by Month for", coh, "for",year), cex.main=1.1)

par(new=T)

ylimit = c(min(eligbymon$Eligibility.Date.length)/1.05, (max(eligbymon$Eligibility.Date.length))) plot(eligbymon$Eligibility.Date, eligbymon$Eligibility.Date.length, type="l", ylim=ylimit, ylab="Eligibility (n)", xlab="", cex.lab=1)

legend("bottomright",legend=c("Eligibility","Events"),lty=c(1,3), cex=0.85, inset=.025)

Advertisements

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 )

Google+ photo

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

Connecting to %s