Subplotting 3D plots in R - with rotation animation

I have the following R code:

It should be plotting two separate 3D subplots but it plots the both dataset overlaid onto a single axis.

server_mut <- function(input, output, session) {
 
 output$graph <- renderPlotly({
   
   plot1 <- plot_ly(
     type = "scatter3d",
     mode = "markers",
     data = plot.dataWT,
     color = ~seurat_clusters, 
     colors = UMAP.cols.13,
     x = ~ UMAP_1,
     y = ~ UMAP_2,
     z = ~ UMAP_3,
     text = ~label,
     marker = list(size = 3, width = 1)
   ) %>%
     layout(
       scene = list(
         camera = list(
           eye = list(
             x = 0.8,
             y = 0.8,
             z = 0.8
           ),
           center = list(
             x = 0,
             y = 0,
             z = 0
           )
         )
       )
     )
   
   plot2 <- plot_ly(
     type = "scatter3d",
     mode = "markers",
     data = plot.dataMut,
     color = ~seurat_clusters, 
     colors = UMAP.cols.13,
     x = ~ UMAP_1,
     y = ~ UMAP_2,
     z = ~ UMAP_3,
     text = ~label,
     marker = list(size = 3, width = 1)
   ) %>%
     layout(
       scene2 = list(
         camera = list(
           eye = list(
             x = 0.8,
             y = 0.8,
             z = 0.8
           ),
           center = list(
             x = 0,
             y = 0,
             z = 0
           )
         )
       )
     )
   
   subplot(plot1, plot2
           ) %>%
     onRender("
     function(el, x){
 var id = el.getAttribute('id');
 var gd = document.getElementById(id);
 Plotly.update(id).then(attach);
 function attach() {
   var cnt = 0;
   
   function run() {
     rotate('scene', Math.PI / 720);
     requestAnimationFrame(run);
   } 
   run();
   
   function rotate(id, angle) {
     var eye0 = gd.layout[id].camera.eye
     var rtz = xyz2rtz(eye0);
     rtz.t += angle;
     
     var eye1 = rtz2xyz(rtz);
     Plotly.relayout(gd, id + '.camera.eye', eye1)
   }
   
   function xyz2rtz(xyz) {
     return {
       r: Math.sqrt(xyz.x * xyz.x + xyz.y * xyz.y),
       t: Math.atan2(xyz.y, xyz.x),
       z: xyz.z
     };
   }
   
   function rtz2xyz(rtz) {
     return {
       x: rtz.r * Math.cos(rtz.t),
       y: rtz.r * Math.sin(rtz.t),
       z: rtz.z
     };
   }
 };
}
   ")
 })
}

shinyApp(ui, server_mut)
1 Like