如何使用预定义的样本组使用 dendextend 和 heatmap.2 为树状图的标签着色
How to color labels of dendogram with dendextend and heatmap.2 using pre-defined sample groups
heatmap.2 在使用 "colRow" 参数时将不正确的颜色分配给标签。有没有其他方法可以在 heatmap.2 中为标签分配颜色?还是我做错了什么? (示例基于 Label and color leaf dendrogram and 中的示例)
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)
ash,需要保持col_labels
数据原来的顺序
修改后的代码如下:(找到"WHAT I CHANGED"评论)
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)
heatmap.2 在使用 "colRow" 参数时将不正确的颜色分配给标签。有没有其他方法可以在 heatmap.2 中为标签分配颜色?还是我做错了什么? (示例基于 Label and color leaf dendrogram and
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)
ash,需要保持col_labels
数据原来的顺序
修改后的代码如下:(找到"WHAT I CHANGED"评论)
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)