【问题标题】:Color surface by variable with plotly in R在R中通过变量着色表面
【发布时间】:2019-10-25 12:54:51
【问题描述】:

使用volcano 数据,我想用另一个变量为火山表面着色,这是一个矩阵MatrixForColor,与volcano 大小相同,它只包含1s 和2s(是二进制)。对于MatrixForColor = 1MatrixForColor = 2,我想分别涂上蓝色和红色。

Formatting of persp3d plot 的启发,我设法使用rgl 包中的persp3d 实现了这一点,如下所示:

library(rgl)    
color = c("blue", "red")
type  = MatrixForColor
persp3d(volcano, theta=50, phi=25, expand=0.75, col=color[type],
        ticktype="detailed", xlab="", ylab="", zlab="", axes=TRUE)

得到这个图:

我也尝试使用plotly 来实现这一点(在plotly - different colours for different surfaces 的响应之后进行调整)如下:

library(plotly)
plot_ly(colors = c('blue', 'red')) %>%
  add_surface(z = volcano,
              opacity = 0.8,
              surfacecolor=MatrixForColor,
              cauto=F,
              cmax=1,
              cmin=0
  )

但我得到了这个数字:

这不是我想要的,因为在MatrixForColor 之后它没有被着色为红色和蓝色。

任何人知道如何使用plotly 做到这一点?

【问题讨论】:

  • 能否也加入MatrixForColor

标签: r plotly r-plotly


【解决方案1】:

您需要为cmincmax 设置正确的值:

library(plotly)

MatrixForColor <- matrix(1, nrow = nrow(volcano), ncol = ncol(volcano))
MatrixForColor[, 1:30] <- 2

plot_ly(colors = c('blue', 'red')) %>%
  add_surface(z = volcano,
              opacity = 0.8,
              surfacecolor = MatrixForColor,
              cauto=F,
              cmax=max(MatrixForColor),
              cmin=min(MatrixForColor)
  )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-03
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多