在 R 热图中使用颜色标签(文本)

5

我正在尝试在R中制作一个热力图,其中标签文本被着色(以指示数据点来自哪个组)。

我目前正在使用heatmap.2,但也很乐意使用其他软件包。

heatmap.2(data.matrix(data),trace = 'none', dendrogram='none',ColSideColors=col)

这个调用给我带来了彩色条(ColSideColors)沿着标签,但我想把标签本身也变成彩色的。非常感谢!
3个回答

3

您需要创建一个包含col.axis参数的新函数。以下是要使用的函数行:

# there were four long pages of code before this section:

axis(1, 1:nc, labels = labCol, las = 2, line = -0.5, tick = 0, # original line
        col.axis="green",   # added argument
        cex.axis = cexCol)
if (!is.null(xlab)) 
        mtext(xlab, side = 1, line = margins[1] - 1.25)
axis(4, iy, labels = labRow, las = 2, line = -0.5, tick = 0, # original line
        col.axis="green",   # added argument
        cex.axis = cexRow)

0

我无法回复lwz0203,因为缺乏评论权限,但是他们的代码不完整。您需要添加以下行:

if(is.vector(RlabColor)) {
    RlabColor=RlabColor[rowInd]
}

(类似地,对ClabColor执行同样的操作)
if(is.vector(ClabColor)) {
    ClabColor=ClabColor[colInd]
}

在代码的某个时刻,如果使用颜色向量,标签的着色可能会不匹配。使用以下代码,向量labRow或labCol中的文本已经被重新排序:

if (is.null(labRow))
    labRow <- if (is.null(rownames(x)))
        (1:nr)[rowInd]
        else rownames(x)
else labRow <- labRow[rowInd] 

所以我在同一个位置添加了RlabColor和ClabColor的重新排序。

0

我最近遇到了相同的问题,最终使用 mtext 替换原始的 axis。先前的答案显示使用 axis 语句来绘制标签。但是,col.axis 只能指定一种颜色。为了启用向量颜色,

#    axis(1, 1:nc, labels = labCol, las = 2, line = -0.5, tick = 0, 
#        cex.axis = cexCol )
mtext(side = 1, text = labCol, at = 1:nc, las = 2, line = 0.5,col = ClabColor, cex = cexCol)


#    axis(4, iy, labels = labRow, las = 2, line = -0.5, tick = 0, 
#        cex.axis = cexRow )
mtext(side = 4, text = labRow, at = iy, las = 2, line = 0.5,col = RlabColor, cex = cexCol)

另外,记得在函数中添加两个参数:ClabColor = "black", RlabColor = "black"。默认颜色为黑色。
另一件需要注意的事情是,向量颜色应该按照标签的顺序排列,在计算树状图时会进行排列。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接