【问题标题】:Plot Sphere with custom gridlines in R在 R 中使用自定义网格线绘制球体
【发布时间】:2019-07-15 21:58:00
【问题描述】:

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

我一直在试验 R packakge rgl 并得到了一些帮助: Plot points on a sphere in R

它以相等的纬度和长间距绘制网格线。

我有下面的函数,它返回一个点数据框,这些点是我想要的网格线的交叉点,但不知道如何继续。

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 函数很难使用。

【问题讨论】:

    标签: r plot 3d latitude-longitude rgl


    【解决方案1】:

    如果您使用lines3dplot3d(..., type="l"),您将获得一个连接数据框中点的图。要获得中断(您不希望一行很长),请添加包含 NA 值的行。

    plot_sphere 函数中的代码似乎真的很乱(你计算了两次 phi,你没有生成请求长度的向量等),但是基于它的这个函数可以工作:

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 2019-03-02
      • 2021-03-04
      • 2014-11-11
      相关资源
      最近更新 更多