3D networks: embedding rgl in Rmarkdown

I’m usually not a fan of 3D views of networks, because unless you’ve got a VR headset, the image needs to be moving for the 3D to work visually. Otherwise you just have 2D view with lots of edges covering each other up.

However, the Carter Butts’ sna package has included gplot3d for rendering 3D views of networks using the rgl for many years. The recent release of the htmlwidgets frameworks and Duncan Murdoch’s rglwidgets package means that we can now trivially embed 3D rgl scenes in rmarkdown documents (like this one) to display in almost all (modern) web browsers.

First, load the libraries

library(sna)
library(rgl)
library(rglwidget)

Then get the network and render the scene.

# load up the sna package
data("coleman")
# open a 3d rgl device so we can set window size and zoom
open3d(antialias=1,windowRect=c(0,0,500,500),zoom=0.5)
## glX 
##   1
# use the sna gplot3d command to render network in 3d
gplot3d(coleman,edge.col='#55555555')
# save the rgl scene
rglscene<-scene3d()
# turn off the device
rgl.close()
# render the saved rgl scene as widget in the markdown page
rglwidget(rglscene)