使用颜色图例在R中绘制三维Tri-Surf图

l7wslrjt  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(117)

我希望创建一个3D网格并叠加热图,根据外部变量(下面代码中的f)为每个面分配统一的颜色。下面是我使用plotly包的R代码:

library(plotly)
library(colorspace)
pal2 <- sequential_hcl(n = 99, h = c(320, 80), c = c(60, 65, 20), l = c(30, 95), 
                       power = c(0.7, 1.3), register = )
# Landmark data
lm <- matrix(c(0,1,0,0,0,0,1,0,0,0,0,1), ncol = 3)

# Triangulation file
it <- matrix(c(1,1,2,3,3,4), ncol = 3)

# Value associated with the 2 triangular facets
f <- c(2,5)
col <- pal2[findInterval(f,seq(from = 0, to = 7, length.out = length(pal2)))]

# Create 3D surface mesh
fig <- plot_ly(x=lm[,1], y=lm[,2], z=lm[,3], type="mesh3d",
               i=it[,1]-1, j=it[,2]-1, k=it[,3]-1,
               facecolor = col)

# Display plot
fig

虽然这似乎正确地创建了图形,但没有创建颜色图例。x1c 0d1x
我的调色板pal 2有99种颜色,从0到7的范围。我想要一个颜色图例来指定它。但我不知道如何在R中plotly实现。
请注意,如果我要为每个**顶点(地标)**赋值,我可以使用以下代码创建热图和颜色图例:

lm <- matrix(c(0,1,0,0,0,0,1,0,0,0,0,1), ncol = 3)
it <- matrix(c(1,1,2,3,3,4), ncol = 3)
y <- c(1,2,3,4) # Value associated with the 4 landmarks

library(plotly)

thresholds_colors = seq(0, 1, length.out = 99)

library(colorspace)
pal2 <- sequential_hcl(n = 99, h = c(320, 80), c = c(60, 65, 20), l = c(30, 95), 
                       power = c(0.7, 1.3), register = )
palRGB <- col2rgb(pal2)
colors <- NULL
for (i in 1:99) {
 colors[i] <- paste0("rgb(", palRGB[1, i], ", ", palRGB[2, i], ", ", palRGB[3, i], ")")
}

# Create the surface mesh
mesh <- plot_ly(x = lm[,1], y = lm[,2], z = lm[,3], type = "mesh3d", 
                i = it[,1]-1, j = it[,2]-1, k = it[,3]-1,
                intensity = y,
                colorscale = list(thresholds_colors, colors),
                showscale = T, cmin = 0, cmax = 7)
mesh

我只是无法在facet赋值时生成颜色图例。我只想在第一张图中添加颜色图例,就像第二张图一样。谢谢!
在R plotly中可以找到一些3D tri-surf图的例子here。但是没有一个例子显示了颜色图例。

gab6jxml

gab6jxml1#

您可以使用color参数来中断colors,如下所示:

library(plotly)
library(colorspace)
pal2 <- sequential_hcl(n = 99, h = c(320, 80), c = c(60, 65, 20), l = c(30, 95), 
                       power = c(0.7, 1.3), register = )
# Landmark data
lm <- matrix(c(0,1,0,0,0,0,1,0,0,0,0,1), ncol = 3)

# Triangulation file
it <- matrix(c(1,1,2,3,3,4), ncol = 3)

# Value associated with the 2 triangular facets
f <- c(2,5)
col <- pal2[findInterval(f,seq(from = 0, to = 7, length.out = length(pal2)))]

# Create 3D surface mesh
fig <- plot_ly(x=lm[,1], y=lm[,2], z=lm[,3], type="mesh3d",
               i=it[,1]-1, j=it[,2]-1, k=it[,3]-1,
               intensity = c(0, 0.5, 1),
               color = c(0, 0.5, 1),
               colors = col)

# Display plot
fig

x1c 0d1x创建于2023-04-01,附带reprex v2.0.2

相关问题