自定义调色板,在统计数据上具有自定义中断::热图

问题描述 投票:0回答:1

我想创建数据的聚类热图,根据 MAPE 对不同的模型和类别进行聚类。我想应用绿-白-红调色板,其中绿色的范围(MAPE:0-10)小于红色的范围(MAPE 10,最多100),中间为值为 10。 然而,当我尝试这样做时,它会产生一个仅包含绿色的热图。

虚拟矩阵 - 假设我的数据如下所示:

matrix_heatmap <- matrix(1:16, nrow=4, ncol=4, 
                         dimnames=list(c("W", "X", "Y", "Z"), 
                                         c("A", "B", "C", "D")))
matrix_heatmap

自定义调色板和中断

colors <- colorRampPalette(c("green", "white", "red"))
number_breaks <- 6
breaks_green <- seq(from=min(matrix_heatmap), to=10, 
                    length.out=floor(number_breaks / 2))
breaks_red <- seq(from=10, to=quantile(matrix_heatmap, 0.95), 
                  length.out=ceiling(number_breaks / 2))
breaks <- unique(c(0, breaks_green, breaks_red))

custom_palette <- colors(number_breaks - 1)

custom_palette
breaks

heatmap(matrix_heatmap, 
        col=custom_palette, 
        breaks=breaks,
        cexRow=0.5, cexCol=0.5)
legend("topleft", legend=breaks, fill=custom_palette, title="legenda")
r colors heatmap palette
1个回答
0
投票

heatmap
根据参数
scale
缩放值。来自
?heatmap

scale: character indicating if the values should be centered and
       scaled in either the row direction or the column direction,
       or none.  The default is ‘"row"’ if ‘symm’ false, and
       ‘"none"’ otherwise.

因此,您要么以缩放版本提供

breaks
,要么要求不缩放:

heatmap(matrix_heatmap, 
        col = custom_palette, 
        breaks = breaks,
        cexRow = .5, cexCol = .5, scale = "none")

© www.soinside.com 2019 - 2024. All rights reserved.