Skip to content

Plotting a 3d convex hull

tretherington edited this page Apr 12, 2021 · 1 revision

It is possible to make some really nice 3-dimensional geometry plots with compGeometeR by making use of the rgl package. For example, this 3-dimensional convex hull

was created using the following code:

#-------------------------------------------------------------------------------

# R version 3.5.3 (2019-03-11) -- "Great Truth"
# Platform: x86_64-w64-mingw32/x64 (64-bit)

library(rgl) # version 0.100.30
library(compGeometeR) # version 1.0.0

#-------------------------------------------------------------------------------

# Generate some random data
set.seed(0) # to get the same values
x = rnorm(100)
y = rnorm(100)
z = rnorm(100)
xyz = cbind(x, y, z)

# Create convex hull
ch = convex_hull(xyz)

#-------------------------------------------------------------------------------

rgl.open() # Open a new RGL device
par3d(windowRect = c(0, 0, 600, 600)) # left, top, right, bottom of window in pixels
aspect3d(1,1,1) # Define aspect ratio as equal
rgl.bg(color = "white") # Set background colour

# Plot occurrences in climate space
rgl.spheres(x=xyz[,1], y=xyz[,2], z=xyz[,3], radius = 0.1, color = "orange")

# Plot convex hull simplices
ch_simplices = t(ch$hull_simplices)
rgl.triangles(xyz[ch_simplices, 1],
              xyz[ch_simplices, 2],
              xyz[ch_simplices, 3],
              col="dodgerblue",alpha=0.4)

#-------------------------------------------------------------------------------
Clone this wiki locally