1

Using plotly and R I want to check that an ellipsoid is a good fit to the data points.

library(plotrix)
library(plotly)
library(Rvcg)
Library(rgl)

df_r_n_r = read.csv('temp.csv', header = TRUE)
fig <- plot_ly(df_r_n_r,type = "scatter3d", mode="markers", x = ~x, y = ~y, z = ~z)
fig

D <- rbind(c(459.956, -34.198, -29.844), c(-34.198, 481.647, 1.505), c(-29.844, 1.505, 559.393))
A <- D %*% t(D)
Am <- solve(A)
o <- c(73.658, 420.551, -429.058)
r <- 1

sphr <- vcgSphere()
ell <- scale3d(transform3d(sphr, chol(Am)), r, r, r)
vs <- ell$vb[1:3,] + o
idx <- ell$it - 1
e_plot <- plot_ly(type="mesh3d",
  x = vs[1,], y = vs[2,], z = vs[3,],
  i = idx[1,], j = idx[2,], k = idx[3,],
  opacity = 0.3)

I can plot the points using fig and the ellipsoid using e_plot, but how do I put them on the same plot so I can see how closely they fit.

Obromios
  • 15,408
  • 15
  • 72
  • 127
  • Take a look at [this example](https://stackoverflow.com/a/51872043/3460670) that uses `add_trace` for `scatter3d` and `mesh3d` together --- does that help? – Ben Jul 14 '21 at 18:49

1 Answers1

1

Using plot_ly, you can combine a number of plots using theadd_trace method. In the first instance, you can add the traces using the same parameters as you used to individually plot each item. In regard to the above question, this would produce:

    plot<- plot_ly()%>%
      add_trace(df_r_n_r,type = "scatter3d", mode="markers", x = ~x, y = ~y, z = ~z) %>%
      add_trace(type="mesh3d",x = vs[1,], y = vs[2,], z = vs[3,], idx[1,], j = idx[2,], k = idx[3,],opacity = 0.3)
   plot

Then you might want to vary the parameters in each trace to make the final plot clearer, e.g. change the opacity in the second plot.

Obromios
  • 15,408
  • 15
  • 72
  • 127