plotly - 多个表面的不同颜色标度

时间:2017-10-06 15:06:04

标签: r plotly

我试图用情节(4.7.1)和R 3.4.1在同一个地块上绘制两个3D表面,有两种不同的颜色标度(一种颜色为蓝色,另一种颜色为紫色)和从z值导出的颜色值。当我单独绘制曲面时,它的工作非常精细,但是当我使用add_trace或add_surface在同一个绘图上绘制两个曲面时,第二个曲面将采用第一个曲面的颜色。

以下是使用https://plot.ly/r/3d-surface-plots/#new-to-plotly

中的示例代码的示例
library(plotly)

z <- c(
  c(8.83,8.89,8.81,8.87,8.9,8.87),
  c(8.89,8.94,8.85,8.94,8.96,8.92),
  c(8.84,8.9,8.82,8.92,8.93,8.91),
  c(8.79,8.85,8.79,8.9,8.94,8.92),
  c(8.79,8.88,8.81,8.9,8.95,8.92),
  c(8.8,8.82,8.78,8.91,8.94,8.92),
  c(8.75,8.78,8.77,8.91,8.95,8.92),
  c(8.8,8.8,8.77,8.91,8.95,8.94),
  c(8.74,8.81,8.76,8.93,8.98,8.99),
  c(8.89,8.99,8.92,9.1,9.13,9.11),
  c(8.97,8.97,8.91,9.09,9.11,9.11),
  c(9.04,9.08,9.05,9.25,9.28,9.27),
  c(9,9.01,9,9.2,9.23,9.2),
  c(8.99,8.99,8.98,9.18,9.2,9.19),
  c(8.93,8.97,8.97,9.18,9.2,9.18)
)
dim(z) <- c(15,6)

z1 <- z - 1
z2 <- z + 1

cols1 <- c(rgb(255/255,112/255,183/255,1),rgb(128/255,0/255,64/255,1))
cols2 <- c(rgb(107/255,184/255,214/255,1),rgb(0/255,90/255,124/255,1))

p1 <- plot_ly(showscale = TRUE) %>%
  add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), color = ~z1, colors = cols1) %>%
  layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))

p2 <- plot_ly(showscale = TRUE) %>%
  add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), color = ~z2, colors = cols2) %>%
  layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))

p3 <- plot_ly(showscale = TRUE) %>%
  add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), color = ~z1, colors = cols1) %>%
  add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), color = ~z2, colors = cols2) %>%
  layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))

p1
p2
p3

我尝试inherit=F进入第二个add_surface,但它没有改变任何内容。我也看了plotly - different colours for different surfaces,但答案不适用于我的情况,因为我不想要一致的彩色图,而是颜色取决于z值。

我在其他地方没有找到任何答案,但我对情节很新,所以我希望答案不明显。 我通过在第二个colorscale中使用add_surface参数设法得到两个不同的比例:第一个表面的色阶是默认值,第二个是不同的,但不是我定义的颜色。

p4 <- plot_ly(showscale = TRUE) %>%
  add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), color = ~z1, reversescale=T) %>%
  add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), color = ~z2, colorscale = list(c(min(z1),"rgb(107,184,214)"),c(max(z2),"rgb(0,90,124)"))) %>%
  layout(scene = list(zaxis = list(range = c(min(z1),max(z2)))))

p4

我想这是因为最后一个例子,它在某种程度上是可以管理的,但我想我在这里做错了。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我设法通过正确使用colorscale参数得到了我想要的结果,如下面的代码所示:

p6 <- plot_ly(showscale = TRUE) %>%
   add_surface(z = ~z1, cmin = min(z1), cmax = max(z2), colorscale = list(c(0,1),c("rgb(255,112,184)","rgb(128,0,64)"))) %>%
   add_surface(z = ~z2, cmin = min(z1), cmax = max(z2), colorscale = list(c(0,1),c("rgb(107,184,214)","rgb(0,90,124)"))) %>%
   layout(title="SURFACE 1 + SURFACE 2\n(Distinct colorscales as defined)", scene = list(zaxis = list(range = c(min(z1),max(z2)))))

它提供了这样的内容:https://plot.ly/~sebastien1785/20/

但是,我无法使用colors参数重现此结果。谢谢你的帮助。