R语言 自定义调色板与自定义中断统计::热图

atmip9wb  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(115)

我想为我的数据创建一个聚类热图,基于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")

cu6pst1q

cu6pst1q1#

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")


的数据

相关问题