Skip to content
Prev 86405 / 398513 Next

transforming data frame for use with persp

On Mon, 13 Feb 2006, Denis Chabot wrote:

            
Well, image() and friends have a rather strange representation, so it 
isn't obvious (see the help page:

     "Notice that 'image' interprets the 'z' matrix as a table of
     'f(x[i], y[j])' values, so that the x axis corresponds to row
     number and the y axis to column number, with column 1 at the
     bottom, i.e. a 90 degree counter-clockwise rotation of the
     conventional printed layout of a matrix.").

So:

Depth <- seq(40,220, 20)
Temp <- seq(-1, 6, 0.5)
My.data <- expand.grid(Depth=Depth, Temp=Temp)
predgam <- -0.5*My.data$Depth + 12.5*My.data$Temp + 8*rnorm(nrow(My.data))
pred.data <- data.frame(My.data, predgam)
library(lattice)
levelplot(predgam ~ Depth + Temp, pred.data) # for sanity check
z <- t(matrix(predgam, nrow=length(Temp), byrow=TRUE)) # see "Notice" 
image(Depth, Temp, z)
persp(Depth, Temp, z)

should do it, since the data are already in a regular grid.