R for Ecologists: Making MATLAB-like Graphs in R

I’ve decided that my blog should become a brain dump for my experience/troubles/solutions to programming in R. I’ve met many people who want to learn but don’t have the time or patience to sit down and figure it out from scratch. I’ll post interesting tidbits of code and examples to help people who are trying to do similar things. Most of my data manipulation will have an ecological bent, which is great if you’re an ecologist and possibly useful if you’re not. I won’t cover basics (i.e. how to make a plot or do regression), I’ll attempt to cover intermediate and advanced concepts that aren’t easily found online or in a manual. Many of my posts will be related to graphing, as most of my issues are related to graphing.

This first post will cover how to replicate MATLAB-style graphs in R.

MATLAB scatterplot

I’ve always been a fan of the MATLAB style, at least for scatter plots, although I find the quality of MATLAB graphs to be far below the quality of R graphs. Note that the points overlap the axes (MATLAB doesn’t automatically fix this) and the quality of the figure just doesn’t seem right. The beauty of R is that it generates high-quality plots and it’s sort of like a drawing board in that if you can dream it, you can do it (albeit with some frustration, swearing, and tears).

To start with, generate some data. We’ll make the x-values evenly spaced from one to 10, and make y = x + error.

x <- 1:10
y <- x + rnorm(10, 0, 2)

Let’s look at the generic R scatterplot:

plot(x,y)

Generic R scatterplot

Notice that R has automatically adjusted the axes so the points don’t overlap. Great, less work for me. The big difference between R and MATLAB is the inward facing ticks around the graph. That requires making use of the ‘tck’ argument in the axis( ) command. ‘tck’ adjusts how long and in which direction the tick points: negative values point outwards and positive values point inwards. The number of the ‘tck’ argument is percentage of plot area the tick mark covers (i.e.¬†a value of 0.5 indicates the tick will be as long as 50% of the plot area). Let’s make our new plot with four custom axes:

plot(x, y, pch=16, axes=F)
axis(1, lwd=0, lwd.tick=1, tck=0.02)
axis(2, lwd=0, lwd.tick=1, tck=0.02)
# Remember to suppress axis labels on the top and right axes
axis(3, lwd=0, lwd.tick=1, tck=0.02, lab=F)
axis(4, lwd=0, lwd.tick=1, tck=0.02, lab=F)
box()

MATLAB-style R scatterplot

Voilà: a MATLAB-style R graph that has the nice quality of R with the style of MATLAB.

This was a pretty basic introduction, I started simple for my first post. Expect things to get more complicated as I begin digging into graphs and statistics for my dissertation.

About these ads

One thought on “R for Ecologists: Making MATLAB-like Graphs in R

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