3D绘图中的R - 补丁 [英] 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]
例如:
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:
我尝试过R,但所有我能得到的是href="http://www.statmethods.net/graphs/images/s3d2.png" rel="nofollow">不那么花哨的3D一。 我也看了一下格三维线框,但我不能让我的头周围。
I tried on R, but all I could get was a not-so-fancy 3d scatterplot. I also read about the lattice 3d wireframe, but I couldn't get my head around it.
我该怎么做才能得到一个像Matlab的线框在研发? 涉及哪些内容转换?
What am I supposed to do to get a Matlab like wireframe in R? What data transforms are involved?
这是从文档样本$ C $ C:
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.
修改:在 persp3d
功能正常工作,我是能够生成3D绘图用一种颜色。如何设置一个色阶相对于z值?
EDIT: the persp3d
function works fine, and I was able to generate a 3d plot with one colour. How can I set a colour scale relative to the z value?
感谢您的任何提示, Mulone
Thanks for any hints, Mulone
推荐答案
使用外
来创建z值,然后使用 persp
来图:
Use outer
to create the z values and then use persp
to plot:
z <- outer(x,y, function(x,y) sin(sqrt(x^2+y^2)))
persp(x,y,z)
有选择着色和设置的视角,看到?persp
。见第四例子Matlab的风格色彩。
There are options for colouring and setting the viewing angle, see ?persp
. See the fourth example for Matlab style colouring.
有关的互动情节,可以考虑使用 persp3d
在 RGL
包:
For an interactive plot, consider using persp3d
in the rgl
package:
require(rgl)
persp3d(x,y,z,col="blue")
修改
要添加颜色,有从方法中 persp
A略有差异,由于彩色涉及顶点而不是小面的中心,但它使更容易。
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.
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])
帮助文件建议添加参数光滑= FALSE
,但这是看个人的preference。
The help file recommends adding the parameter smooth=FALSE
, but that's down to personal preference.
这篇关于3D绘图中的R - 补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!