问题描述
使用R程序包Pheatmap绘制热图.有没有办法为输入矩阵中的NA分配颜色?默认情况下,似乎NA的颜色为白色.例如:
Using R package pheatmap to draw heatmaps. Is there a way to assign a color to NAs in the input matrix? It seems NA gets colored in white by default.E.g.:
library(pheatmap)
m<- matrix(c(1:100), nrow= 10)
m[1,1]<- NA
m[10,10]<- NA
pheatmap(m, cluster_rows=FALSE, cluster_cols=FALSE)
谢谢
推荐答案
可能,但是需要一些技巧.
It is possible, but requires some hacking.
首先,让我们看看pheatmap
如何绘制热图.您可以通过在控制台中键入pheatmap
并滚动显示输出,或使用edit(pheatmap)
来进行检查.
First of all let's see how pheatmap
draws a heatmap. You can check that just by typing pheatmap
in the console and scrolling through the output, or alternatively using edit(pheatmap)
.
您会发现颜色是使用
mat = scale_colours(mat, col = color, breaks = breaks)
scale_colours
函数似乎是pheatmap
包的内部函数,但是我们可以使用
The scale_colours
function seems to be an internal function of the pheatmap
package, but we can check the source code using
getAnywhere(scale_colours)
哪个给
function (mat, col = rainbow(10), breaks = NA)
{
mat = as.matrix(mat)
return(matrix(scale_vec_colours(as.vector(mat), col = col,
breaks = breaks), nrow(mat), ncol(mat), dimnames = list(rownames(mat),
colnames(mat))))
}
现在我们需要检查scale_vec_colours
,结果是:
Now we need to check scale_vec_colours
, that turns out to be:
function (x, col = rainbow(10), breaks = NA)
{
return(col[as.numeric(cut(x, breaks = breaks, include.lowest = T))])
}
因此,实质上,pheatmap
是使用cut
来决定要使用的颜色.
So, essentially, pheatmap
is using cut
to decide which colours to use.
如果周围有NA,请尝试看看cut
会做什么:
Let's try and see what cut
does if there are NAs around:
as.numeric(cut(c(1:100, NA, NA), seq(0, 100, 10)))
[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
[29] 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6
[57] 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9
[85] 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 NA NA
它返回NA!所以,这是您的问题!
It returns NA! So, here's your issue!
现在,我们如何解决它?最简单的方法是让pheatmap
绘制热图,然后根据需要对NA值进行过度绘制.
Now, how do we get around it?The easiest thing is to let pheatmap
draw the heatmap, then overplot the NA values as we like.
再次查看pheatmap
函数,您会看到它使用grid
程序包进行绘图(另请参见以下问题:)
Looking again at the pheatmap
function you'll see it uses the grid
package for plotting (see also this question: R - How do I add lines and text to pheatmap?)
因此,您可以使用grid.rect
将矩形添加到NA位置.我要做的是通过反复试验找到热图边界的坐标,然后从那里开始绘制矩形.
So you can use grid.rect
to add rectangles to the NA positions.What I would do is find the coordinates of the heatmap border by trial and error, then work from there to plot the rectangles.
例如:
library(pheatmap)
m<- matrix(c(1:100), nrow= 10)
m[1,1]<- NA
m[10,10]<- NA
hmap <- pheatmap(m, cluster_rows=FALSE, cluster_cols=FALSE)
# These values were found by trial and error
# They WILL be different on your system and will vary when you change
# the size of the output, you may want to take that into account.
min.x <- 0.005
min.y <- 0.01
max.x <- 0.968
max.y <- 0.990
width <- 0.095
height <- 0.095
coord.x <- seq(min.x, max.x-width, length.out=ncol(m))
coord.y <- seq(max.y-height, min.y, length.out=nrow(m))
for (x in seq_along(coord.x))
{
for (y in seq_along(coord.y))
{
if (is.na(m[x,y]))
grid.rect(coord.x[x], coord.y[y], just=c("left", "bottom"),
width, height, gp = gpar(fill = "green"))
}
}
更好的解决方案是使用edit
函数破解pheatmap
的代码,并按您的意愿处理NA.
A better solution would be to hack the code of pheatmap
using the edit
function and have it deal with NAs as you wish...
这篇关于pheatmap:不适用的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!