【问题标题】:Surfaceplot with non-complete grid具有非完整网格的 Surfaceplot
【发布时间】:2018-10-19 05:41:03
【问题描述】:

我想要一个曲面图,但我的网格不完整。我搜索了,但没有成功。我怎样才能使以下工作:

x = c(10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 30L, 40L, 
     50L, 60L, 70L, 80L, 90L, 100L, 50L, 60L, 70L, 80L, 90L, 100L, 
     70L, 80L, 90L, 100L, 90L, 100L)
y = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 20L, 20L, 
     20L, 20L, 20L, 20L, 20L, 20L, 30L, 30L, 30L, 30L, 30L, 30L, 
     40L, 40L, 40L, 40L, 50L, 50L)
z = c(6.093955007, 44.329214443, 149.103755156, 351.517349974, 
     726.51174655, 1191.039562104, 1980.245204702, 2783.308022984, 
     6974.563519067, 5149.396230019, 142.236259009, 321.170609648, 
     684.959503897, 1121.475597135, 1878.334840961, 2683.116309688, 
     4159.60732066, 5294.774284119, 687.430547359, 1119.765405426, 
     1876.57337196, 2685.951176024, 3945.696884503, 5152.986796572, 
     1870.78724464, 2677.744176903, 3951.928931107, 5160.295960254, 
     3957.503273558, 5147.237754092)

# OK but not a surface plot
scatterplot3d::scatterplot3d(x, y, z,
  color = "blue", pch = 19, 
  type = "h",
  main = "",
  xlab = "x",
  ylab = "y",
  zlab = "z",
  angle = 35,
  grid = FALSE)

# Not working:
M <- plot3D::mesh(x, y, z)
R <- with (M, sqrt(x^2 + y^2 +z^2))
p <- sin(2*R)/(R+1e-3)
plot3D::slice3D(x, y, z, colvar = p,
        xs = 0, ys = c(-4, 0, 4), zs = NULL)
plot3D::isosurf3D(x, y, z, colvar = p, level = 0, col = "red")

【问题讨论】:

    标签: r plot 3d


    【解决方案1】:

    对于此类问题,我可以推荐执行“Delaunay 三角剖分和 Dirichlet tessellation”的 deldir-package。由此您可以计算出给出曲面图的空间三角形。

    rgl-package 提供了将三角形添加到散点图中的可能性。甚至更好 - 生成的情节是动画的,因此您可以旋转和缩放以获得更好的概览。

    x = c(10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 30L, 40L, 
      50L, 60L, 70L, 80L, 90L, 100L, 50L, 60L, 70L, 80L, 90L, 100L, 
      70L, 80L, 90L, 100L, 90L, 100L)
    y = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 20L, 20L, 
      20L, 20L, 20L, 20L, 20L, 20L, 30L, 30L, 30L, 30L, 30L, 30L, 
      40L, 40L, 40L, 40L, 50L, 50L)
    z = c(6.093955007, 44.329214443, 149.103755156, 351.517349974, 
      726.51174655, 1191.039562104, 1980.245204702, 2783.308022984, 
      6974.563519067, 5149.396230019, 142.236259009, 321.170609648, 
      684.959503897, 1121.475597135, 1878.334840961, 2683.116309688, 
      4159.60732066, 5294.774284119, 687.430547359, 1119.765405426, 
      1876.57337196, 2685.951176024, 3945.696884503, 5152.986796572, 
      1870.78724464, 2677.744176903, 3951.928931107, 5160.295960254, 
      3957.503273558, 5147.237754092)
    
    # create spacial triangles
     del <- deldir::deldir(x, y, z = z)
     triangs <- do.call(rbind, triang.list(del))
    # create plot 
     rgl::plot3d(x, y, z, size = 5, xlab = "my_x", ylab = "my_y", zlab = "my_z", col = "red")
     rgl::triangles3d(triangs[, c("x", "y", "z")], col = "gray")
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      这更像是一个提示,而不是一个完整的答案:

      library(plotly)
      plot_ly(z = ~volcano) %>% add_surface()
      

      是做这种情节的好方法。所以对于你的例子:

      x <- c(10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 30L, 40L, 
        50L, 60L, 70L, 80L, 90L, 100L, 50L, 60L, 70L, 80L, 90L, 100L, 
        70L, 80L, 90L, 100L, 90L, 100L)
      y <- c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 20L, 20L, 
        20L, 20L, 20L, 20L, 20L, 20L, 30L, 30L, 30L, 30L, 30L, 30L, 
        40L, 40L, 40L, 40L, 50L, 50L)
      z <- c(6.093955007, 44.329214443, 149.103755156, 351.517349974, 
        726.51174655, 1191.039562104, 1980.245204702, 2783.308022984, 
        6974.563519067, 5149.396230019, 142.236259009, 321.170609648, 
        684.959503897, 1121.475597135, 1878.334840961, 2683.116309688, 
        4159.60732066, 5294.774284119, 687.430547359, 1119.765405426, 
        1876.57337196, 2685.951176024, 3945.696884503, 5152.986796572, 
        1870.78724464, 2677.744176903, 3951.928931107, 5160.295960254, 
        3957.503273558, 5147.237754092)
      
      library(plotly)
      m <- matrix(c(x,y,z), nrow = 3)
      plot_ly(z = ~m) %>% add_surface()
      

      生产

      ..这是第一步,但是 x 轴的缩放仍然存在一些问题。我认为解决方案的关键是设置整个(稀疏)矩阵,然后绘制它。

      x <- c(10L, 20L, 30L, 40L, 50L, 60L, 70L, 80L, 90L, 100L, 30L, 40L, 
             50L, 60L, 70L, 80L, 90L, 100L, 50L, 60L, 70L, 80L, 90L, 100L, 
             70L, 80L, 90L, 100L, 90L, 100L)
      y <- c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 20L, 20L, 
             20L, 20L, 20L, 20L, 20L, 20L, 30L, 30L, 30L, 30L, 30L, 30L, 
             40L, 40L, 40L, 40L, 50L, 50L)
      z <- c(6.093955007, 44.329214443, 149.103755156, 351.517349974, 
             726.51174655, 1191.039562104, 1980.245204702, 2783.308022984, 
             6974.563519067, 5149.396230019, 142.236259009, 321.170609648, 
             684.959503897, 1121.475597135, 1878.334840961, 2683.116309688, 
             4159.60732066, 5294.774284119, 687.430547359, 1119.765405426, 
             1876.57337196, 2685.951176024, 3945.696884503, 5152.986796572, 
             1870.78724464, 2677.744176903, 3951.928931107, 5160.295960254, 
             3957.503273558, 5147.237754092)
      
      xx <- 1:100L
      yy <- 1:100L
      zz <- matrix(0, nrow = 100, ncol = 100)
      
      for (i in 1:length(x)){
        zz[x[i], y[i]] <- z[i]
      }
      
      library(plotly)
      plot_ly(z = ~zz) %>% add_surface()
      

      生产

      这基本上就是您的数据所假设的......

      希望我也能弄清楚。 希望这会有所帮助。

      【讨论】:

      • 谢谢,真的很接近。三角测量有点棘手;-) 请参阅 MichiSmith 的回答...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2021-12-11
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      相关资源
      最近更新 更多