R outer function Error in persp.default(x, y, z) :

2019-08-06 05:35发布

问题:

could someone explain to me why these two pieces of codes return two diferent things? the first one :

x<-y<-seq(from=-1,to=1,by=0.1)
one<-function(x,y){
    x
}
z<-outer(x,y,FUN=one)
persp(x,y,z)

returns the surface it should.

while :

x<-y<-seq(from=-1,to=1,by=0.1)
one<-function(x,y){
    array(1, dim=length(x))
 }
 z<-outer(x,y,FUN=one)
 persp(x,y,z) 

returns : "Error in persp.default(x, y, z = outer(x, y, one)) : invalid 'z' limits"

回答1:

For the second case, plotting the plane z=1, you just need to specify the desired zlim yourself, e.g.

persp(x,y,z,zlim=c(0,2))

The persp function expects to plot in a 3-dimensional space. The default is to try to plot over the ranges of x, y and z, but in this case z's range is degenerate.



标签: r limits