错误:在persp {fields}中期望增加'x'和'y'值 [英] Error : increasing 'x' and 'y' values expected in persp {fields}

查看:256
本文介绍了错误:在persp {fields}中期望增加'x'和'y'值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在空间中有500个点,其中latlng为:

lng=runif(100,1,4)
lat=runif(100,40,40.1)

假设我们的密度(密度)为

z=rnorm(1000)

我尝试使用persp

绘制3d曲面

persp(lng,lat,z)

但是我有这个错误:

Error : increasing 'x' and 'y' values expected

我使用

Error : increasing 'x' and 'y' values expected

xy进行了排序

data=data.frame(lng,lat)
data=data[ order(data[,1],data[,2]), ]

然后我再次尝试

persp(data[,1],data[,2],z)

但是我仍然有同样的问题.

我该怎么办?

谢谢.

解决方案

问题是参数z以及xy的顺序.正如阴影在评论中提到的那样,在这种情况下,它必须是dim(z) = length(x) x length(y)的矩阵.显然,x和y必须同时两者排序,与上面的阴影所提到的完全相同,在persp.default的源代码中也是如此.

if (any(diff(x) <= 0) || any(diff(y) <= 0)) #both need to be ordered
        stop("increasing 'x' and 'y' values expected")

因此,以下工作有效:

lng=runif(100,1,4)
lat=runif(100,40,40.1)
z=matrix(rnorm(10000),ncol=100,nrow=100) #use the correct z as matrix
data=data.frame(lng,lat)
data=data[ order(data[,1],data[,2]), ]   #just order lng as you do 
data$lat <- sort(data$lat)               #you need to sort lat too

persp(data[,1] ,data[,2], z) 

现在可以使用了.

修改

我认为您真正想做的是在z中获得lan/lng坐标并绘制它们. z参数需要携带坐标. x和y参数仅分别携带x和y轴的值.您可以使用默认的x和y argumens(即,将它们保留为空白),也可以分别对lan和lng进行排序以提供实际值.因此,您正在寻找的解决方案是:

lng=runif(100,1,4)
lat=runif(100,40,40.1)
z= as.matrix(data.frame(lng=lng,lat=lat))
z=matrix(rnorm(10000),ncol=100,nrow=100) #z contains lan and lng

#plot z with default x and y values (or use sorted lng and lat instead)
persp( z = z)  

哪个输出:

I have 500 points in space with lat, lng as :

lng=runif(100,1,4)
lat=runif(100,40,40.1)

Let's say that we have there (density) as

z=rnorm(1000)

I've tried to plot 3d surface using persp

persp(lng,lat,z)

But I have this error :

Error : increasing 'x' and 'y' values expected

I sorted x and y using

data=data.frame(lng,lat)
data=data[ order(data[,1],data[,2]), ]

And I tried again

persp(data[,1],data[,2],z)

But I still have the same problem.

How can I do it ?

Thank you.

解决方案

The problem is the argument z and the ordering of both x and y. As shadow mentions in the comments this needs to be a matrix of dim(z) = length(x) x length(y) in this case 100x100. And apparently x and y need to be both ordered at the same time exactly as shadow mentions above which is seen in the source code of persp.default as.well:

if (any(diff(x) <= 0) || any(diff(y) <= 0)) #both need to be ordered
        stop("increasing 'x' and 'y' values expected")

So the following works:

lng=runif(100,1,4)
lat=runif(100,40,40.1)
z=matrix(rnorm(10000),ncol=100,nrow=100) #use the correct z as matrix
data=data.frame(lng,lat)
data=data[ order(data[,1],data[,2]), ]   #just order lng as you do 
data$lat <- sort(data$lat)               #you need to sort lat too

persp(data[,1] ,data[,2], z) 

And this works now.

Edit

I think what you really want to do is to have you lan/lng coordinates in z and plot them. The z argument needs to carry the coordinates. the x and y arguments just carry the values of the x and y axes respectively. You can use the default x and y argumens (i.e. leave them blank) or just sort lan and lng separately to provide the real values. So, the solution you are looking for is:

lng=runif(100,1,4)
lat=runif(100,40,40.1)
z= as.matrix(data.frame(lng=lng,lat=lat))
z=matrix(rnorm(10000),ncol=100,nrow=100) #z contains lan and lng

#plot z with default x and y values (or use sorted lng and lat instead)
persp( z = z)  

Which outputs:

这篇关于错误:在persp {fields}中期望增加'x'和'y'值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆