x1 <- seq(1, 10, length.out = 30)
x2 <- seq(1, 10, length.out = 30)
grid <- expand.grid(x1 = x1, x2 = x2)
grid$y <- model$coefficients[1] + model$coefficients[2]*grid$x1
fig <- plot_ly(df, x = ~x, y = ~y, type = 'scatter3d', mode = 'markers',
marker = list(size = 4, color = 'maroon')) %>%
add_surface(x = ~x1, y = ~x2, z = matrix(grid$y, 30, 30),
showscale = FALSE, opacity = 0.6) %>%
layout(title = "Projection of Data Points onto Regression Plane",
scene = list(xaxis = list(title = "x"),
yaxis = list(title = "index"),
zaxis = list(title = "y")),
margin = list(l = 0, r = 0, b = 0, t = 40)) %>%
config(displayModeBar = FALSE)
fig