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
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)
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With