绘制热图我们常用pheatmap或者heatmap.2、ggplot2。其能满足我们基本的热图绘制,但当我们做研究想展示多个标签或者组合图时,再用这几个包会发现定制起来非常困难,ComplexHeatmap包就是一个可定制化热图的一个高级R包。

ComplexHeatmap 可以绘制复杂热图、组合热图、也可以绘制多标签、定制化的热图、OncoPrint等(更多细节请访问:http://www.bioconductor.org/packages/release/bioc/html/ComplexHeatmap.html

下面通过一个示例简单介绍 ComplexHeatmap 的使用:


# 加载包
library(ComplexHeatmap)
# 此处引入circlize的目的使用了colorRamp2颜色选择的函数
library(circlize)

# 读取ComplexHeatmap自带的基因表达的数据集
expr = readRDS(paste0(
    system.file(package = "ComplexHeatmap"),
    "/extdata/gene_expression.rds"
))

# 获取细胞的基因表达值(去掉其他信息)
mat = as.matrix(expr[, grep("cell", colnames(expr))])
base_mean = rowMeans(mat)
mat_scaled = t(apply(mat, 1, scale))

# 替换[s+数字]
type = gsub("s\\d+_", "", colnames(mat))

ha = HeatmapAnnotation(df = data.frame(type = type))

# 绘制主热图
Heatmap(
    mat_scaled,
    name = "expression",
    km = 5,
    col = colorRamp2(c(-2, 0, 2), c("green", "white", "red")),
    top_annotation = ha,
    top_annotation_height = unit(4, "mm"),
    show_row_names = FALSE,
    show_column_names = FALSE
) +
    Heatmap( # 绘制右侧base_mean的单列热图
        base_mean,
        name = "base_mean",
        show_row_names = FALSE,
        width = unit(5, "mm")
    ) +
    Heatmap( # 绘制右侧length的单列热图
        expr$length,
        name = "length",
        col = colorRamp2(c(0, 1000000), c("white", "orange")),
        heatmap_legend_param = list(
            at = c(0, 200000, 400000, 60000, 800000, 1000000),
            labels = c("0kb", "200kb", "400kb", "600kb", "800kb", "1mb")
        ),
        width = unit(5, "mm")
    ) +
    # 绘制右侧type的单列热图,注意和最右侧的type区分开,最右侧的type是
    # 主热图的legend
    Heatmap(expr$type, name = "type", width = unit(5, "mm")) 

结果如下:

参考文章:

1.http://bioinformatics.oxfordjournals.org/content/early/2016/05/20/bioinformatics.btw313.abstract

2.https://jokergoo.github.io/ComplexHeatmap-reference/book/