我想在R图中创建三维散点图,并使用updatemenue将椭圆添加到每个图集。 (类似于https://plot.ly/r/custom-buttons/#relayout-button中的“重新布局按钮”部分,但适用于3D)。我可以使用ellipse3d添加椭圆来绘制图,但我不知道如何使用updatemenue布局使其交互。 我写了下面的代码,但还没有添加updatemenus。你能帮我一个忙吗?
df<-
structure(list(C = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L), .Label = c("h", "j", "k", "l"), class = "factor"),
R = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L), .Label = c("a", "b", "c"), class = "factor"),
p1 = c(-58.2553800845032, -56.8446241583638, -57.4730903743034,
9.69295162882175, 23.2783600609086, 17.5961245834489, 27.1776771896781,
31.8555589999195, 31.1329423894753, 5.49176565720366, 12.9720812431292,
13.3756328645854), p2 = c(1.89343789795219, 2.96630118684064,
3.36783254906029, 22.1036613994383, 19.1821210966211, 26.161634708624,
0.00600630960161123, 6.18082767371698, 1.73282189156538,
-26.351364716711, -26.6021818789505, -30.641098117759), p3 = c(1.98930539820297,
3.3628193816464, 4.50430994627108, -16.0161352497141, -10.0505758631406,
-3.19889710494869, 8.92935203885341, 7.00720243933593, 22.7494673296249,
-11.7929746942337, -5.26783717642642, -2.21603644547113)), .Names = c("C",
"R", "p1", "p2", "p3"), class = "data.frame", row.names = c(NA,
-12L))
library(plotly)
library(corpcor)
library(rgl)
x=df$p1; y=df$p2; z=df$p3
col <- c("orange", "blue", "purple", "green")
p <- plot_ly(df, x =x, y = y, z = z,type = "scatter3d",
mode = "markers", marker = list(color = col[df$C],
showscale = FALSE)
#, text = paste(df$C, df$R)
)
groups <- df$C
levs <- levels(groups)
group.col <- c("red", "blue", "yellow", "green")
for (i in 1:length(levs)) {
group <- levs[i]
selected <- groups == group
xx <- x[selected]; yy <- y[selected]; zz <- z[selected]
co<- cov(cbind(xx,yy,zz))
S<- make.positive.definite(co)
ellips <- ellipse3d(S, centre=c(mean(xx),mean(yy),mean(zz)), level = 0.95)
p<- add_trace(p, x = ellips$vb[1,], y = ellips$vb[2,], z = ellips$vb[3,]
,type = 'scatter3d', size = 1
,opacity=0.002
#,color= group.col[i]
,showlegend = FALSE)
}
print(p)