# non-numeric argument to mathematical function

I am trying to "stack 3d surfaces on top of each other" using the plotly library. I am following the tutorial from this website here : 3D Surface Plots | R | Plotly (official plotly website).

``````#code that works
library(plotly)

z <- c(
c(8.83,8.89,8.81,8.87,8.9,8.87),
c(8.89,8.94,8.85,8.94,8.96,8.92),
c(8.84,8.9,8.82,8.92,8.93,8.91),
c(8.79,8.85,8.79,8.9,8.94,8.92),
c(8.79,8.88,8.81,8.9,8.95,8.92),
c(8.8,8.82,8.78,8.91,8.94,8.92),
c(8.75,8.78,8.77,8.91,8.95,8.92),
c(8.8,8.8,8.77,8.91,8.95,8.94),
c(8.74,8.81,8.76,8.93,8.98,8.99),
c(8.89,8.99,8.92,9.1,9.13,9.11),
c(8.97,8.97,8.91,9.09,9.11,9.11),
c(9.04,9.08,9.05,9.25,9.28,9.27),
c(9,9.01,9,9.2,9.23,9.2),
c(8.99,8.99,8.98,9.18,9.2,9.19),
c(8.93,8.97,8.97,9.18,9.2,9.18)
)
dim(z) <- c(15,6)
z2 <- z + 1
z3 <- z - 1

fig <- plot_ly(showscale = FALSE)
fig <- fig %>% add_surface(z = ~z)
fig <- fig %>% add_surface(z = ~z2, opacity = 0.98)
fig <- fig %>% add_surface(z = ~z3, opacity = 0.98)

fig
``````

But when I try to run this on my own data, I seem to get a few errors:

``````#code does not work
library(plotly)

DF <- data.frame(X = seq(0,100,1), Y = seq(0,100,1))
DF <- cbind(DF, expand.grid(X1,Y1))

DF\$Z1 <- sin(DF\$Var1) + cos(DF\$Var2)
Z1 <- matrix(DF\$Z1, nrow = 100)

DF\$Z2 <- -sin(DF\$Var1) + 0.5*cos(DF\$Var2)
Z2 <- matrix(DF\$Z2, nrow = 100)

DF\$Z3 <- DF\$Var2^2  + DF\$Var1^2
Z3 <- matrix(DF\$Z3, nrow = 100)

fig <- plot_ly(y = DF\$y, x = DF\$X, z=Z1, type = "surface")
fig <- fig %>% add_surface(z=Z2, opacity = 0.98)
fig <- fig %>% add_surface(z=Z3, opacity = 0.98)
fig
``````

For example:

• Error in sin(DF\$Var1) : non-numeric argument to mathematical function
• Error in matrix(DF\$Z1, nrow = 100) :
• 'data' must be of a vector type, was 'NULL'

Can someone please show me how to fix these errors?
Thanks

In

it looks like you haven't defined `X1` and `Y1`. Perhaps you mend to put in `X` and `Y`?

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.