一般顺序:
- 先用 pheatmap 聚类
- 再用 ComplexHeatmap 做可视化:添加顶部、左侧聚类颜色,显示若干代表性基因
1.示例1
gene=c("Gene18", "Gene19", "Gene7","Gene3", "Gene9", "Gene15")
gene_pos=which(rownames(test) %in% gene) #3 7 9 15 18 19
#右侧要注释的基因
row_anno=rowAnnotation(gene=anno_mark(at=gene_pos, #位置
labels=gene, #文字
labels_gp=gpar(fontsize=8, col="red"))) #样式
Heatmap( test,
heatmap_legend_param = list(title=""), #修改图例标题,该语句或者 name=语句
show_row_names = F, #不显示右侧注释
right_annotation = row_anno) #只显示感兴趣基因
2.示例2: 排序更稳定的方法
#gene_pos = which( rev(rownames(newOrder)) %in% genelist) #11 191 221 434 532 536 574 586 630 排序可能错乱
gene_pos = match(genelist, rev(rownames(newOrder)) ) #排序更稳定
完整代码:
### complext heatmap ====
#1) 获取聚类后的基因顺序
row_cluster = cutree(ht.RNA.time$tree_row, k=4)
# 对聚类后的数据进行重新排序
newOrder = RNA_mean.time[ht.RNA.time$tree_row$order,] |> as.data.frame()
newOrder[,ncol(newOrder)+1]= row_cluster[match(rownames(newOrder), names(row_cluster))]
colnames(newOrder)[ncol(newOrder)]="Cluster"
# 查看重新排序后的数据
head(newOrder)
table(newOrder$Cluster)
# 1 2 3 4
#226 397 468 93
### save newOrder ====
write.table(newOrder, paste0(outputRoot, keyword, "_02_3_DEG.RNA-time.ComplexHeatmap.df.txt"))
#2) redraw with complexheatmap, with some genes
#BiocManager::install("ComplexHeatmap")
library(ComplexHeatmap)
# select some gene in each cluster
genelist= lapply( split( rownames(newOrder), newOrder$Cluster), function(x){
set.seed(42)
sample(x, 5)
}) |> unlist()
# before unlist
# $`1`
# [1] "RPL21" "TAGAP" "GPSM3" "RPL11" "GZMA"
#
# $`2`
# [1] "SQSTM1" "GLS" "GRPEL1" "MRPL1" "MRPS34"
#
# $`3`
# [1] "ARPC2" "EED" "UBE2L3" "RBBP8" "CCDC124"
#
# $`4`
# [1] "CACNA1A" "IGF2R" "CAMK4" "NEDD4L" "ZC3HAV1"
#gene_pos = which( rev(rownames(newOrder)) %in% genelist) #11 191 221 434 532 536 574 586 630 排序可能错乱
gene_pos = match(genelist, rev(rownames(newOrder)) ) #排序更稳定
#右侧要注释的基因
row_anno=rowAnnotation(gene=anno_mark(at=gene_pos, #位置
labels=genelist, #文字
labels_gp=gpar(fontsize=8, col="black"))) #样式
#cluster 颜色
colors.cluster = c('#E64B35','#4DBBD5','#3C5488', "#DE7417")
# 左侧聚类bar图
left_anno = rowAnnotation(cluster=anno_block(
gp=gpar(fill= lapply(colors.cluster, function(x) paste0(x, "55") ) |> unlist() ),
labels=sprintf("n=%s",
#unique( newOrder$Cluster),
table(newOrder$Cluster) |> as.numeric() ) ,
labels_gp = gpar(cex=1, col="black")
))
ht.RNA.time2 = Heatmap(
(function(){
t1=t(scale(t(newOrder[, 1:3])))
t1[ rev(rownames(t1)),]
})(),
#t(scale(t(RNA_mean.time))),
cluster_rows = FALSE,
cluster_columns = FALSE,
left_annotation = left_anno,
heatmap_legend_param = list(title=""), #修改图例标题,该语句或者 name=语句
show_row_names = F, #不显示右侧注释
#clustering_method_rows="ward.D2", clustering_method_columns = "ward.D2", split=3,
split=paste0("Cluster ", rev(newOrder$Cluster) ),
#col= viridis(20),
col= colorRampPalette(c("navy", "white", "firebrick3"))(50),
right_annotation = row_anno) #只显示感兴趣基因
#
pdf(paste0(outputRoot, keyword, "_02_3_DEG.RNA-time.ComplexHeatmap-withGenes.pdf"), width=3.2, height=6.5)
print(ht.RNA.time2)
dev.off()
#
ref
- https://dawneve.github.io/R_best_practice/plots.html#complexheatmap-%E7%83%AD%E5%9B%BE%E5%8A%9F%E8%83%BD%E5%A4%9A
- https://www.jianshu.com/p/eb8548cf73c4