【问题标题】:How to use persp() with R to graph three variables in a dataset如何使用 persp() 和 R 来绘制数据集中的三个变量
【发布时间】:2017-03-14 13:10:57
【问题描述】:

我有这些数据:

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() 函数对每个变量进行了排序。

我应该如何进行?

【问题讨论】:

    标签: r plot visualization


    【解决方案1】:

    根据哲元的回复,persp 不是 3d 散点图的正确选择,您可以使用 rgl 代替您的葡萄酒数据:

    library(rgl)
    plot3d(wine$V1, wine$V2, wine$V3, type='s', size=2, col=wine$V1)
    

    【讨论】:

      【解决方案2】:

      这是一些概念上的错误。 persp 用于曲面图,但您的数据仅支持散点图。

      对于曲面图,我们需要由xy 扩展的网格上的曲面值。换句话说,我们在网格上绘制了一个二维函数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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        • 2018-11-04
        相关资源
        最近更新 更多