1

I'm trying to visualize a matrix using image function. I'd like to set the size of cells (I mean the small squares each of which represents one elements of the matrix). I don't know how many elements will my matrix have beforehand.

This is my code now:

A <- matrix(1:20, 5, 4)
image(A)

and I'd like to have something like this:

image(A, sizeOfCell=10)

Would anyone have some idea?

josliber
  • 43,891
  • 12
  • 98
  • 133
kamila
  • 122
  • 1
  • 2
  • 13
  • A [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be very helpful to illustrate your problem. At the time you call `image()`, surely you must have some matrix with some know dimension. Why is a specific size required? Are you during further manipulation after? – MrFlick Apr 27 '15 at 19:17

1 Answers1

1

grid units are probably easiest to work with,

A <- matrix(1:20, 5, 4)
library(grid)
m = A/max(A) # replace with matrix of colours, this will default to grey
grid.raster(m, 
            width = unit(NROW(A)*5,"mm"), # cell 5mm wide
            height = unit(NCOL(A)*4,"mm"),# and 4mm high
            interpolate = FALSE)
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Thank you very much for your comment. It works but I d need to: 1) not to be dependent on number of rows and columns 2) to be albe set the columns by myself like this `loc1g <- c(0, 1, 2, 0) loc2g <- c(1,1, 2, 0) loc3g <- c(0, 1, 2, 0) pval <- data.frame(loc1g, loc2g, loc3g) x = 1:ncol(pval) y = 1:nrow(pval) image(x, y, t(as.matrix(pval)), col = c('white', 'yellow', 'blue', 'green'), breaks = c(0, 0.5, 1, 1.5, 2), xaxt='n', yaxt='n', ylim=c(max(y)+0.5, min(y)-0.5), xlab='', ylab='', )` Would it be able to modify it to fulfil these conditions? – kamila Apr 29 '15 at 05:20