2013-06-20 101 views
11

在R中使用2d矩陣,我如何創建一個3d表面圖,其中columns = x,rows = y,值是z中的高度?2D矩陣的3D表面圖

在下面的例子中,x值爲1:5,y值爲1:5,數字代表z中的高度/值。

> m<-matrix(rnorm(25),nrow=5,ncol=5) 
> m 
      [,1]  [,2]  [,3]  [,4]  [,5] 
[1,] -1.495513045 0.8000040 -1.1499261 1.65661138 0.8140510 
[2,] -1.150018195 -0.7582933 0.8306922 0.16026908 0.3913198 
[3,] -0.852609406 0.5525621 0.3585986 -0.45054768 -1.2259927 
[4,] -0.001926297 -0.5857351 2.1518281 0.03192463 0.2065039 
[5,] -1.641128610 0.4333973 -1.0616628 -0.92143426 -0.9598991 

回答

10

rgl應該讓你開始...

require(rgl) 
# open renderer 
open3d() 
# plot surface 
rgl.surface(1:10 , 1:10 , runif(100)) 
# Export to png 
rgl.snapshot("sample.png" , fmt="png", top=TRUE) 

enter image description here

3

要獲得包含Z值二維矩陣的三維曲面圖,嘗試:

require(plot3D) 

persp3D(z = TheMatrix2D, theta = 120) 

使用theta來調整視角。隨着我的數據,我得到了以下情節:

enter image description here

+0

這似乎是更好的語法(因爲它接受一個矩陣輸入),並具有較好的默認設置。 – Paul