r - 如何在 R 中绘制完美的圆形球体 (rgl.spheres)

标签 r rgl

R 具有绘制球体的强大功能(见下文)。但是,球体不是圆的,但在放大时看起来很尖锐。如何绘制一个完美的圆球体?谢谢!干杯,克里斯

library(rgl)

rgl.spheres(1,1,1,radius=1,color="blue")

最佳答案

下面的函数是 rgl.spheres 的模仿。 ng 是球体上的网格数。如果你增加 ng,你会得到更圆的球体。

rgl.sphere <- function (x, y=NULL, z=NULL, ng=50, radius = 1, color="white", add=F, ...) {
  lat <- matrix(seq(90, -90, len = ng)*pi/180, ng, ng, byrow = TRUE)
  long <- matrix(seq(-180, 180, len = ng)*pi/180, ng, ng)

  vertex  <- rgl:::rgl.vertex(x, y, z)
  nvertex <- rgl:::rgl.nvertex(vertex)
  radius  <- rbind(vertex, rgl:::rgl.attr(radius, nvertex))[4,]
  color  <- rbind(vertex, rgl:::rgl.attr(color, nvertex))[4,]

  for(i in 1:nvertex) {
    add2 <- if(!add) i>1 else T
    x <- vertex[1,i] + radius[i]*cos(lat)*cos(long)
    y <- vertex[2,i] + radius[i]*cos(lat)*sin(long)
    z <- vertex[3,i] + radius[i]*sin(lat)
    persp3d(x, y, z, specular="white", add=add2, color=color[i], ...)
  }
}

测试功能:

rgl.sphere(rnorm(10), rnorm(10), rnorm(10), radius = runif(10), color = rainbow(10))

关于r - 如何在 R 中绘制完美的圆形球体 (rgl.spheres),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30627647/

相关文章:

r - 我可以按字符串/值的最后一部分对列(字符)进行排序吗?

r - 如何查找两个数据帧之间不相同的值的数量

r - 具有 xyz 坐标的 3d 曲面图

R 透明平面 3d 不应隐藏 plot3d 点/球体

r - 仅在指定因素之间连接 geom_line

r - 如何使用 distHaversine 函数?

R:rgl 3D 绘制边界框面颜色和描边

r - 如何每隔一行将行添加到R数据框中?

r - 您必须启用 Javascript 才能正确查看此页面。错误 knitr rgl

r - 我可以将 glmnet 与插入符号和稀疏矩阵一起使用吗?