2014-02-13 100 views
1

如何绘制R中的球坐标(r,theta和phi)?如何绘制R中的球坐标?

是否可以使用persp()函数?

我有点以及网格的集合。

+0

我做了一些谷歌进行搜索,发现没有R代码。但是,我确实找到了显示如何在系统间转换的页面。也许你可以使用这些公式来转换你的数据,并用标准的3D绘图代码来绘制:http://tutorial.math.lamar.edu/Classes/CalcIII/SphericalCoords.aspx –

+2

有一些函数'cart2sph','sph2cart' ,'cart2pol'和'pol2cart'包装在'pracma'中,它们将为您执行这些转换。 –

回答

3

只要你知道your math,这是一个相当琐碎的转变:

spher_to_cart <- function(r, theta, phi) list(x=r*cos(phi)*sin(theta), 
               y=r*sin(theta)*sin(phi), 
               z=r*cos(theta)) 

#An example dataset 
data <- data.frame(r=1:10, 
        theta = seq(0,2*pi,length=10), 
        phi = seq(2*pi, 0,length=10)) 
spher_to_cart(data$r, data$theta, data$phi) 
$x 
[1] 0.000000e+00 9.848078e-01 5.130302e-01 -1.732051e+00 -1.606969e+00 1.928363e+00 3.031089e+00 -1.368081e+00 -4.431635e+00 -2.449294e-15 

$y 
[1] 0.0000000 -0.8263518 -2.9095389 -3.0000000 -0.5848889 -0.7018667 -5.2500000 -7.7587705 -3.7185832 0.0000000 

$z 
[1] 1.0000000 1.5320889 0.5209445 -2.0000000 -4.6984631 -5.6381557 -3.5000000 1.3891854 6.8944000 10.0000000 

小心使用theta和披值的弧度,不度。
然后你可以用绘制为plot3drgl例如:

s <- spher_to_cart(data$r, data$theta, data$phi) 
library(rgl) 
plot3d(s$x,s$y,s$z) 
+0

但是,为什么不使用'pracma'函数呢?他们有一些错误检查的东西,这往往是有帮助的。 –

+2

@CarlWitthoft为什么不当然。这很简单,我只是喜欢从头开始做事。 – plannapus

+0

事实上,对于某人(wink wink)来说,为了完整起见,将“pracma”解决方案添加为另一个答案并不是一个坏主意。 – plannapus