Warning message: data length is not a sub-multiple or multiple of the number of rows

Can someone please help me understand why the following code runs, produces a graph, but with warning messages:

library(plotly)
library(dplyr)

X <- seq(0,100,1)
Y <- seq(0,100,1)

DF <- expand.grid(X,Y)
DF$Z <- sin(DF$Var1) + cos(DF$Var2)
Z <- matrix(DF$Z, nrow = 100)

plot_ly(y = ~Y, x = ~X, z=~Z) %>%  
    add_surface()

But the code below does runs with no warning messages, but produces an empty graph:


X <- seq(0,100,1)
Y <- seq(0,100,1)
DF <- expand.grid(X,Y)
DF <- expand.grid(X,Y)

DF$Z <- sin(DF$Var1) + cos(DF$Var2)
Z <- matrix(DF$Z, nrow = 10201)

plot_ly(y = ~Y, x = ~X, z=~Z) %>%  
    add_surface()

Thanks

In your first example, X has a length of 101 but you give Z only 100 rows. That causes the warning. Z should have as many rows as X has elements and as many columns as Y has elements.
In the second example, Z has only one column so the Y values cannot be aligned to the columns of Z.

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.