【问题标题】:How to color labels of dendogram with dendextend and heatmap.2 using pre-defined sample groups如何使用预定义的样本组使用 dendextend 和 heatmap.2 为树状图的标签着色
【发布时间】:2018-07-03 18:46:04
【问题描述】:

heatmap.2 在使用“colRow”参数时为标签分配了不正确的颜色。有没有另一种方法可以为 heatmap.2 中的标签分配颜色?还是我做错了什么? (示例基于来自Label and color leaf dendrogramHow to color the branches and tick labels in the heatmap.2? 的示例)

library(dendextend)
library(gplots)

#make dataset
sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=1000))
groupCodes <- c(rep("Cont",5), rep("Tre1",5), rep("Tre2",5), rep("Tre3",5))
rownames(sample) <- make.unique(groupCodes)
colorCodes <- c(Cont="red", Tre1="green", Tre2="blue", Tre3="yellow")

#calculate distances, cluster
distSamples <- dist(sample)
hc <- hclust(distSamples)
dend <- as.dendrogram(hc)

# Assign the labels of dendrogram object with new colors:
labels_colors(dend) <- colorCodes[groupCodes][order.dendrogram(dend)]
col_labels<-labels_colors(dend)

# plot dendrogram
plot(dend,main ="colors of labels OK")

# plot dendogram and heatmap with heatmap.2
sample.datamatrix<-data.matrix(sample)
heatmap.2(sample.datamatrix, scale="row", 
          trace="none", 
          dendrogram="row",
          colRow = col_labels, # to add colored labels
          Rowv = dend, 
          main="colors of labels mixed-up",
          labCol = FALSE) # hide column names (i.e. gene names)

【问题讨论】:

    标签: r colors dendrogram gplots dendextend


    【解决方案1】:

    ash,你需要保持col_labels数据的原始顺序。

    这是修改后的代码:(找到“我改变了什么”注释)

    library(dendextend)
    library(gplots)
    
    #make dataset
    sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=1000))
    groupCodes <- c(rep("Cont",5), rep("Tre1",5), rep("Tre2",5), rep("Tre3",5))
    rownames(sample) <- make.unique(groupCodes)
    colorCodes <- c(Cont="red", Tre1="green", Tre2="blue", Tre3="yellow")
    
    #calculate distances, cluster
    distSamples <- dist(sample)
    hc <- hclust(distSamples)
    dend <- as.dendrogram(hc)
    
    # Assign the labels of dendrogram object with new colors:
    labels_colors(dend) <- colorCodes[groupCodes][order.dendrogram(dend)]
    col_labels<-labels_colors(dend)
    
    # plot dendrogram
    plot(dend,main ="colors of labels OK")
    
    # <================= WHAT I CHANGED ===================>
    # The labels need to be in the order of the original data:
    col_labels <- colorCodes[groupCodes]
    # </================= WHAT I CHANGED ===================>
    
    # plot dendogram and heatmap with heatmap.2
    sample.datamatrix<-data.matrix(sample)
    heatmap.2(sample.datamatrix, scale="row", 
              trace="none", 
              dendrogram="row",
              colRow = col_labels, # to add colored labels
              Rowv = dend, 
              main="colors of labels mixed-up",
              labCol = FALSE) # hide column names (i.e. gene names)
    

    【讨论】:

      猜你喜欢
      • 2017-12-26
      • 2015-09-16
      • 2015-05-29
      • 2021-09-08
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 2013-07-25
      • 2016-08-13
      相关资源
      最近更新 更多