Функция bluered
в пакете gplots
делает это. Вы можете сделать свою цветовую палитру как:
library(gplots) # not to be confused with `ggplot2`, which is a very different package
color_palette <- bluered(9) # change the number to adjust how many shades of blue/red you have. Even numbers will assign white to two bins in the middle.
Чтобы выровнять их по центру, вы можете использовать функцию heatmap.2
, также в gplots
- просто не делайте кластеризацию:
heatmap.2(mat,
Rowv = FALSE,
Colv = FALSE,
dendrogram = 'none',
trace = 'none',
col = bluered, # this can take a function
symbreaks = TRUE, # this is the key value for symmetric breaks
)
Чтобы придерживаться функции image
, вам необходимо вручную установить разрывы. Следующий код получит это для вас:
pos_breaks <- quantile(abs(mat), probs = seq(0, 1, length.out = 5))
centered_breaks <- c(rev(-pos_breaks[-1]), pos_breaks)