我正在尝试在R中制作一个热力图,其中标签文本被着色(以指示数据点来自哪个组)。
我目前正在使用heatmap.2,但也很乐意使用其他软件包。
heatmap.2(data.matrix(data),trace = 'none', dendrogram='none',ColSideColors=col)
这个调用给我带来了彩色条(ColSideColors)沿着标签,但我想把标签本身也变成彩色的。非常感谢!
您需要创建一个包含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)
我无法回复lwz0203,因为缺乏评论权限,但是他们的代码不完整。您需要添加以下行:
if(is.vector(RlabColor)) {
RlabColor=RlabColor[rowInd]
}
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]
我最近遇到了相同的问题,最终使用 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"
。默认颜色为黑色。