I'd like to create a clustered heatmap of my data, clustering different models and categories based on the MAPE. I'd like to apply the color palette green-white-red, in which the range of green colors (MAPE: 0-10) is smaller than the range of red colors (MAPE 10, up to 100), with a mid-value of 10. However, when i try to do this, it results in a heatmap containing only green colors.
Dummy matrix - Let's say my data looks like this:
matrix_heatmap <- matrix(1:16, nrow=4, ncol=4,
dimnames=list(c("W", "X", "Y", "Z"),
c("A", "B", "C", "D")))
matrix_heatmap
custom colorpalette and breaks
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")
heatmapscales the values depending on parameterscale. From?heatmap:So either you provide your
breaksin a scaled version, or you ask for no-scaling: