28 July 2011

Raster Graphics in R

As reported in P. Murrell, “Raster Images in R Graphics,” The R Journal, vol. 3, no. 1, pp. 48-54, Jun. 2011 (available online), the facilities for producing raster plots in R have improved dramatically.

I tried this out with a correlation matrix for one of my research projects. Here is the resulting image:

Quite nice and colourful. I originally plotted this using something like
> palette = rainbow(256) > image(x, col = palette)
But the new functions do not have a facility to directly specify a colour palette. You can feed them an matrix of colours though... So the solution is to transform the raw matrix into a colour matrix. So, assuming that the raw data consists of values between 0 and 1, you could do the following:
> y = matrix((rainbow(256))[c(x) * 255 + 1], ncol = ncol(x), nrow = nrow(x), byrow = TRUE) > grid.raster(y)
You need to do a little extra work to get the axes.

No comments:

Post a Comment