Customizing Plotly Surfaces

I am trying to follow the instructions from the official plotly website (3D Surface Plots | R | Plotly) and make this kind of graph:

library(plotly)

x = c(1,2,3,4,5)
y = c(1,2,3,4,5)
z = rbind(
  c(0, 1, 0, 1, 0),
  c(1, 0, 1, 0, 1),
  c(0, 1, 0, 1, 0),
  c(1, 0, 1, 0, 1),
  c(0, 1, 0, 1, 0))


library(plotly)
fig <- plot_ly(
  type = 'surface',
  contours = list(
    x = list(show = TRUE, start = 1.5, end = 2, size = 0.04, color = 'white'),
    z = list(show = TRUE, start = 0.5, end = 0.8, size = 0.05)),
  x = ~x,
  y = ~y,
  z = ~z)
fig <- fig %>% layout(
    scene = list(
      xaxis = list(nticks = 20),
      zaxis = list(nticks = 4),
      camera = list(eye = list(x = 0, y = -1, z = 0.5)),
      aspectratio = list(x = .9, y = .8, z = 0.2)))

fig

Suppose I have my own data over here - I figured out how to make a standard plotly plot:

library(plotly)

#plot1
X1 <- seq(0,100,1)
Y1 <- seq(0,100,1)
DF <- expand.grid(X1,Y1)
DF$Z1 <- sin(DF$Var1) + cos(DF$Var2)
Z1 <- matrix(DF$Z1, nrow = 100)
plot_ly(y = ~Y1, x = ~X1, z=~Z1) %>% add_surface() 

Can someone please show me how to adapt this code to make it look like the first example?

Thanks

It is not clear what features of the first plot you want to include. I added contours to your plot and greatly reduced the number of peaks so that the contours would be visible.

X1 <- seq(0,4*pi,pi/10)
Y1 <- seq(0,4*pi,pi/10)
DF <- expand.grid(X1,Y1)
DF$Z1 <- sin(DF$Var1) + cos(DF$Var2)
Z1 <- matrix(DF$Z1, nrow = 41)
plot_ly(y = ~Y1, x = ~X1, z=~Z1,
        contours = list(
          x = list(show = TRUE, start = 1, end = 4, size = 0.5, color = 'white'),
          z = list(show = TRUE, start = 0.0, end = 1.8, size = 0.3))) %>% 
  add_surface()
1 Like

Thank you! This is perfect! I will remember to use the "expand.grid" statement when working with plotly.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.