R语言 | ComplexHeatmap 画注释若干基因的热图 //todo

一般顺序:

  • 先用 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值