r - 如何在R中绘制球坐标

标签 r plot

如何在 R 中绘制球面坐标(r、theta 和 phi)?

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

我有一组点和一个网格。

最佳答案

只要你知道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 和 phi 值。
然后,您可以使用 plot3d 为包 rgl 进行绘图,例如:

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

关于r - 如何在R中绘制球坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21749151/

相关文章:

r - 更改 R 中 rCharts sankey 图中节点的颜色

python - 当我使用第三个变量定义颜色制作 python 散点颜色图时没有颜色

r - R 绘图图例行为中的换行

matlab - 带动画的实时脚本

r - 将(df 的)每一列的唯一值存储在列表中

r - ggplot facets 中的单独排序

在 R 中为多个数据帧运行 for 循环?

R按组无监督聚类(?)

python - 如何阻止显示分辨率影响 pyqtgraph 图中的轴

python - 当鼠标悬停时使用 matplotlib 注释绘图的线条