I have plotted a mesh in rgl to visualize data on it. I.e., the mesh has colors that originate from applying a colormap to its data (one scalar value at each vertex). Here is a minimal example that consists of a mesh with a single face:
library(rgl);
library(squash);
# create a mesh
vertices <- c(
-1.0, -1.0, 0, 1.0,
1.0, -1.0, 0, 1.0,
1.0, 1.0, 0, 1.0,
-1.0, 1.0, 0, 1.0
)
indices <- c( 1, 2, 3, 4 )
# add a data value for each vertex
morph_data = rnorm(length(indices), mean = 3, sd = 1)
# create colors from values by applying a colormap
col = squash::cmap(morph_data, map = squash::makecmap(morph_data, colFn = squash::jet));
# plot
open3d()
shade3d( qmesh3d(vertices, indices), col=col )
How can I add a colorbar to this plot in rgl?
An example for what exactly I mean with colorbar is shown in the right part of this example picture from octave.sourceforge.io.
You can use bgplot3d()
to draw any sort of 2D plot in the background of an rgl
plot. There are lots of different implementations of colorbars around; see Colorbar from custom colorRampPalette for a discussion. The last post in that thread was in 2014, so there may be newer solutions.
For example, using the fields::image.plot
function, you can put this after your plot:
bgplot3d(fields::image.plot(legend.only = TRUE, zlim = range(morph_data), col = col) )
A documented disadvantage of this approach is that the window doesn't resize nicely; you should set your window size first, then add the colorbar. You'll also want to work on your definition of col
to get more than 4 colors to show up if you do use image.plot
.