Set the color scheme for the heatmap

I am trying to compare data using heatmaps. I want the color scale to be the same for all of them. for example, everything on the scale for values ​​from -0.5 to 0.5;

I am using gplots redgreen (50). but color intensity scales on different datasets.

for example: my r.matrix can vary from -1 to +1; and my r1.matrix can range from -0.2 to +0.2. building two heat maps, if on the same color scheme I expected that the color for r1.matrix would be much dimmer

hm <- heatmap(r.matrix, Colv = NA, col = redgreen(50)) hm1 <- heatmap(r1.matrix, Colv = NA, col = redgreen(50)) 

The color scale will cover the data range, so with the same red (50) it will be the same red or green for data in the range from -1 to +1 and for data from -.2 to +2. the reason that the color range [-1,1] in the data range [-.2, .2] should be able to visualize the difference in the data without looking at or not knowing the data range is the search step.

+6
source share
1 answer

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) # the original color vector # range of values in first matrix around median r1 <- range(r.matrix) - median(r.matrix) # range of values in second matrix around median r2 <- range(r1.matrix) - median(r1.matrix) # relative distances to median of second compared to first matrix prop <- r1 / r2 # center of colcor vector cent <- length(rg) / 2 + 0.5 # calculate indices of shorter color vector for the second matrix ind <- cent / prop idx <- round(cent - c(1, -1) * ind) # new color vector rg_new <- rg[Reduce(seq, idx)] 

Use these vectors to color heat maps:

 heatmap(r.matrix, Colv = NA, col = rg) 

enter image description here

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

enter image description here

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

+4
source

All Articles