Plotting Multiple Reference Planes on a 3-Dimensional 'plotly::plot_ly()' Plot in R

85 Views Asked by At

I have the following data frame.

df <- data.frame(col1 = rnorm(100, 0, 5), col2 = rnorm(100, 0, 5), col3 = rnorm(100, 0, 5))

I'm making a 3-dimensional plotly plot and I want to plot the x, y, and z planes so that they can serve as reference planes. I've created some additional data frames with the coordinates of these reference planes so that I can use these coordinates when I make the plots.

# The First Reference Plane
Points_in_Plane_1 <- expand.grid(c(min(df$col1), max(df$col1)), c(min(df$col2), max(df$col2)))
colnames(Points_in_Plane_1) <- c('x', 'y')
Points_in_Plane_1$z <- rep(0, nrow(Points_in_Plane_1))
Points_in_Plane_1 <- Points_in_Plane_1[, c(which(colnames(Points_in_Plane_1) == 'x'), which(colnames(Points_in_Plane_1) == 'y'), which(colnames(Points_in_Plane_1) == 'z'))]
Points_in_Plane_1$Plane_Number <- rep(1, nrow(Points_in_Plane_1))
Points_in_Plane_1 <- Points_in_Plane_1[, c(which(colnames(Points_in_Plane_1) == 'Plane_Number'), which(colnames(Points_in_Plane_1) != 'Plane_Number'))]

# The Second Reference Plane
Points_in_Plane_2 <- expand.grid(c(min(df$col1), max(df$col1)), c(min(df$col3), max(df$col3)))
colnames(Points_in_Plane_2) <- c('x', 'z')
Points_in_Plane_2$y <- rep(0, nrow(Points_in_Plane_2))
Points_in_Plane_2 <- Points_in_Plane_2[, c(which(colnames(Points_in_Plane_2) == 'x'), which(colnames(Points_in_Plane_2) == 'y'), which(colnames(Points_in_Plane_2) == 'z'))]
Points_in_Plane_2$Plane_Number <- rep(2, nrow(Points_in_Plane_2))
Points_in_Plane_2 <- Points_in_Plane_2[, c(which(colnames(Points_in_Plane_2) == 'Plane_Number'), which(colnames(Points_in_Plane_2) != 'Plane_Number'))]

# The Third Reference Plane
Points_in_Plane_3 <- expand.grid(c(min(df$col2), max(df$col2)), c(min(df$col3), max(df$col3)))
colnames(Points_in_Plane_3) <- c('y', 'z')
Points_in_Plane_3$x <- rep(0, nrow(Points_in_Plane_3))
Points_in_Plane_3 <- Points_in_Plane_3[, c(which(colnames(Points_in_Plane_3) == 'x'), which(colnames(Points_in_Plane_3) == 'y'), which(colnames(Points_in_Plane_3) == 'z'))]
Points_in_Plane_3$Plane_Number <- rep(3, nrow(Points_in_Plane_3))
Points_in_Plane_3 <- Points_in_Plane_3[, c(which(colnames(Points_in_Plane_3) == 'Plane_Number'), which(colnames(Points_in_Plane_3) != 'Plane_Number'))]

# Combine All Plane Coordinates Into One Data Frame
Plane_Coordinates <- rbind(Points_in_Plane_1, Points_in_Plane_2, Points_in_Plane_3)

I have no problem plotting the first reference plane with the data.

# Adding the First Reference Plane
plotly::plot_ly(data = df, x = ~col1, y = ~col2, z = ~col3, type = 'scatter3d', mode = 'markers') %>%
  plotly::add_trace(data = Plane_Coordinates[Plane_Coordinates$Plane_Number == 1, ], x = ~x, y = ~y, z = ~z, type = 'mesh3d', inherit = FALSE)

I can't seem to get the other two planes to appear, though.

# Adding the Second Reference Plane
plotly::plot_ly(data = df, x = ~col1, y = ~col2, z = ~col3, type = 'scatter3d', mode = 'markers') %>%
  plotly::add_trace(data = Plane_Coordinates[Plane_Coordinates$Plane_Number == 2, ], x = ~x, y = ~y, z = ~z, type = 'mesh3d', inherit = FALSE)

# Adding the Third Reference Plane
plotly::plot_ly(data = df, x = ~col1, y = ~col2, z = ~col3, type = 'scatter3d', mode = 'markers') %>%
  plotly::add_trace(data = Plane_Coordinates[Plane_Coordinates$Plane_Number == 3, ], x = ~x, y = ~y, z = ~z, type = 'mesh3d', inherit = FALSE)

Why can I add the first reference plane to the figure but not the other 2 reference planes? I know that the first reference plane is horizontal and the other two are vertical; I'm not sure if the problem is that the other two reference planes are purely vertical and have no horizontal components. Thanks!

0

There are 0 best solutions below