在R的球面上绘制点

时间:2022-12-07 00:21:42

Could you help me to make a plot similar to this in R?

你能帮我在R中画一个类似的图吗?

在R的球面上绘制点

I would like to have it interactive such that I could rotate the sphere. I guess I should use rgl. I found an example similar to what I need here, however I couldn't find a way to draw a grid instead of a filled sphere.

我想让它具有互动性,这样我就可以旋转球体。我想我应该用rgl。我发现了一个类似于我在这里需要的例子,但是我找不到一个方法来画一个网格而不是一个填充的球体。

UPD: A reproducible dataset that could help answering the question (I took it from here):

一个可复制的数据集可以帮助回答这个问题(我从这里开始):

u <- runif(1000,0,1) 
v <- runif(1000,0,1) 
theta <- 2 * pi * u 
phi   <- acos(2 * v - 1) 
x <- sin(theta) * cos(phi) 
y <- sin(theta) * sin(phi) 
z <- cos(theta) 
library("lattice") 
cloud(z ~ x + y)

1 个解决方案

#1


17  

Start with

开始

library("rgl")
spheres3d(0,0,0,lit=FALSE,color="white")
spheres3d(0,0,0,radius=1.01,lit=FALSE,color="black",front="lines")

to create a "wireframe" sphere (I'm cheating a little bit here by drawing two spheres, one a little bit larger than the other ... there may be a better way to do this, but I couldn't easily/quickly figure it out).

为了创建一个“线框图”球体(我在这里画了两个球体,一个比另一个大一点……)也许有更好的方法来做这件事,但是我不能轻易/快速地搞清楚)。

from the Wolfram web page on sphere point picking (the source of your picture) we get

我们从Wolfram的球面点选择(图片来源)网页上得到

Similarly, we can pick u=cos(phi) to be uniformly distributed (so we have du=sin phi dphi) and obtain the points x = sqrt(1-u^2)*cos(theta); y = sqrt(1-u^2)*sin(theta); z=u with theta in [0,2pi) and u in [-1,1], which are also uniformly distributed over S^2.

同样的,我们可以选择u = cos(φ)均匀分布(我们有du = sinφdphi)和获得点x =√6(1 u ^ 2)* cos(θ);y =√1 u ^ 2)*罪(θ);z = u与θ(0,2π)和u[1],也均匀分布/ S ^ 2。

So:

所以:

set.seed(101)
n <- 50
theta <- runif(n,0,2*pi)
u <- runif(n,-1,1)
x <- sqrt(1-u^2)*cos(theta)
y <- sqrt(1-u^2)*sin(theta)
z <- u
spheres3d(x,y,z,col="red",radius=0.02)

The spheres take a little more effort to render but are prettier than the results of points3d() (flat squares) ...

球体需要付出更多的努力来渲染,但比points3d()(平面正方形)的结果更漂亮……

在R的球面上绘制点

#1


17  

Start with

开始

library("rgl")
spheres3d(0,0,0,lit=FALSE,color="white")
spheres3d(0,0,0,radius=1.01,lit=FALSE,color="black",front="lines")

to create a "wireframe" sphere (I'm cheating a little bit here by drawing two spheres, one a little bit larger than the other ... there may be a better way to do this, but I couldn't easily/quickly figure it out).

为了创建一个“线框图”球体(我在这里画了两个球体,一个比另一个大一点……)也许有更好的方法来做这件事,但是我不能轻易/快速地搞清楚)。

from the Wolfram web page on sphere point picking (the source of your picture) we get

我们从Wolfram的球面点选择(图片来源)网页上得到

Similarly, we can pick u=cos(phi) to be uniformly distributed (so we have du=sin phi dphi) and obtain the points x = sqrt(1-u^2)*cos(theta); y = sqrt(1-u^2)*sin(theta); z=u with theta in [0,2pi) and u in [-1,1], which are also uniformly distributed over S^2.

同样的,我们可以选择u = cos(φ)均匀分布(我们有du = sinφdphi)和获得点x =√6(1 u ^ 2)* cos(θ);y =√1 u ^ 2)*罪(θ);z = u与θ(0,2π)和u[1],也均匀分布/ S ^ 2。

So:

所以:

set.seed(101)
n <- 50
theta <- runif(n,0,2*pi)
u <- runif(n,-1,1)
x <- sqrt(1-u^2)*cos(theta)
y <- sqrt(1-u^2)*sin(theta)
z <- u
spheres3d(x,y,z,col="red",radius=0.02)

The spheres take a little more effort to render but are prettier than the results of points3d() (flat squares) ...

球体需要付出更多的努力来渲染,但比points3d()(平面正方形)的结果更漂亮……

在R的球面上绘制点