r - 预期增加 'x' 和 'y' 值

标签 r

我想用这段代码做一个非常简单的 3D 绘图:

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")

但我收到此错误:
Error in persp.default(x, y, z, theta = 30, phi = 30,  : 
  increasing 'x' and 'y' values expected

我应该怎么做才能解决这个问题?

更新
如果我改变我的数据。例如对于 x
test<-c(0.4118836, 0.3481329, 0.3582897, 0.3122690, 0.3346040, 0.2409363, 0.2474494, 0.1744042, 0.1589965, 0.2435564)

进而
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")

我收到同样的错误

最佳答案

两个xy正如错误消息所说,应该增加。试试 y <- 1:10 .

此外,z也可能有问题。我们读到 ?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.



创建z 的简单方法是通过调用 outer .

尝试例如
x <- 1:10
y <- 1:10
z <- outer(x, y, function(xi, yj) xi^2+yj^2)
persp(x, y, z)

关于r - 预期增加 'x' 和 'y' 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23651195/

相关文章:

r - 在 R 中合并多个数据帧

按行平均超过增加号。在 mutate : dplyr R 内使用 for 循环的列数

r - 从 `sf` 对象中提取没有几何形状的数据,例如 `sp@data`

r - 获取 R igraph 中最大的连通分量

R 编程 : cache the inverse of a matrix

r - Packrat 与本地二进制存储库

r - 另存为网页时如何向 networkD3 可视化添加标题?

r - 如何使用lapply函数在R中的矩阵中进行跨列的逻辑运算?

R 将列转换为 NA 值 - 为什么?

r - 使用 read.csv() 时保持前导 0