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

Need help with 3d Surface plot in R using plotly

My data is in format like show in the link.

dt <- read.csv(‘https://raw.githubusercontent.com/plotly/datasets/master/3d-line1.csv’)

I followed the tutorials in the link http://moderndata.plot.ly/3d-surface-plots-with-rstudio-and-plotly/ and also referred to other plotly r examples of 3d surface plots. I am sure there is something I am missing on how to use the z axis data before plotting it, may be it needs further modification or something.

This is what I tried,
x_vec <- matrix(dt$x, nrow = 601, ncol = 1)
y_vec <- matrix(dt$y, nrow = 1, ncol = 601)
z_vec <- matrix(dt$z, nrow = 601, ncol = 601)
plot_ly(x = x_vec, y = y_vec, z = z_vec, type = ‘surface’)

But it resulted in an empty plot.

I got the same with that code! Maybe we need to do standard deviations? Hence the zero-centered axes?

I don’t think so…I followed the volcano tutorial on plotly site and was able to get this for my data using the MASS package which uses kde2d algo.
But the z values that I get for my data using the MASS:kde2d() function are then not the same as the initial dataset and are altered.
The 2nd plot is what I expected (it is generated in JMP and shows quite exact z values on the plot as they are in the dataset)

So the problem is a bit fundamental about how one should calculate z axis for 3d plotting.

kd <- with(subdt, MASS::kde2d(WaferPointX, WaferPointY, Value,n=500))
plot_ly(x = kd$x, y = kd$y, z = kd$z) %>% add_surface()

3d Plot of same data generated using JMP

@Nushiamme I was able to get something close to what I wanted. My dataset is large so the plot is based on just 5% of randomly sampled rows.

Seems I just had to do interpolation so this worked for me:

dens <- interp(subdt$WaferPointX, subdt$WaferPointY, subdt$Value, duplicate = “mean”)
plot_ly(x = dens$x, y = dens$y, z = dens$z) %>% add_surface()