RasterVis R – How to Add Line Shapefile to 3D Raster Plot

linerraster

I have generated a 3D raster plot using the plot3D function in the rasterVis package in R.

For enhanced data visualisation, I want to add a line shapefile, representing a sampling transect, to the 3D plot.

Does anyone know how to do this in R?

enter image description here

Best Answer

Use lines3d from the rgl package but make sure to plot the raster with adjust=FALSE or the axes won't line up.

Where r is a raster and xyz is a data frame with x, y, z (height) locations, I do:

plot3D(r, col = rainbow, adjust=FALSE)
lines3d(xyz)

and get

enter image description here

You can get the heights of points from the raster using extract if you don't have them already, and you might want to add a little offset to the heights to keep them just clear of the terrain, so they don't get clipped by any interpolation. You can see my line gets a bit clipped because it's a rough transect with only ten points.

You can use any rgl package functions to augment that plot, such as axis3d("x") to show the x-axis.