我是使用 plotly 的新手,我正在尝试将一组平面添加到特定点的数据中。
我已经能够使用以下代码在 scatterplot3d 中成功添加平面:
my_data <- mtcars
# Create Scatter Plot
library(scatterplot3d)
# Scale data accordingly
cars_3d_plot <- scatterplot3d(x = scale(my_data$drat), y = scale(my_data$wt), z = scale(my_data$qsec),
highlight.3d = F, type = "p",
pch = 16,
ylab = "wt (y)", zlab = "qsec (z)", xlab = "drat (x)",
xlim = c(-4,5),
ylim = c(-4,5),
zlim = c(-4,5))
# Put in Qsec Cut - scale down from 20
qsec_cut <- (20-mean(my_data$qsec, na.rm = T))/sd(my_data$qsec, na.rm = T)
cars_3d_plot$plane3d(qsec_cut, 0, 0)
# Put in Drat Cut at 3.596563
drat_before <- 3.596563
drat_scaled <- (3.596563 - mean(my_data$drat, na.rm = T))/sd(my_data$drat, na.rm = T)
# YZ
# THIS CUTS THE PLANE AT X = 5
x0 <- drat_scaled
xyz1 <- cars_3d_plot$xyz.convert(rep(x0, 11), rep(-4, 11), seq(-4, 6, by=1))
xyz2 <- cars_3d_plot$xyz.convert(rep(x0, 11), rep(6, 11), seq(-4, 6, by=1))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")
xyz1 <- cars_3d_plot$xyz.convert(rep(x0, 11), seq(-4, 6, by=1), rep(-4, 11))
xyz2 <- cars_3d_plot$xyz.convert(rep(x0, 11), seq(-4, 6, by=1), rep(6, 11))
segments(xyz1$x, xyz1$y, xyz2$x, xyz2$y, lty="dotted")
但是,我想让它具有交互性并巧妙地使用。
我已经阅读了这个页面(Add Regression Plane to 3d Scatter Plot in Plotly)并成功添加了回归(如果需要可以提供代码)
但是,我真的很想以交互式形式添加这些飞机,并且非常卡住:(。如果你能帮助或指出正确的方向:) 提前谢谢你。
我曾尝试使用 rgl 包,但使用我拥有的数据,它不能正确计算轴,我已经进入了情节兔子洞。
编辑:我使用 add_mesh 在 z 轴上添加,但是,如何在 x = 4 时添加网格?
my_data <- mtcars
df <- data.frame(x = my_data$drat, y = my_data$wt, z = my_data$qsec)
planDf <- data.frame(x = rep(range(df$x, na.rm = T), 2), y = rep(range(df$y, na.rm = T), each = 2), z = 20)
plan_df_2 <- data.frame(x = rep(range(df$x, na.rm = T), 2), y = rep(range(df$y, na.rm = T), each = 2), z = 100)
library(plotly)
my_data$gear = as.character(my_data$gear)
plot_ly(my_data) %>%
add_markers(x = ~drat, y = ~wt, z = ~qsec, color = ~gear, colors = c("firebrick", "forestgreen", "orange")) %>%
add_mesh(x = ~x, y = ~y, z = ~z, data = planDf, opacity = 0.3)