如何使用带有R的persp()来绘制数据集中的三个变量(How to use persp() with R to graph three variables in a dataset)

编程入门 行业动态 更新时间:2024-10-24 05:23:08
如何使用带有R的persp()来绘制数据集中的三个变量(How to use persp() with R to graph three variables in a dataset)

我有这个数据:

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 expected

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

enter image description here

Scatter plot on the other hand, is only restricted to (x, y):

library(scatterplot3d) scatterplot3d(V2, V3, V4) ## your `wine` data

enter image description here

更多推荐

本文发布于:2023-07-19 09:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1176921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   变量   数据   persp   dataset

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!