rasterImage

rasterImage(image, xleft, ybottom, xright, ytop, angle=0, interpolate=TRUE)
Create a grid of colors, which could represent pixels of an image or something like a correlation matrix.
  • image – A "raster" object, or an object that can be coerced to a raster object via as.raster (e.g. an array with dimensions d x d x 3).
  • xleft, ybottom, xright, ytop – The boundaries of the raster image.
  • angle – If desired, specify an angle to rotate the raster image. The rotation pivots on the bottom-left corner.
  • interpolate – If TRUE, then automatic smoothing will be done on the raster image.

Example. I generate a correlation matrix, then create two objects called red and blue that will represent negative and positive values in the matrix. I then put these into an array object, where the [,,1] component is the red component, [,,2] represents green and is all zeros, and [,,3] represents blue. Each [i,j,] element is passed to the rgb function to create colors for the raster image at location i,j. Finally, I generate a plot and add the raster image.
> set.seed(5)
> R <- cor(matrix(rnorm(40), 8))
> round(R, 3)
       [,1]   [,2]   [,3]   [,4]   [,5]
[1,]  1.000 -0.439 -0.334 -0.565 -0.125
[2,] -0.439  1.000 -0.340  0.309  0.496
[3,] -0.334 -0.340  1.000 -0.073 -0.739
[4,] -0.565  0.309 -0.073  1.000  0.604
[5,] -0.125  0.496 -0.739  0.604  1.000
> 
> d <- dim(R)
> red  <- ifelse(R < 0, -R, 0)
> blue <- ifelse(R < 0, 0, R)
> cols <- array(c(red, rep(0, prod(d)), blue),
+               c(d[1],d[2],3))
> 
> png("rasterImage.png")
> plot(0:5+0.5, 0:5+0.5, type="n")
> rasterImage(cols, 0.5, 0.5, 5.5, 5.5,
+             interpolate=FALSE)
> dev.off()
null device 
          1
> 

Thank you. Thanks to Tamar Sofer! One of her questions motivated this post.

Leave a Reply