简体   繁体   中英

How to make color key range from 0 to 1 on pre-normalized data in R's heatmap.2

The following code of mine:

library(RColorBrewer)
library(gplots)
dat <- mtcars 

# normalize here
dat <- t(apply(dat, 1, function(x) log2(x+1)/sum(log2(x+1)) ))

heatcol <- brewer.pal(9, "Reds")

dat.m <- as.matrix(dat) 
heatmap.2(dat.m,
        Colv=FALSE, # We want to maintain column order
        Rowv=FALSE,  
        key=TRUE,
        symbreaks=TRUE,
        col=heatcol,
        dendrogram="none",
        scale="none", # because we already normalize
        trace="none",
        labRow=FALSE, 
        density.info="none",
        keysize=2);

Produces this figure: 在此处输入图片说明

Note that 1) we already perform pre normalization and 2) scale = "none" . But why in the color key the scale is from -0.2 to 0.2? How can I change the code to make the color key to scale between 0 to 1?

Ideally the final plot would have values of 0 encoded as white.

Simply use heatmap.2() with the option symkey=FALSE and the colour key will go from 0 to c. 0.24 (max. value of your matrix).

If you need 0 as white you will have to define a different colour palette, since yours contains only reds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM