Matrix rotation for image and contour plots in R

Say we wish to plot a matrix object in R using common functions like image, contour, or filled.contour, or perhaps using a related plotting function such as image.plot from the fields package. Many R functions whose outputs include matrices which are intended to be plotted with the above functions are typically written with a certain convention in mind.

That is, these plotting functions which work on matrix objects interpret the matrix z as a table of f(x[i], y[j]). This means that for an n x m matrix, the x axis of the plot will correspond to the rows of the matrix, left to right from rows 1 to n, and the y axis to columns. Further, columns 1 to m go from the bottom row to the top row. This represents a 90 degree counter-clockwise rotation of how we conventionally think of a matrix, rows by columns, top to bottom and left to right, respectively.

So if we create a matrix ourselves, we typically do it the “normal” way rather than follow this programmatic convention. As a result, a simple image plot reveals the classic sideways picture. In these cases we have to rotate our matrix 90 degrees clockwise in anticipation of this odd display. An efficient, quick and concise example of how to do this is with the following function:

f <- function(m) t(m)[,nrow(m):1]
mat <- matrix(1:25,5,5) # Example
mat[4:5,4:5] <- 100
mat # original matrix
f(mat) # rotation achieved by our function

We want our plot to be similarly oriented, but initially it is not. The quarter turn in advance, allows us to plot with the desired orientation:

image(mat) on the left and image(f(mat)) on the right

Also, we don’t need to formally define a function to do this. We can simply redefine our matrix:

m <- t(m)[,nrow(m):1]

and be done with it. But the function is nice if you anticipate doing this a bunch of times to different matrix objects.

To achieve the 90 degree clockwise rotation, all we do is transpose our matrix and then flip the columns. (Note that the reversed column numbers begin with nrow because m refers to the original matrix but the operation is carried out after m has been transposed.)

I often use the image.plot function from the fields package because I like the control it gives me over plot formatting. I find it preferable to both the basic image function and the plot function for rasterLayer objects from the raster package. If I am working with a raster layer and I extract the values as a matrix object, the matrix retains the standard orientation. This is another case where plotting the matrix object will require it first be rotated.

This entry was posted by Matt Leonawicz.

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: