R语言可视化公共数据挖掘DL/ML/生物信息

ComplexHeatmap绘制复杂热图

2020-06-21  本文已影响0人  nnlrl

pheatmap交互

最近ComplexHeatmap更新可以将pheatmap对象进行参数转换。新增的ComplexHeatmap::pheatmap()的功能与pheatmap::pheatmap()完全一致,其返回一个Formal class Heatmap对象,这意味着我们可以使用ComplexHeatmap中的其他函数对其进行操作并绘制更加复杂的热图。

# 模拟数据
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")

# 整理注释信息
annotation_col = data.frame(
  CellType = factor(rep(c("CT1", "CT2"), 5)), 
  Time = 1:5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")

annotation_row = data.frame(
  GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "")

ann_colors = list(
  Time = c("white", "firebrick"),
  CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
  GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)

# 绘图
p1 = pheatmap(test, name = "mat1")
p2 = rowAnnotation(foo = anno_barplot(1:nrow(test)), border = F)
p3 = pheatmap(test, name = "mat2", 
              col = colorRampPalette(c("navy", "white", "firebrick3"))(50))
# or you can simply specify as
# p3 = pheatmap(test, name = "mat2", col = c("navy", "white", "firebrick3"))
p1 + p2 + p3
ComplexHeatmap::pheatmap

ComplexHeatmap::Heatmap主体热图参数

绘制热图首先需要准备已经标准化的二维矩阵作为输入,可以出图后我们就需要对主体部分进行修改,一些比较常用的参数包括单元格(颜色,边界,大小等),行/列聚类(是否聚类,是否展示聚类树,聚类树的位置,大小,颜色等),行/列注释(行/列名,位置,大小,颜色,角度等),图注信息等。

ComplexHeatmap主要使用了grid绘图系统,使用gpar调整热图参数:

unit参数则主要控制一些距离参数,包括子图间的空隙row/column_gap = unit(1, "mm"),热图的长宽heatmap_width/height = unit(1, "npc"),聚类树的高度row/column_dend_width/height = unit(10, "mm")以及文字的最大长度row/column_names_max_width/height = unit(6, "cm")

控制聚类信息的参数:

控制单元格的参数:

控制行/列标签的参数:

控制整体图形的参数:

控制图注的参数:

控制注释的参数:

HeatmapAnnotation热图注释

对于ComplexHeatmap来说,其主旨就是绘制复杂热图,所以对热图的注释以及拼图极其重视,对于*_annotation来说包括包括各种类型图形和文字丰富结果,并且还支持使用+(行)和%v%(列)对多个热图进行组装。热图注释是热图的重要组成部分,它显示与热图中的行或列关联的其他信息。 ComplexHeatmap软件包为设置注释和定义新的注释图形提供了非常灵活的支持。注释可以放在热图的四个侧面,由top_annotationbottom_annotationleft_annotationright_annotation参数。

四个参数的值应在HeatmapAnnotation类中,并应由HeatmapAnnotation()函数构造,如果是行批注,则应由rowAnnotation()函数构造。(rowAnnotation()columnAnnotation()只是一个与HeatmapAnnotation(…,which=“row/column”)相同的辅助函数)。HeatmapAnnotation的简单用法如下。

ha = HeatmapAnnotation(
    # 指定annotation的名称
    foo = cbind(a = 1:10, b = 10:1), 
    bar = sample(letters[1:3], 10, replace = TRUE),
    col = list(foo = col_fun, bar = c("a" = "red", "b" = "green", "c" = "blue")),    # 指定颜色,传入list
    gp = gpar(col='white')    # 指定边界颜色
    simple_anno_size = unit(1, "cm")    # 指定单元格大小
)
Heatmap(matrix(rnorm(100), 10), 
    top_annotation = HeatmapAnnotation(foo = anno_block(gp = gpar(fill = 2:4),
        labels = c("group1", "group2", "group3"), 
        labels_gp = gpar(col = "white", fontsize = 10))),
    column_km = 3,
    left_annotation = rowAnnotation(foo = anno_block(gp = gpar(fill = 2:4),
        labels = c("group1", "group2", "group3"), 
        labels_gp = gpar(col = "white", fontsize = 10))),
    row_km = 3)
# 横向散点图
ha = HeatmapAnnotation(foo = anno_points(runif(10), ylim = c(0, 1),
    axis_param = list(
        side = "right",
        at = c(0, 0.5, 1), 
        labels = c("zero", "half", "one")
    ))
)

# 纵向散点图
ha = rowAnnotation(foo = anno_points(runif(10), ylim = c(0, 1),
    width = unit(2, "cm"),
    axis_param = list(
        side = "bottom",
        at = c(0, 0.5, 1), 
        labels = c("zero", "half", "one"),
        labels_rot = 45
    ))
)
ha = HeatmapAnnotation(foo = anno_lines(cbind(c(1:5, 1:5), c(5:1, 5:1)), 
    gp = gpar(col = 2:3), add_points = TRUE, pt_gp = gpar(col = 5:6), pch = c(1, 16)))

ha = HeatmapAnnotation(foo = anno_lines(runif(10), smooth = TRUE))
# 绘制柱状图
ha = HeatmapAnnotation(foo = anno_barplot(seq(-5, 5), baseline = "min"))

# 绘制堆积图
ha = HeatmapAnnotation(foo = anno_barplot(matrix(nc = 2, c(1:10, 10:1))))
柱状图
堆积图
ha = HeatmapAnnotation(foo = anno_boxplot(m, height = unit(4, "cm"), 
    gp = gpar(fill = 1:10)))
# n_breaks控制显示的柱子数量
ha = rowAnnotation(foo = anno_histogram(m, n_breaks = 20))
ha = rowAnnotation(foo = anno_density(m))

示例

# 示例数据
mat = readRDS(system.file("extdata", "measles.rds", package = "ComplexHeatmap"))

# 列注释 顶部 条形图
ha1 = HeatmapAnnotation(
  dist1 = anno_barplot(
    colSums(mat), 
    bar_width = 1, 
    gp = gpar(col = "white", fill = "#FFE200"), 
    border = FALSE,
    axis_param = list(at = c(0, 2e5, 4e5, 6e5, 8e5),
                      labels = c("0", "200k", "400k", "600k", "800k")),
    height = unit(2, "cm")
  ), show_annotation_name = FALSE)

# 行注释 右侧 条形图
ha2 = rowAnnotation(
  dist2 = anno_barplot(
    rowSums(mat), 
    bar_width = 1, 
    gp = gpar(col = "white", fill = "#FFE200"), 
    border = FALSE,
    axis_param = list(at = c(0, 5e5, 1e6, 1.5e6),
                      labels = c("0", "500k", "1m", "1.5m")),
    width = unit(2, "cm")
  ), show_annotation_name = FALSE)

# 列注释 底部 标签注释
year_text = as.numeric(colnames(mat))
year_text[year_text %% 10 != 0] = ""
ha_column = HeatmapAnnotation(
  year = anno_text(year_text, rot = 0, location = unit(1, "npc"), just = "top")
)

# 颜色
col_fun = colorRamp2(c(0, 800, 1000, 127000), c("white", "cornflowerblue", "yellow", "red"))

# 热图主体
ht_list = Heatmap(mat, name = "cases", col = col_fun, cluster_row_slices = 2, 
                  cluster_columns = F, show_row_dend = F, rect_gp = gpar(col= "white"), 
                  show_column_names = FALSE,
                  row_names_side = "left", row_names_gp = gpar(fontsize = 6),
                  column_title = 'Measles cases in US states 1930-2001\nVaccine introduced 1961',
                  top_annotation = ha1, bottom_annotation = ha_column,
                  heatmap_legend_param = list(at = c(0, 5e4, 1e5, 1.5e5), 
                                              labels = c("0", "50k", "100k", "150k"))) + ha2
draw(ht_list, ht_gap = unit(5, "mm"))

# 分割线
decorate_heatmap_body("cases", {
  i = which(colnames(mat) == "1961")
  x = i/ncol(mat)
  grid.lines(c(x, x), c(0, 1), gp = gpar(lwd = 2, lty = 2))
  grid.text("Vaccine introduced", x, unit(1, "npc") + unit(5, "mm"))
})
上一篇 下一篇

猜你喜欢

热点阅读