Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Adding Regression Plane to 3d Scatter Plot in R

I am looking to take advantage of the awesome features in Plotly but I am having a hard time figuring out how to add a regression plane to a 3d scatter plot. I know how to get the scatter plot started, does anyone know how to take it the next step and add the plane based on the linear model?

library(plotly)
data(iris)


iris_plot <- plot_ly(my_df, 
                x = Sepal.Length, 
                y = Sepal.Width, 
                z = Petal.Length, 
                type = "scatter3d", 
                mode = "markers")

petal_lm <- lm(Petal.Length ~ 0 + Sepal.Length + Sepal.Width, 

Thanks!

1 Like

I don’t believe you can generate a surface plot with a linear model at the moment. You would have to sample some data points with the equation of the plane, and use these to make the surface plot. See here: https://plot.ly/r/3d-surface-plots/.