在R中使用自定义网格线绘制球体

在R中使用自定义网格线绘制球体

本文介绍了在R中使用自定义网格线绘制球体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用arcos变换在R中绘制一个球体,其表面上的网格线对应于该球体的等面积网格化.

I would like to plot a sphere in R with the gridlines on the surface corresponding to the equal area gridding of the sphere using the arcos transformation.

我一直在尝试R packakge rgl,并从以下方面获得了一些帮助:在R中的球面上绘制点

I have been experimenting with the R packakge rgl and got some help from :Plot points on a sphere in R

以相等的纬度长距绘制网格线.

Which plots the gridlines with equal lat long spacing.

我具有以下函数,该函数返回点的数据框,这些点是我想要的网格线的交叉点,但不确定如何进行.

I have the below function which returns a data frame of points that are the cross over points of the grid lines I want, but not sure how to proceed.

plot_sphere <- function(theta_num,phi_num){

  theta <- seq(0,2*pi,(2*pi)/(theta_num))

  phi <-  seq(0,pi,pi/(phi_num))

  tmp <- seq(0,2*phi_num,2)/phi_num

  phi <- acos(1-tmp)

  tmp <- cbind(rep(seq(1,theta_num),each = phi_num),rep(seq(1,phi_num),times = theta_num))

  results <- as.data.frame(cbind(theta[tmp[,1]],phi[tmp[,2]]))
  names(results) <- c("theta","phi")

  results$x <- cos(results$theta)*sin(results$phi)
  results$y <- sin(results$theta)*sin(results$phi)
  results$z <- cos(results$phi)
  return(results)
}

sphere <- plot_sphere(10,10)

任何人都可以帮忙,总的来说,我发现很难使用rgl函数.

Can anyone help, in general I am finding the rgl functions tricky to work with.

推荐答案

如果使用lines3dplot3d(..., type="l"),则会得到一个将数据框中的点连接起来的图.要休息一下(您不需要长行),请添加包含NA值的行.

If you use lines3d or plot3d(..., type="l"), you'll get a plot joining the points in your dataframe. To get breaks (you don't want one long line), add rows containing NA values.

plot_sphere函数中的代码似乎真的搞砸了(您两次计算了phi,没有生成所请求长度的向量,等等),但是基于它的此函数有效:

The code in your plot_sphere function seems really messed up (you compute phi twice, you don't generate vectors of the requested length, etc.), but this function based on it works:

function(theta_num,phi_num){

  theta0 <- seq(0,2*pi, len = theta_num)

  tmp <- seq(0, 2, len = phi_num)

  phi0 <- acos(1-tmp)

  i <- seq(1, (phi_num + 1)*theta_num) - 1

  theta <- theta0[i %/% (phi_num + 1) + 1]
  phi <- phi0[i %% (phi_num + 1) + 1]

  i <- seq(1, phi_num*(theta_num + 1)) - 1
  theta <- c(theta, theta0[i %% (theta_num + 1) + 1])
  phi <- c(phi, phi0[i %/% (theta_num + 1) + 1])

  results <- data.frame( x = cos(theta)*sin(phi),
                         y = sin(theta)*sin(phi),
                         z = cos(phi))
  lines3d(results)
}

这篇关于在R中使用自定义网格线绘制球体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:36