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.

1
  • Take a look at this example that uses add_trace for scatter3d and mesh3d together --- does that help? Commented Jul 14, 2021 at 18:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.