R for Ecologists: Making MATLAB-like Graphs in R

July 13, 2012

(This article was first published on Climate Change Ecology » R, and kindly contributed to R-bloggers)

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:


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)

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.

To leave a comment for the author, please follow the link and comment on their blog: Climate Change Ecology » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)