Customizing Scatter Plots

I am using the R programming language. I am trying to follow this tutorial (scatterplot3d: regression plane with residuals) and add a "plane" to a scatterplot.

Suppose I have the following data:

my_data <- data.frame(read.table(header=TRUE,
row.names = 1,
text="
                         weight   height age
                      1  2998.958 15.26611  53
                      2  3002.208 18.08711  52
                      3  3008.171 16.70896  49
                      4  3002.374 17.37032  55
                      5  3000.658 18.04860  50
                      6  3002.688 17.24797  45
                      7  3004.923 16.45360  47
                      8  2987.264 16.71712  47
                      9  3011.332 17.76626  50
                      10 2983.783 18.10337  42
                      11 3007.167 18.18355  50
                      12 3007.049 18.11375  53
                      13 3002.656 15.49990  42
                      14 2986.710 16.73089  47
                      15 2998.286 17.12075  52
"))

I adapted the code to fit my example:

library(scatterplot3d)

model_1 <- lm(age ~ weight + height, data = my_data)

# scatterplot
s3d <- scatterplot3d(my_data$height, my_data$weight, my_data$age, pch = 19, type = "p", color = "darkgrey",
                     main = "Regression Plane", grid = TRUE, box = FALSE,  
                     mar = c(2.5, 2.5, 2, 1.5), angle = 55)

# regression plane
s3d$plane3d(model_1, draw_polygon = TRUE, draw_lines = TRUE, 
            polygon_args = list(col = rgb(.1, .2, .7, .5)))

# overlay positive residuals
wh <- resid(model_1) > 0
s3d$points3d(my_data$height, my_data$weight, my_data$age, pch = 19)

Problem : However, the "plane" appears to be absent :

enter image description here

Desired Result:

enter image description here

Can someone please show me what I am doing wrong?

Thanks

the plane plotting code is looking at the model_1 lm object's coefficient, and it not looking at their names so much as the values and assumes a plotting order.
i.e. the z dimension, should map to the target of lm, the lm should list the independent variables in the order that the first mention will be plotted on the axis and the second will be on the y.

i.e. change to use

scatterplot3d(x =  my_data$weight
y = my_data$height,
z =   my_data$age ,
1 Like

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.