非完整网格的曲面图

6

我想要一个表面图,但是我的网格不完整。我搜索了一下,但没有成功。如何使以下内容正常工作:

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")
2个回答

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

生成

enter image description here

...这是第一步,但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()

生成

enter image description here

这基本上就是你的数据所假设的。

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


谢谢,非常接近了。三角测量有点棘手;-) 请参考MichiSmith的答案... - Christoph

2

针对这种问题,我可以推荐使用deldir包,它可以进行“Delaunay三角剖分和Dirichlet镶嵌”。通过它,你可以计算出形成表面图的空间三角形。

rgl包可以让你将这些三角形添加到散点图中。更好的是,生成的图表是动态的,因此你可以旋转和缩放以获得更好的概览。

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

这里输入图片描述

希望这有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接