简体   繁体   English

R - 补丁中的3d图

[英]3d plot in R - Patch

I have the following data in a data frame: 我在数据框中有以下数据:

**x** in (0,1)
**y** in [0,1]
**z** in [0,1]

For example: 例如:

X,Y,Z
0.1, 0.2, 0.56
0.1, 0.3, 0.57
...

I'd like to plot them on this type of chart: 我想在这种类型的图表上绘制它们: 一个3d图

I tried on R, but all I could get was a not-so-fancy 3d scatterplot . 我试过R,但我能得到的只是一个不那么花哨的3D散点图 I also read about the lattice 3d wireframe , but I couldn't get my head around it. 我还读到了格子3D线框 ,但我无法理解它。

What am I supposed to do to get a Matlab like wireframe in R? 我应该怎样做才能在R中获得类似线框的Matlab? What data transforms are involved? 涉及哪些数据转换?

This is the sample code from the documentation: 这是文档中的示例代码:

x <- seq(-pi, pi, len = 20)
y <- seq(-pi, pi, len = 20)
g <- expand.grid(x = x, y = y)
g$z <- sin(sqrt(g$x^2 + g$y^2))
wireframe(z ~ x * y, g, drape = TRUE,
aspect = c(3,1), colorkey = TRUE)

I don't find it particularly clear. 我不觉得特别清楚。

EDIT : the persp3d function works fine, and I was able to generate a 3d plot with one colour. 编辑persp3d函数工作正常,我能够生成一种颜色的3d图。 How can I set a colour scale relative to the z value? 如何设置相对于z值的色阶?

Thanks for any hints, Mulone 感谢任何提示,Mulone

Use outer to create the z values and then use persp to plot: 使用outer创建z值,然后使用persp绘制:

z <- outer(x,y, function(x,y) sin(sqrt(x^2+y^2)))
persp(x,y,z)

persp

There are options for colouring and setting the viewing angle, see ?persp . 有着色和设置视角的选项,请参阅?persp See the fourth example for Matlab style colouring. 请参阅Matlab样式着色的第四个示例。

For an interactive plot, consider using persp3d in the rgl package: 对于交互式绘图,请考虑在rgl包中使用persp3d

require(rgl)
persp3d(x,y,z,col="blue")

Edit 编辑

To add colour, there is a slight difference from the method in persp , since the colour relates to the vertex rather than the centre of the facet, but it makes it easier. 要添加颜色,与persp的方法略有不同,因为颜色与顶点有关,而不是与构面的中心有关,但它更容易。

jet.colors <- colorRampPalette( c("blue", "green") ) 
pal <- jet.colors(100)
col.ind <- cut(z,100) # colour indices of each point
persp3d(x,y,z,col=pal[col.ind])

persp3d

The help file recommends adding the parameter smooth=FALSE , but that's down to personal preference. 帮助文件建议添加参数smooth=FALSE ,但这smooth=FALSE个人偏好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM