简体   繁体   中英

How do I plot a 3D surface and data points using plotly and R?

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.

Using plot_ly , you can combine a number of plots using the add_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, eg change the opacity in the second plot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM