R语言:热图(Heatmap)显示指定 Gene 的标签注释

前言

效果图,手头没有数据……所以模拟的数据,将就着看吧😑:
在这里插入图片描述
   左图,可以指示感兴趣的基因在热图中的位置,有一些文章中可以看到一样的图;右图,可以指示每个分组中感兴趣的基因,只是这个组有这个基因,位置不代表在热图中的具体位置。

Package简介

   R package: ComplexHeatmap,作者:Zuguang Gu,文章发表:Gu, Z. (2016) Complex heatmaps reveal patterns and correlations in multidimensional genomic data. DOI: 10.1093/bioinformatics/btw313,个人使用起来的最大感受就是高度定制化风格多样,也是我目前使用最多的热图呈现工具😑

安装

# 直接 Github 安装
devtools::install_github("jokergoo/ComplexHeatmap")
# 从 Bioconductor 安装
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("ComplexHeatmap")

图(左)

library(ComplexHeatmap)
## 准备一下数据
m <- matrix(rnorm(1000), nrow = 100)
rownames(m) <- 1:100

ha <- rowAnnotation(
	## 标记在原始数据(就是上面的m)的哪个位置
	foo = anno_mark(at = c(1:4, 20, 60, 97:100),
	## 要标记的基因
	labels = month.name[1:10])
	)

pdf("tmp1.pdf", height = 10, width = 5)

Heatmap(
	m, 
	name = "mat", 
	cluster_rows = FALSE, 
	right_annotation = ha,
	row_names_side = "left", 
	row_names_gp = gpar(fontsize = 4), 
	row_km = 4
	)

dev.off()

图(右)

# 搞一个随机生成字符串的fun
random_text <- function(n) {
  sapply(1:n, function(i) {
    paste0(sample(letters, sample(4:10, 1)), collapse = "")
  })
}
# 
(text_list <- list(
  text1 = random_text(4), # 日常使用的时候直接用基因之类的ok
  text2 = random_text(4),
  text3 = random_text(4),
  text4 = random_text(4)
))
## 需要这样格式的一个list,list中的每个部分就是分组中要注释的内容,
## 直接应用的时候需要换成,基因甚至可以是GO的通路
# $text1
# [1] "irtgfbkqp" "uxzflesr"  "wtaql"     "iytoqj"   
# $text2
# [1] "hefnwzsgcp" "mqsuw"      "cnemqvdop"  "rvfco"     
# $text3
# [1] "axiw"   "girmlb" "xztlc"  "gecubh"
# $text4
# [1] "gczw"     "edposqvi" "ldrv"     "fmbkapd"

ha <- rowAnnotation(
	foo = anno_empty(border = FALSE,
	width = max_text_width(unlist(text_list)) + unit(4, "mm"))
	)

pdf("tmp2.pdf", height = 10, width = 5)

Heatmap(
	m, 
	name = "mat",
	row_km = 4,
	right_annotation = ha,
	show_row_names = F
	)

for(i in 1:4) {
  decorate_annotation("foo", slice = i, {
    grid.rect(x = 0, width = unit(2, "mm"), gp = gpar(fill = i, col = NA), just = "left")
    grid.text(paste(text_list[[i]], collapse = "\n"), x = unit(4, "mm"), just = "left")
  })
}

dev.off()

总结

   根据使用的集成开发环境(IDE),比如Rstudio、vscode、Atom等,画图的时候可能会有个Message(不翻译了……):

It seems you are using RStudio IDE. `anno_mark()` needs to work with the physical size of the graphics device. It
only generates correct plot in the figure panel, while in the zoomed plot (by clicking the icon 'Zoom') or in the
exported plot (by clicking the icon 'Export'), the connection to heatmap rows/columns might be wrong. You can
directly use e.g. pdf() to save the plot into a file.

Use `ht_opt$message = FALSE` to turn off this message.

   这也是我直接保存为PDF的原因。对于左图,跟适合注释一些基因在热图中的位置,而左图可能更适合其他一些,至于是啥,还没想好……

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值