rplotly

Incorrect z-values in plotly


I want to plot a surface $f(x,y) = x^2+2x-y$ and its tangent plane at point $(0,3,-3)$ but the code does not give the correct plot.

library(plotly)

x <- seq(from = -3, to = 3, length.out = 100)
y <- seq(from = -2, to = 4, length.out = 100)
one <- rep(1, 100)

z <- x^2 %o% one + 2*x %o% one - one %o% y
z1 <- 2*x %o% one - one %o% y 

fig <- plot_ly(showscale = FALSE) %>%
  add_surface(z = z, x = x, y = y
  ) %>%
  add_surface(z = z1, x = x, y = y) %>%
  add_markers(x = 0, y = 3, z = -3, 
              marker = list(
                color = "red",
                size = 5))
fig <- fig %>% layout(
  scene = list(
    camera=list(
      eye = list(x=1.87, y=0.88, z=-0.64)
    )
  )
)

fig

For example, the point (0,3,-3) does not even lie on either of the surface! Please helpenter image description here


Solution

  • You need to pass x, y, and z data of the same size. Use the same trick with one for the x and y:

    add_surface(z = z,  x = x %o% one, y = one %o% y) %>%
    add_surface(z = z1, x = x %o% one, y = one %o% y)