The redgreen(50) command is independent of your actual values ββand returns a vector of 50 colors. You can use this color vector and extract a subset of it to adapt it to the second matrix.
Example:
set.seed(1) r.matrix <- matrix(runif(16, -1, 1), 4, 4) r1.matrix <- r.matrix / 5
The values ββin the r1.matrix matrix are one fifth of the values ββin r.matrix .
Now color vectors can be created as follows:
library(gplots) rg <- redgreen(50)
Use these vectors to color heat maps:
heatmap(r.matrix, Colv = NA, col = rg)

heatmap(r1.matrix, Colv = NA, col = rg_new)

The range of colors in the second heat map is smaller than the range in the first heat map.
source share