Black Lives Matter. Please consider donating to Black Girls Code today.

Problem setting surfacecolor in 3D surface plots

I’m having some problems setting the surfacecolor in a 3D surface plot to be different from the z values. I’d like to use the surface plot to effectively show 4 dimensions: x, y, z, and the color.

The color should be mapped to the absolute value of z, meaning the peaks (both at positive and negative z) will have the highest color value.

Can anyone provide some help?

Here is a example of some code.:

library(plotly)
n=50
x = seq(from=1, to=20, length.out = n)
y = seq(from=30, to=40, length.out = n)

z = matrix(0, ncol=n, nrow=n)

for (i in 1:n) {
for (j in 1:n) {
z[i,j] = sin(x[i])*cos(y[j])

colorMat[i,j] = abs(z[i,j])

}
}

plot_ly(z=z, x=x, y=y, type = “surface”,
surfacecolor=colorMat
)

Is there a bug in the API for R?
surfacecolor works in JavaScript according to this demo.