【问题标题】:Define specific value colouring with pheatmap in R在 R 中使用 pheatmap 定义特定值着色
【发布时间】:2015-12-09 07:13:33
【问题描述】:

假设:

m1<-matrix(rnorm(1000),ncol=100)

和定义颜色:

cols = colorRampPalette(c("white", "red"))(30)

我正在生成一个没有使用 pheatmap 功能聚类的热图:

pheatmap(dist(t(m1)), cluster_rows = F, cluster_cols = F, show_rownames = TRUE, 
color = cols, main = 'Heatmap')

问题是,我如何定义颜色以获得相同的热图,但仅使用特定值着色的像素(例如小于 0.1)。

我试着设置

cols = ifelse(dist(t(m1))<0.1,'red','black')

但没用。

【问题讨论】:

    标签: r matrix colors pheatmap


    【解决方案1】:

    对于简单的二元配色方案,您可以使用breaks 参数:

    library(pheatmap)
    
    set.seed(1)
    m1<-matrix(c(rnorm(1000)), ncol=100)
    
    pheatmap(dist(t(m1)),
             cluster_rows = F,
             cluster_cols = F,
             show_rownames = TRUE, 
             color = c("red", "black"),
             breaks = c(0, 3, 9),  # distances 0 to 3 are red, 3 to 9 black
             main = 'Heatmap')
    

    看起来像这样:

    如果你更喜欢颜色渐变,可以按如下方式进行:

    m <- matrix(c(rnorm(1000)), ncol=100)
    distmat <- dist(t(m))
    
    # Returns a vector of 'num.colors.in.palette'+1 colors. The first 'cutoff.fraction'
    # fraction of the palette interpolates between colors[1] and colors[2], the remainder
    # between colors[3] and colors[4]. 'num.colors.in.palette' must be sufficiently large
    # to get smooth color gradients.
    makeColorRampPalette <- function(colors, cutoff.fraction, num.colors.in.palette)
    {
      stopifnot(length(colors) == 4)
      ramp1 <- colorRampPalette(colors[1:2])(num.colors.in.palette * cutoff.fraction)
      ramp2 <- colorRampPalette(colors[3:4])(num.colors.in.palette * (1 - cutoff.fraction))
      return(c(ramp1, ramp2))
    }
    
    cutoff.distance <- 3  
    cols <- makeColorRampPalette(c("white", "red",    # distances 0 to 3 colored from white to red
                                   "green", "black"), # distances 3 to max(distmat) colored from green to black
                                 cutoff.distance / max(distmat),
                                 100)
    
    pheatmap(distmat,
             cluster_rows = F,
             cluster_cols = F,
             show_rownames = TRUE, 
             color = cols,
             main = 'Heatmap')
    

    然后看起来像这样:

    【讨论】:

    • 嗨@WhiteViking,我试过这个,它处理了半个多小时,直到我停止它。我正在使用 McBook 1,4 GHz Intel Core i5 中的 Rstudio。另外,黑底只有红点对我来说也不错。
    • @Kwnwps 我添加了一个使用二进制配色方案(黑底红点)的示例。
    • @Kwnwps 至于处理时间长:这个玩具示例会发生这种情况吗?或者仅适用于更大的数据集 - 如果是这样,它的大小是多少?
    • 谢谢。我在 230 x 230 距离矩阵中进行了尝试。玩具示例没有任何问题。最适合我的情况是组合,即黑色直到阈值,然后逐渐从一种颜色变为另一种颜色。
    • border_color = NA 作为附加参数传递给 pheatmap。 (见?pheatmap...)
    【解决方案2】:

    不是您要求的,但这里有一个可以帮助其他人的 ggplot 解决方案。

    set.seed(1)         # for reproducible example
    m1 <- matrix(rnorm(1000),ncol=100)
    d  <- dist(t(m1))
    
    library(ggplot2)
    library(reshape2)   # for melt(...)
    gg.df <- melt(as.matrix(d), varnames=c("row","col"))
    
    # fill is red for value < 3; black for value >= 3
    ggplot(gg.df, aes(x=factor(col), y=factor(row)))+ 
      geom_tile(aes(fill=ifelse(value<3, "below", "above")), color=NA)+
      scale_fill_manual("Threshold",values=c(below="#FF0000", above="#000000"))+
      coord_fixed()
    

    # fill is black for value > 3; gradient white to red for value <= 3
    ggplot(gg.df, aes(x=factor(col), y=factor(row)))+ 
      geom_tile(aes(fill=value), color=NA)+
      scale_fill_gradient(low="#FFFFFF", high="#FF0000", limits=c(0,3), na.value="black")+
      coord_fixed()
    

    【讨论】:

      猜你喜欢
      • 2017-06-06
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 2018-06-22
      • 1970-01-01
      相关资源
      最近更新 更多