在R中绘制线框和带有格子的云

在R中绘制线框和带有格子的云

本文介绍了在R中绘制线框和带有格子的云的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很好的曲面,它表示具有两个自变量的回归的非线性多部分回归结果.我想将回归预测值绘制为漂亮的3D表面,然后将实际值显示为围绕该表面反弹的点.这将是绘制回归线并将实际值显示为围绕该线的点的3D版本.我不知道如何用格来做到这一点.我很高兴在R中使用另一个图形库,但是我不知道其他人可以进行3D绘图.

I have a nice surface that represents nonlinear multi-part regression results on a regression with two independent variables. I would like to plot the regression predicted values as a nice 3D surface and then show the actual values as point that bounce around the surface. This would be the 3D version of plotting a regression line and showing the actuals as points around the line. I can't figure out how to do this with lattice. I'm happy to use another graphing library in R, but I don't know of others that do 3D plots.

这是我想做的事情的简化版本:

Here's a simplified version of what I want to do:

library(lattice)
#set up some simplified data
x <- seq(-.8, .8, .1)
y <- seq(-.8, .8, .1)
myGrid <- data.frame(expand.grid(x,y))
colnames(myGrid) <- c("x","y")
myGrid$z <- myGrid$x + myGrid$y
noise <- rnorm(length(myGrid$z),.3,.2)
myGrid$z2 <- myGrid$x + myGrid$y + noise

z是我的光滑表面,z2是我的嘈杂点,大部分位于表面上方.所以表面看起来像这样:

z is my smooth surface and z2 are my noisy points mostly slightly above the surface. So the surface looks like this:

wireframe(myGrid$z ~ myGrid$x * myGrid$y, xlab="X", ylab="Y", zlab="Z")

点云如下所示:

cloud(myGrid$z2 ~ myGrid$x * myGrid$y, xlab="X", ylab="Y", zlab="Z")

是否可以将两个都放在一个格子面板中?

Is it possible to get both of these in one lattice panel?

推荐答案

我确实喜欢rgl!但是有时候,点阵中的3-D图也很有用-您可以编写自己的函数,然后将其传递给点阵函数的'panel'参数.例如,

I do love rgl! But there are times when 3-D plots in lattice are useful too - you can write your own function which you can pass to the 'panel' argument to lattice functions. For instance,

mypanel <- function(x,y,z,...) {
  panel.wireframe(x,y,z,...)
  panel.cloud(x,y,z,...)
}
wireframe(myGrid$z ~ myGrid$x * myGrid$y, xlab="X", ylab="Y", zlab="Z",
          panel=mypanel)

您调用的最后一个函数可以是wireframe()或cloud();在这两种情况下,由于在panel函数中都调用panel.wireframe()和panel.cloud(),结果应该是相同的.

The last function you call can be wireframe() or cloud(); in either case since panel.wireframe() and panel.cloud() are called within the panel function the result should be the same.

感谢您指出,亚伦,那么您可以将z2作为另一个变量传递:

Thanks for pointing that out, Aaron, then probably you can pass z2 as another variable:

mypanel <- function(x,y,z,z2,...) {
  panel.wireframe(x,y,z,...)
  panel.cloud(x,y,z2,...)
}
wireframe(z ~ x * y, data=myGrid, xlab="X", ylab="Y", zlab="Z",
          panel=mypanel, z2=myGrid$z2)

这篇关于在R中绘制线框和带有格子的云的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:03