简体   繁体   中英

How to change the color key value in heatmap.2?

在此处输入图片说明

As the above screenshot showed, I used the function heatmap.2() here.

how can I change ' Value ' in the color coded bar to any other name ?

One can just use the data from gplots package:

 library(gplots)

 data(mtcars)

 x  <- as.matrix(mtcars)

 rc <- rainbow(nrow(x), start=0, end=.3)

 cc <- rainbow(ncol(x), start=0, end=.3)

 heatmap.2(x, key=TRUE)

Many thanks :-)

The function heatmap.2 may have changed since @BondedDust answered, but its now possible to easily change the heatmap.2 key labels via:

key.xlab="New value"

First, your code from above (using the standard colors):

library(gplots)
data(mtcars)
x  <- as.matrix(mtcars)
heatmap.2(x,key=TRUE)

关键截图前

Now replace the x and y labels:

library(gplots)
data(mtcars)
x  <- as.matrix(mtcars)
heatmap.2(x, key=TRUE , key.xlab="New value", key.ylab="New count")

钥匙截图后

It's hard-coded. You will need to change it in the code. It appears about midway down the section that draws the key and the line is:

else mtext(side = 1, "Value", line = 2)

This is the section of the heatmap.2 code that creates the key (at least up to the point where the word "Value" appears) :

 if (key) {
        par(mar = c(5, 4, 2, 1), cex = 0.75)
        tmpbreaks <- breaks
        if (symkey) {
            max.raw <- max(abs(c(x, breaks)), na.rm = TRUE)
            min.raw <- -max.raw
            tmpbreaks[1] <- -max(abs(x), na.rm = TRUE)
            tmpbreaks[length(tmpbreaks)] <- max(abs(x), na.rm = TRUE)
        }
        else {
            min.raw <- min(x, na.rm = TRUE)
            max.raw <- max(x, na.rm = TRUE)
        }
        z <- seq(min.raw, max.raw, length = length(col))
        image(z = matrix(z, ncol = 1), col = col, breaks = tmpbreaks, 
            xaxt = "n", yaxt = "n")
        par(usr = c(0, 1, 0, 1))
        lv <- pretty(breaks)
        xv <- scale01(as.numeric(lv), min.raw, max.raw)
        axis(1, at = xv, labels = lv)
        if (scale == "row") 
            mtext(side = 1, "Row Z-Score", line = 2)
        else if (scale == "column") 
            mtext(side = 1, "Column Z-Score", line = 2)
        else mtext(side = 1, "Value", line = 2)
 .... lots more code below

You should type heatmap.2 , then copy the source code to an editor and then use the search function to find "Value". Change "Value" to something else (in quotes) and then type heatmap.2 <- and paste in the code and hit return. (Unless you save this it will only persist as long as the session continues.)

Just come across same task recently. Now there is an option "key.title" to set the title for scale inlet:

library(gplots)
data(mtcars)
x  <- as.matrix(mtcars)
heatmap.2(x, key.title = "New Title", key.xlab="New value", key.ylab="New count")

在此处输入图片说明

Unfortunately, it do not propagate properly if there is no histogram in inlet:

library(gplots)
data(mtcars)
x  <- as.matrix(mtcars)
heatmap.2(x, key.title = "New Title", key.xlab="New value", key.ylab="New count")

在此处输入图片说明

Well, key.xlab working as expected and can be used instead.

I've checked the source code on github and it is already fixed there.

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