Facecolor colourscale in mesh3D R plotly


Does anyone know how to put a colour bar on a 3D plot, using Mesh 3D, based on the colour intensity of the facecolour asthetic?

Here is my code

p1 <- plot_ly(
x = points[, 1], y = points[, 2], z = points[, 3],

i = faces[, 1]-1, j = faces[, 2]-1, k = faces[, 3]-1,

type = “mesh3d”,

facecolor = face$colour,


where facecolour is the colour of each triangle based on something measured at each vertice (but not the actual x,y or z vertice itself)

face$colour[zmean<= 0.5] <- "#800026"
face$colour[zmean> 0.5 & zmean<=0.6] <- "#bd0026"
face$colour[zmean> 0.6 & zmean<=0.7] <- "#e31a1c"
face$colour[zmean> 0.7 & zmean<=0.8] <- "#fc4e2a"
face$colour[zmean> 0.8 & zmean<=0.9] <- "#fd8d3c"
face$colour[zmean> 0.9 & zmean<=1] <- "#feb24c"
face$colour[zmean> 1 & zmean<=1.1] <- "#ffffe5"
face$colour[zmean> 1.1 & zmean<=1.2] <- "#d9f0a3"
face$colour[zmean> 1.2 & zmean<=1.3] <- "#addd8e"
face$colour[zmean> 1.3 & zmean<=1.4] <- "#78c679"
face$colour[zmean> 1.4 & zmean<=1.5] <- "#41ab5d"
face$colour[zmean> 1.5 & zmean<=1.6] <- "#238443"
face$colour[zmean> 1.6 & zmean<=1.8] <- "#41b6c4"
face$colour[zmean> 1.8 & zmean<=2] <- "#41b6c4"
face$colour[zmean> 2 & zmean<=2.5] <- "#225ea8"
face$colour[zmean> 2.5] <- “#081d58

heres the plot if that helps



i was unable to apply a colour scale bar/legend to the mesh3d file. i think it may be somethign to do with a bug in the system

however, my workaround was to draw a seperate graph of the points in th emesh using scatter3d for which the colorbar worked

x = points[, 1], y = points[, 2], z = points[, 3],

marker = list(
autocolorscale = FALSE,
cmax = 2.5,
cmin = 0,
color = vert$colour,
#colorscale = vert$bi,
colorbar = “middle”,
colorscale = list(c(0, “#800026”),
list(0.2, “#bd0026”),
list(0.24, “#e31a1c”),
list(0.28, “#fc4e2a”),
list(0.32, “#fd8d3c”),
list(0.36, “#feb24c”),
list(0.4, “#ffffe5”),
list(0.44, “#d9f0a3”),
list(0.48, “#addd8e”),
list(0.52, “#78c679”),
list(0.56, “#41ab5d”),
list(0.6, “#238443”),
list(0.64, “#41b6c4”),
list(0.8, “#225ea8”),
list(1, “#081d58”)),
line = list(width = 0),
opacity = 0.9,
size = 20,
symbol = “circle”,

opacity = 0.5,
mode= “markers”,

here is the code for those interested.

i will cut the colorbar out of this graph and past it into the figure for the mesh .

hope this helps.

interested to know if anyine else can add this colorbar to the mesh 3d graph

i asked plotly team but they couldn’t get a workaround and suggested using this forum.


Color bars in mesh3d are only supported when the mesh colors are specified using intensity. Other color specifications e.g. facecolors, vertexcolors and color don’t support colorscale - and for good reasons: as the colors are specified per datum not via a mapping.

I have to say there is no reason to have this setting. You should be able to set the colour bar based on the face colours.

Hence my workaround of a 3D scatter plot on the plot with is set to opacity 0 and then provide the colour bar.

I think they should enable you to set the colour bar legend based on anything you want