title: Slices in volumetric space in R
body: <p>Im trying to make a plot_ly surface plot with two surfaces that cross-section, ie mapping one surface on the Z plane, and another surface mapped on the X plane.</p>
  
  <p>The surface on the z-plane is working fine, but adding a second surface in another plane is tripping me up. I'm probably misunderstanding something about how plot_ly handles data.</p>
  
  <p>I'm trying to do something like this, but I can't get the code from this plot working in my own R (RStudio)
  <a href="https://plot.ly/~schippkus/0/slices-in-volumetric-data/#plot" rel="nofollow noreferrer">https://plot.ly/~schippkus/0/slices-in-volumetric-data/#plot</a></p>
  
  <p>Here a reproducible example with one-surface working, and the two-surface being odd. The data created in this sample has the same dimensions as the data I am working on. It originally comes in a 3-dimensional array, that I force into this format.</p>
  
  <pre><code>library(plotly); library(tidyverse); 
  
      x = 1:91
      y = 1:109
      z = 1:91
  
      val = as.numeric(scale(sample(length(x)*length(y)*length(z))))
  
      DATA = expand.grid(X=x,Y=y,Z=z) %&gt;% as.data.frame() %&gt;% 
        mutate(Val = val)
  
      getTrace = function(data,plane,slice){
        require(tidyverse)
        tmp = data %&gt;% filter(get(plane) %in% slice)
  
        Trace = list(
          x = tmp %&gt;% select(X) %&gt;% unique() %&gt;% unlist() %&gt;% unname(),
          y = tmp %&gt;% select(Y) %&gt;% unique() %&gt;% unlist() %&gt;% unname(),
          z = tmp %&gt;% select(Z) %&gt;% unique() %&gt;% unlist() %&gt;% unname()
        )
  
        switch(plane,
               "Z"={
                 oneplane = expand.grid(x = Trace$x, y = Trace$y)
                 oneplane$z &lt;- tmp %&gt;% select(Val) %&gt;% unlist() %&gt;% unname()
  
                 Trace$SurfaceColor = as.matrix(spread(oneplane, key = x, value = z)[, -1]) %&gt;% unname()
                 Trace$z &lt;- matrix(slice, ncol=ncol(Trace$SurfaceColor), nrow=nrow(Trace$SurfaceColor))
                 Trace
               },
               "Y"={
                 oneplane = expand.grid(x = Trace$x, z = Trace$z)
                 oneplane$y &lt;- tmp %&gt;% select(Val) %&gt;% unlist() %&gt;% unname()
  
                 Trace$SurfaceColor = as.matrix(spread(oneplane, key = z, value = y)[, -1]) %&gt;% unname()
                 Trace$y &lt;- matrix(slice, ncol=ncol(Trace$SurfaceColor), nrow=nrow(Trace$SurfaceColor))
                 Trace
               },
               "X"={
                 oneplane = expand.grid(y = Trace$y, z = Trace$z)
                 oneplane$x &lt;- tmp %&gt;% select(Val) %&gt;% unlist() %&gt;% unname()
  
                 Trace$SurfaceColor = as.matrix(spread(oneplane, key = y, value = x)[, -1]) %&gt;% unname()
                 Trace$x &lt;- matrix(slice, ncol=ncol(Trace$SurfaceColor), nrow=nrow(Trace$SurfaceColor))
                 Trace
               }
        )
      }
  
      Trace1 = getTrace(DATA,"Z",45)
      Trace2 = getTrace(DATA,"Y",45)
  
      # This works as intended (except I can't seem to make the surface greyscale)
      plot_ly() %&gt;% 
        add_trace(x=Trace1$x,
                  y=Trace1$y,
                  z=Trace1$z, 
                  type="surface", 
                  cauto=T,
                  surfacecolor=Trace1$SurfaceColor,
                  colorscale=c("white","black")
        )  %&gt;%
        layout(title="Ok one-surface plot",
               scene = list(
          aspectratio = list(
            x = 91/109, 
            y = 1, 
            z = 91/109
          ), 
          xaxis = list(
            backgroundcolor = "rgb(230, 230,230)", 
            gridcolor = "rgb(255, 255, 255)", 
            showbackground = TRUE, 
            zerolinecolor = "rgb(255, 255, 255)",
            range=c(1,91)
          ), 
          yaxis = list(
            backgroundcolor = "rgb(230, 230,230)", 
            gridcolor = "rgb(255, 255, 255)", 
            showbackground = TRUE, 
            zerolinecolor = "rgb(255, 255, 255)",
            range=c(1,109)
          ), 
          zaxis = list(
            backgroundcolor = "rgb(230, 230,230)", 
            gridcolor = "rgb(255, 255, 255)", 
            range = c(1, 91), 
            showbackground = TRUE, 
            zerolinecolor = "rgb(255, 255, 255)"
          )
        ))
  
      # This definately does not do as intended
      plot_ly() %&gt;% 
        add_trace(x=Trace1$x,
                  y=Trace1$y,
                  z=Trace1$z, 
                  type="surface", 
                  cauto=T,
                  surfacecolor=Trace1$SurfaceColor,
                  colorscale=c("white","black")
        ) %&gt;%
        add_trace(x=Trace2$x,
                  y=Trace2$y,
                  z=Trace2$z, 
                  type="surface", 
                  cauto=T,
                  surfacecolor=Trace2$SurfaceColor,
                  colorscale=c("white","black")
        ) %&gt;%
        layout(title="Bogus plot",
               scene = list(
                 aspectratio = list(
                   x = 91/109, 
                   y = 1, 
                   z = 91/109
                 ), 
                 xaxis = list(
                   backgroundcolor = "rgb(230, 230,230)", 
                   gridcolor = "rgb(255, 255, 255)", 
                   showbackground = TRUE, 
                   zerolinecolor = "rgb(255, 255, 255)",
                   range=c(1,91)
                 ), 
                 yaxis = list(
                   backgroundcolor = "rgb(230, 230,230)", 
                   gridcolor = "rgb(255, 255, 255)", 
                   showbackground = TRUE, 
                   zerolinecolor = "rgb(255, 255, 255)",
                   range=c(1,109)
                 ), 
                 zaxis = list(
                   backgroundcolor = "rgb(230, 230,230)", 
                   gridcolor = "rgb(255, 255, 255)", 
                   range = c(1, 91), 
                   showbackground = TRUE, 
                   zerolinecolor = "rgb(255, 255, 255)"
                 )
               ))
  </code></pre>