Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

increasing 'x' and 'y' values expected

Tags:

r

I want to do a very simple 3D plot with this code:

x<-c(1:10)
y<-c(10:1)
z<-cbind(x,y)

persp(x,y,z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")

but I get this error:

Error in persp.default(x, y, z, theta = 30, phi = 30,  : 
  increasing 'x' and 'y' values expected

what should I do exactly to solve this problem?

UPDATE If I change my data. For example for x

test<-c(0.4118836, 0.3481329, 0.3582897, 0.3122690, 0.3346040, 0.2409363, 0.2474494, 0.1744042, 0.1589965, 0.2435564)

and then

x<-test
y<-c(1:10)
z<-matrix(x,nrow=length(x),ncol=length(y))
persp(x,y,z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")

I am getting the same error

like image 316
Kaja Avatar asked May 14 '14 09:05

Kaja


2 Answers

Both x and y should be increasing, as the Error msg says. Try with y <- 1:10.

Moreover, z may also be problematic. We read in ?persp:

Notice that persp 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, so that with the standard rotation angles, the top left corner of the matrix is displayed at the left hand side, closest to the user.

An easy way to create z is by calling outer.

Try e.g.

x <- 1:10
y <- 1:10
z <- outer(x, y, function(xi, yj) xi^2+yj^2)
persp(x, y, z)
like image 116
gagolews Avatar answered Sep 19 '22 14:09

gagolews


As the error message implies, one problem is that in y<-c(10:1) the y values do not increase...

Besides, z needs to be an x*y matrix.

So try the following:

x<-c(1:10)
y<-c(1:10)
z<-matrix(x,nrow=length(x),ncol=length(y)

persp(x,y,z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")
like image 33
jfmoyen Avatar answered Sep 20 '22 14:09

jfmoyen