在R中按变量和Plot对曲面进行着色 [英] Color surface by variable with plotly in R
本文介绍了在R中按变量和Plot对曲面进行着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用volcano
数据,我想用另一个变量给火山表面着色,它是一个矩阵MatrixForColor
,大小与volcano
相同,它只包含1和2s(二进制)。对于MatrixForColor = 1
和MatrixForColor = 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
之后不是红色和蓝色。
Any是否知道如何使用plotly
执行此操作?
推荐答案
您需要为cmin
和cmax
设置正确的值:
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)
)
这篇关于在R中按变量和Plot对曲面进行着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文