Black Lives Matter. Please consider donating to Black Girls Code today.
Dash Enterprise delivers an incredible 21x cost savings 💸Download the e-book!

Can't get a Surface plot using cartesian (xyz) coordinates using plot_ly

Hi,

I’m having problems getting plot_ly to display a surface plot when the data input as cartesian (xyz) coordinates, rather than the z/height as a grid/matrix.

According to the documentation for both surface and mesh3D the z/height data can be input as a ‘dataframe column, list, vector’, KEY point here is that it doesn’t have to be a matrix (see below). I can get a meshplot using mesh3D, however when I try the same code and only change type=mesh3d to type=surface something quite weird happens. When using a notebook in RStudio sometimes I get a plot with nothing in it, and sometimes a space is created for the plot but nothing appears at all. I am not getting any error messages at all (unlike add_surface which tells me Error: z must be a numeric matrix)

FROM DOCUMENTATION https://plot.ly/r/reference/#surface.

z (dataframe column, list, vector)
Sets the z coordinates.
x (dataframe column, list, vector)
Sets the x coordinates.
y (dataframe column, list, vector)
Sets the y coordinates.

Here is some code that reproduces the problem

library(plotly)

x <- runif(50, 0, 1)
y <- runif(50, 0, 1)
z <- runif(50, 0, 1)

works

plot_ly(x = ~x, y = ~y, z = ~z, type = ‘mesh3d’)

Doesn’t work. r this gives us the plot area, but no surface!!

plot_ly(x = ~x, y = ~y, z = ~z, type = ‘surface’)

And this code doesn’t even give the plot area

library(MASS)
topo.mar <- list(x=seq(0, 6.5, 0.2), y=seq(0, 6.5, 0.2))
topo.plt <- expand.grid(topo.mar)
topo.loess <- loess(z~x*y, topo, degree=2,span=0.25)
topo.plt$pred <- as.vector(predict(topo.loess, topo.plt))

This works

plot_ly() %>%
add_trace(data = topo.plt, x=topo.plt$x, y=topo.plt$y, z=topo.plt$pred, type=“mesh3d”)

This doesn’t and it wont even give the plot area

plot_ly() %>%
add_trace(data = topo.plt, x=topo.plt$x, y=topo.plt$y, z=topo.plt$pred, type=“surface”)