1

断面の 2 つのサーフェスを使用して plot_ly サーフェス プロットを作成しようとしています。つまり、一方のサーフェスを Z 平面にマッピングし、もう一方のサーフェスを X 平面にマッピングします。

Z 平面上のサーフェスは正常に機能していますが、別の平面に 2 番目のサーフェスを追加するとつまずきます。おそらく、plot_ly がデータを処理する方法について誤解している可能性があります。

私はこのようなことをしようとしていますが、このプロットからコードを自分の R (RStudio) で動作させることができません https://plot.ly/~schippkus/0/slices-in-volumetric-data/ #プロット

これは、片面作業で再現可能な例で、両面作業は奇数です。このサンプルで作成されたデータは、私が取り組んでいるデータと同じディメンションを持っています。もともとは 3 次元配列で提供されていましたが、この形式に強制しました。

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) %>% as.data.frame() %>% 
      mutate(Val = val)

    getTrace = function(data,plane,slice){
      require(tidyverse)
      tmp = data %>% filter(get(plane) %in% slice)

      Trace = list(
        x = tmp %>% select(X) %>% unique() %>% unlist() %>% unname(),
        y = tmp %>% select(Y) %>% unique() %>% unlist() %>% unname(),
        z = tmp %>% select(Z) %>% unique() %>% unlist() %>% unname()
      )

      switch(plane,
             "Z"={
               oneplane = expand.grid(x = Trace$x, y = Trace$y)
               oneplane$z <- tmp %>% select(Val) %>% unlist() %>% unname()

               Trace$SurfaceColor = as.matrix(spread(oneplane, key = x, value = z)[, -1]) %>% unname()
               Trace$z <- matrix(slice, ncol=ncol(Trace$SurfaceColor), nrow=nrow(Trace$SurfaceColor))
               Trace
             },
             "Y"={
               oneplane = expand.grid(x = Trace$x, z = Trace$z)
               oneplane$y <- tmp %>% select(Val) %>% unlist() %>% unname()

               Trace$SurfaceColor = as.matrix(spread(oneplane, key = z, value = y)[, -1]) %>% unname()
               Trace$y <- matrix(slice, ncol=ncol(Trace$SurfaceColor), nrow=nrow(Trace$SurfaceColor))
               Trace
             },
             "X"={
               oneplane = expand.grid(y = Trace$y, z = Trace$z)
               oneplane$x <- tmp %>% select(Val) %>% unlist() %>% unname()

               Trace$SurfaceColor = as.matrix(spread(oneplane, key = y, value = x)[, -1]) %>% unname()
               Trace$x <- 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() %>% 
      add_trace(x=Trace1$x,
                y=Trace1$y,
                z=Trace1$z, 
                type="surface", 
                cauto=T,
                surfacecolor=Trace1$SurfaceColor,
                colorscale=c("white","black")
      )  %>%
      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() %>% 
      add_trace(x=Trace1$x,
                y=Trace1$y,
                z=Trace1$z, 
                type="surface", 
                cauto=T,
                surfacecolor=Trace1$SurfaceColor,
                colorscale=c("white","black")
      ) %>%
      add_trace(x=Trace2$x,
                y=Trace2$y,
                z=Trace2$z, 
                type="surface", 
                cauto=T,
                surfacecolor=Trace2$SurfaceColor,
                colorscale=c("white","black")
      ) %>%
      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)"
               )
             ))
4

1 に答える 1