3D surface plot with plotly error

Hello everybody,

I am a new user of R and I want plot a 3D surface graph, but when I run my script this error message is sent :

Storing 'username' as the environment variable 'plotly_username' Error in Sys.setenv(plotly_username = username) : wrong length for argument

Here you have the script (inspirated by this tutorial : https://moderndata.plot.ly/3d-surface-plots-with-rstudio-and-plotly/) :

data_SMPS <- as.data.frame(data_SMPS)
x <- data_SMPS$total[1:100] - data_SMPS$total[1:100]
x[ ]=1
x_vec <- as.vector(x)
y <- data_SMPS$total[1:100]
y_vec <- as.vector(y)
y_vec <- t(y)

data_plot <- list(

   x = x_vec,
   y = y_vec,
   z = matrix(c(x_vec %*% y_vec) , nrow = 100, ncol = 100), type = "surface")

layout <- list(

   title = "test",
   scene = list(bgcolor = "rgb(244, 244, 248)"))

response <- plotly(data_plot)

Thank you in advance to your answers.

Yacine

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

Hi @Yacine24, that tutorial is quite old. It should really be updated to this:

library(plotly)
x_vec <- c(seq(-5, 4.9, 0.1))
x_matrix <- matrix(c(x_vec), nrow = 100, ncol = 1)
y_matrix <- matrix(c(x_vec), nrow = 1, ncol = 100)

plot_ly() %>%
  add_surface(
    x = x_vec,
    y = x_vec,
    z = matrix(c(cos(x_matrix %*% y_matrix) + sin(x_matrix %*% y_matrix)), nrow = 100, ncol = 100)
  ) %>%
  layout(
    title = "Waaaves in r",
    scene = list(bgcolor = "rgb(244, 244, 248)")
  )