我有这个数据:
wine <-read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data",sep=",") attach(wine)我试图用persp()函数提示变量V2,V3和V4的3D图
我收到此错误:
Error in persp.default(v2, v3, v4) : increasing 'x' and 'y' values expected虽然我已经使用sort()函数sort()每个变量进行了sort() 。
我该怎么办?
I have this data:
wine <-read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data",sep=",") attach(wine)and I am trying to prompt a 3D plot of the variables V2, V3 and V4 with the persp() function
I get this error:
Error in persp.default(v2, v3, v4) : increasing 'x' and 'y' values expectedAlthough I already sorted each variable with the sort() function.
How should I proceed?
最满意答案
这是一个概念上的错误。 persp用于曲面图,但您的数据仅支持散点图。
对于曲面图,我们需要在x , y展开的网格上的曲面值。 换句话说,我们在网格上绘制2D函数f(x, y) : expand.grid(x = sort(x), y = sort(y)) 。 我们需要知道这个函数f和(在几乎所有情况下)使用outer来评估它在这样的网格上。 考虑这个例子:
x <- seq(-10, 10, length = 30) ## already in increasing order y <- x ## already in increasing order f <- function(x, y) {r <- sqrt(x ^ 2 + y ^ 2); 10 * sin(r) / r} z <- outer(x, y, f) ## evaluation on grid; obtain a matrix `z` persp(x, y, z)
另一方面,散点图仅限于(x, y) :
library(scatterplot3d) scatterplot3d(V2, V3, V4) ## your `wine` data
This is some conceptual mistake. persp is used for surface plot, but your data only support scatter plot.
For a surface plot, we need the surface values on a grid expanded by x, y. In other words, we are plotting a 2D function f(x, y) on a grid: expand.grid(x = sort(x), y = sort(y)). We need to know this function f and (in almost all situation) use outer to evaluate it on such grid. Consider this example:
x <- seq(-10, 10, length = 30) ## already in increasing order y <- x ## already in increasing order f <- function(x, y) {r <- sqrt(x ^ 2 + y ^ 2); 10 * sin(r) / r} z <- outer(x, y, f) ## evaluation on grid; obtain a matrix `z` persp(x, y, z)Scatter plot on the other hand, is only restricted to (x, y):
library(scatterplot3d) scatterplot3d(V2, V3, V4) ## your `wine` data更多推荐
发布评论