如何改变热图中的颜色键值?

时间:2022-10-26 13:55:09

如何改变热图中的颜色键值?

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

正如上面的截图所示,我在这里使用了函数热图2()。

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

在颜色编码栏中,我如何改变“值”?

One can just use the data from gplots package:

我们可以使用来自g小区包的数据:

 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 :-)

谢谢:-)

2 个解决方案

#1


2  

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.)

你应该类型的热图。然后将源代码复制到编辑器中,然后使用搜索函数找到“值”。将“值”更改为其他东西(在引号中),然后输入热图。2 <-并粘贴在代码中并点击返回。(除非您保存它,否则它只会持续到会话继续。)

#2


6  

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

函数的热图。自从@BondedDust回答后,可能已经改变了,但是现在可以很容易地改变这个热图了。2主要通过标签:

key.xlab="New value"

关键。xlab =“新价值”

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:

现在替换x和y标签:

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

如何改变热图中的颜色键值?

#1


2  

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.)

你应该类型的热图。然后将源代码复制到编辑器中,然后使用搜索函数找到“值”。将“值”更改为其他东西(在引号中),然后输入热图。2 <-并粘贴在代码中并点击返回。(除非您保存它,否则它只会持续到会话继续。)

#2


6  

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

函数的热图。自从@BondedDust回答后,可能已经改变了,但是现在可以很容易地改变这个热图了。2主要通过标签:

key.xlab="New value"

关键。xlab =“新价值”

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:

现在替换x和y标签:

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

如何改变热图中的颜色键值?