2
$\begingroup$

I do not understand the persp plot, my example is:

c<-mat.or.vec(5,2)
c[1,1]<-1
c[1,2]<-1
c[2,1]<-1
c[2,2]<-1
c[3,1]<-2
c[3,2]<-0
c[4,1]<-1
c[4,2]<-1
c[5,1]<-1
c[5,2]<-1

persp(c,xlab = "X", theta = -60,ylab = "Y", zlab = "Z",ticktype = "detailed"
)

I get the following graphic:

plot of persp understanding

But I do not understand it?!

I mean, why are the x and y axis from 0 to 1? What values does the plot take? I thougt it takes the column and row values from the matrix, so like [5,] gives 5 e.g.. Or how does this plot generates?

The matrix is

      [,1] [,2]
[1,]    1    1
[2,]    1    1
[3,]    2    0
[4,]    1    1
[5,]    1    1

And I thought the first point is like x=1, y= 1 and z =1 and so on? Or x=3 and y=1 z=2?

$\endgroup$
1
  • $\begingroup$ I think there's something wrong with how you created your matrix, but I am not expert enough in R to figure out what it is. Maybe someone else will. I added the R tag to help find them $\endgroup$
    – Peter Flom
    Nov 28, 2012 at 14:28

1 Answer 1

5
$\begingroup$

The function does exactly, what it is supposed to do. First of all, you did not specify any x or y values, but only a matrix of z values. Thus the function creates x and y automatically. This is done by transforming the row numbers of the matrix into an equally spaced sequence from 0 to 1 and the same for column numbers and y values. Please read the help page of the function for more information.

See what happens if we append another column:

mat <- matrix(c(1,1,2,1,1,1,1,0,1,1),ncol=2)
mat <- cbind(mat,c(1,1,1,1,1))

persp(mat,xlab = "X", theta = -60,ylab = "Y", zlab = "Z",ticktype = "detailed")

perspective plot

And here is the way to specify x and y:

persp(x=1:5,y=1:3,z=mat,xlab = "X", theta = -60,ylab = "Y", zlab = "Z",ticktype = "detailed")

enter image description here

PS: Don't call an R object c, because that's the name of a heavily used function.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.