【问题标题】:Color Labels (text) in R heatmapR热图中的颜色标签(文本)
【发布时间】:2012-10-23 18:25:16
【问题描述】:

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

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

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

这个调用给了我标签上的彩色条(ColSideColors),但我想让标签本身有颜色。

非常感谢!

【问题讨论】:

    标签: r colors heatmap labels


    【解决方案1】:

    您需要创建一个包含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)
    

    【讨论】:

      【解决方案2】:

      我最近也遇到了同样的问题,最后我用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"。默认颜色为黑色。

      你需要注意的另一件事是,矢量颜色应该遵循标签的顺序,当你计算树状图时,它们会被置换

      【讨论】:

        【解决方案3】:

        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 的重新排序。

        【讨论】:

          猜你喜欢
          • 2020-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-04
          • 1970-01-01
          • 1970-01-01
          • 2014-05-08
          • 1970-01-01
          相关资源
          最近更新 更多