Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surfaceplot with non-complete grid

Tags:

plot

r

3d

I want a surface plot, but my grid is incomplete. I searched, but without success. How can I make the following work:

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")
like image 981
Christoph Avatar asked May 09 '18 07:05

Christoph


2 Answers

For this kind of problem I can recommend the deldir-package which does "Delaunay triangulation and Dirichlet tessellation". From this you calculate the spacial triangles that give the surface plot.

The rgl-package gives the possibility to add the triangles to your scatterplot. And even better - the resulting plot is animated, so you can rotate and zoom for better overview.

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

enter image description here

I hope this helps.

like image 97
MichiSmith Avatar answered Nov 13 '22 07:11

MichiSmith


This is more of a hint than a complete answer:

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

is a nice way to do this kind of plots. So for your example:

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

produces

enter image description here

..this is a first step, but there's still some issues with the scaling of the x-axis. I think key to the solution is to set up the whole (sparse) matrix and then plot it.

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

produces

enter image description here

which is basically what your data supposes..

Hope I can figure that out as well. And hope this helps.

like image 39
symbolrush Avatar answered Nov 13 '22 08:11

symbolrush