在 R 中的球体上绘制点
Plot points on a sphere in R
你能帮我在 R 中制作一个类似这样的图吗?
我想让它具有交互性,这样我就可以旋转球体。我想我应该使用 rgl
。我找到了一个类似于我需要的示例 here,但是我找不到绘制网格而不是填充球体的方法。
UPD: 可以帮助回答问题的可重现数据集(我从 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)
开始于
library("rgl")
spheres3d(0,0,0,lit=FALSE,color="white")
spheres3d(0,0,0,radius=1.01,lit=FALSE,color="black",front="lines")
创建一个 "wireframe" 球体(我在这里有点作弊,画了两个球体,一个比另一个大一点......可能有更好的方法来做到这一点,但是我无法 easily/quickly 搞清楚)。
从 Wolfram web page on sphere point picking(您图片的来源)我们得到
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.
所以:
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)
球体需要更多的努力来渲染,但比 points3d()
(扁平正方形)的结果更漂亮...
最近徘徊,我可能建议查看包 sphereplot
,如果您真的很勇敢,gensphere
用于 3-[=18= 中高度可配置的一般点放置].
sphereplot
包括简单的功能,例如(从手册页引用)
pointsphere Random sphere pointing
Description Randomly generates data
points within a sphere that are uniformly distributed.
Usage
pointsphere(N = 100, longlim = c(0, 360), latlim = c(-90, 90), rlim =
c(0, 1))
Arguments
N Number of random points.
longlim Limits of longitude in degrees.
latlim Limits of latitude in degrees.
rlim Limits of radius.
你能帮我在 R 中制作一个类似这样的图吗?
我想让它具有交互性,这样我就可以旋转球体。我想我应该使用 rgl
。我找到了一个类似于我需要的示例 here,但是我找不到绘制网格而不是填充球体的方法。
UPD: 可以帮助回答问题的可重现数据集(我从 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)
开始于
library("rgl")
spheres3d(0,0,0,lit=FALSE,color="white")
spheres3d(0,0,0,radius=1.01,lit=FALSE,color="black",front="lines")
创建一个 "wireframe" 球体(我在这里有点作弊,画了两个球体,一个比另一个大一点......可能有更好的方法来做到这一点,但是我无法 easily/quickly 搞清楚)。
从 Wolfram web page on sphere point picking(您图片的来源)我们得到
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.
所以:
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)
球体需要更多的努力来渲染,但比 points3d()
(扁平正方形)的结果更漂亮...
最近徘徊,我可能建议查看包 sphereplot
,如果您真的很勇敢,gensphere
用于 3-[=18= 中高度可配置的一般点放置].
sphereplot
包括简单的功能,例如(从手册页引用)
pointsphere Random sphere pointing
Description Randomly generates data
points within a sphere that are uniformly distributed.
Usage
pointsphere(N = 100, longlim = c(0, 360), latlim = c(-90, 90), rlim = c(0, 1))
Arguments N Number of random points.
longlim Limits of longitude in degrees.
latlim Limits of latitude in degrees.
rlim Limits of radius.