[R] image() with a vector

Steven Cordwell s.cordwell at uq.edu.au
Wed Feb 16 05:43:09 CET 2011


Hi,

I have a vectors x and z, for example,

x <- 0:20
z <- round(runif(20,1,7))
y <- 0.5

and I want to display z as an image. However if I then call image() with a vector

image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",yaxt="n",xaxs="r",yaxs="r")

then I get the error

Error in image.default(x, y, t(z), zlim = c(1, 7), col = heat.colors(7),  : 
  dimensions of z are not length(x)(-1) times length(y)(-1)

However, transforming z into a matrix with two rows, where both rows are the same as z above does seem to be a workaround.

z <- matrix(c(z,z),2,length(z),byrow=TRUE)
y <- c(0.25,0.75)
image(x,y,t(z),zlim=c(1,7),col=heat.colors(7),xlab="Year",ylab="Action",yaxt="n",xaxs="r",yaxs="r")

The problem is that when I include the figure that is produced in my pdf, there ends up being a white line that runs through the middle horizontally (where the divison of the cells is) which ruins the presentation of the graphic as x 
is a time series, and z is an action that is performed at that time. There is only one action that is performed at each time step in x, and having the line running through the middle might cause confusion as to how many actions are 
performed at any given time.

I would appreciate any suggestions as to how to get around this, because I haven't been able to find any options for vectors in image(), or if there is a different function that does it.

Cheers

Steven



More information about the R-help mailing list