基因组数据绘图手机好文R

complexheatmap学习3——热图拼接

2019-07-03  本文已影响58人  drlee_fc74

本文是对于ComplexHeatmap作者说明的学习笔记。如果想看原文可去:https://jokergoo.github.io/ComplexHeatmap-reference/book/a-list-of-heatmaps.html

complexheatmap的主要特点是可以组合多个热图成为一个图片。需要注意的是,对于不同热图组合的时候。如果是水平组合,那么不同热图之间的行数必须相同。如果是垂直组合那么不同热图之间的列数必须相同。

我们主要接受的是水平组合。但是垂直组合也是一样的。

最基本的例子

一般来说,不同的热图可以通过+来进行组合。例如下图

library(ComplexHeatmap)
## Loading required package: grid
## ========================================
## ComplexHeatmap version 2.0.0
## Bioconductor page: http://bioconductor.org/packages/ComplexHeatmap/
## Github page: https://github.com/jokergoo/ComplexHeatmap
## Documentation: http://jokergoo.github.io/ComplexHeatmap-reference
## 
## If you use it in published research, please cite:
## Gu, Z. Complex heatmaps reveal patterns and correlations in multidimensional 
##   genomic data. Bioinformatics 2016.
## ========================================
library(circlize)
## ========================================
## circlize version 0.4.6
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: http://jokergoo.github.io/circlize_book/book/
## 
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization 
##   in R. Bioinformatics 2014.
## ========================================
set.seed(123)
mat1 = matrix(rnorm(80, 2), 8, 10)
mat1 = rbind(mat1, matrix(rnorm(40, -2), 4, 10))
rownames(mat1) = paste0("R", 1:12)
colnames(mat1) = paste0("C", 1:10)

mat2 = matrix(runif(60, max = 3, min = 1), 6, 10)
mat2 = rbind(mat2, matrix(runif(60, max = 2, min = 0), 6, 10))
rownames(mat2) = paste0("R", 1:12)
colnames(mat2) = paste0("C", 1:10)

le = sample(letters[1:3], 12, replace = TRUE)
names(le) = paste0("R", 1:12)

ind = sample(12, 12)
mat1 = mat1[ind, ]
mat2 = mat2[ind, ]
le = le[ind]
ht1 = Heatmap(mat1, name = "rnorm")
ht2 = Heatmap(mat2, name = "runif")
ht3 = Heatmap(le, name = "letters")

ht1 + ht2 + ht3
heatma

标题

我们可以在draw里面的column_title以及row_title来设置标题。同时可以通过row/column_title_gp来自定义变化

col_rnorm = colorRamp2(c(-3, 0, 3), c("green", "white", "red"))
col_runif = colorRamp2(c(0, 3), c("white", "orange"))
col_letters = c("a" = "pink", "b" = "purple", "c" = "blue")
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm,
    row_title = "Heatmap 1", column_title = "Heatmap 1")
ht2 = Heatmap(mat2, name = "runif", col = col_runif,
    row_title = "Heatmap 2", column_title = "Heatmap 2")
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht_list = ht1 + ht2 + ht3

draw(ht_list, row_title = "Three heatmaps, row title", row_title_gp = gpar(col = "red"),
    column_title = "Three heatmaps, column title", column_title_gp = gpar(fontsize = 16))
image.png

热图的大小

热图的大小,我们可以在制作某一个热图的时候,自己先设置好。然后连接到一起就行

ht2 = Heatmap(mat2, name = "runif", col = col_runif, width = unit(4, "cm"))
ht3 = Heatmap(le, name = "letters", col = col_letters, width = unit(5, "mm"))
ht1 + ht2 + ht3
image.png

热图之间的空隙

我们可以通过ht_gap来设置热图的大小。

ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm)
ht2 = Heatmap(mat2, name = "runif", col = col_runif)
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht_list = ht1 + ht2 + ht3
draw(ht_list, ht_gap = unit(c(3, 10), "mm"))
image.png

基于主要的热图自动调整

在一系列热图当中。永远有一个主要的热图。其他的热图都会基于这个热图来进行自动的调整。主要调整包括: - 其他的热图不会进行行的聚类,同时其他的热图的行的顺序都会和主要热图一致。 - 行名只会包括一个 - 如果主要热图基于行进行分割了,那么其他热图也会进行分割 - 其他热图的高度会和主要热图的高度一致 - 默认的第一个热图会主要热图。但是可以通过main_heatmap来调整。这个参数接受的是热图的名字 - 默认的聚类树和聚类名称是在左侧的。我们可以通过row_dend_side以及row_sub_title_side来进行调整。 - 如果不想进行行的聚类。把第一个聚类设置为否即可。 - 我们可以通过auto_adjust来设置是否打开自动调整。如果不打开。那么就都会显示列名

ht_list = ht2 + ht1 + ht3
draw(ht_list, main_heatmap = "rnorm", row_dend_side = "right", row_sub_title_side = "left")
image.png
draw(ht_list, auto_adjust = FALSE)
image.png

主要热图的设置

一般主要热图可以在Heatmap当中设置好再进行链接的。同时的话,也可以通过draw函数来设置主要热图的。这个设置的主要参数包括 cluster_rows; clustering_distance_rows; clustering_method_rows; row_dend_width; show_row_dend; row_dend_reorder; row_dend_gap; row_order; row_gap; row_km; row_km_repeats; row_split; height; height_heatmap

ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm, row_km = 2, cluster_rows = FALSE)
ht2 = Heatmap(mat2, name = "runif", col = col_runif)
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht_list = ht1 + ht2 + ht3
draw(ht_list, row_km = 1, row_split = le, cluster_rows = TRUE)
image.png

注释部分的调整

一般来说如果多个热图都有注释的话,那么就会基于主要热图来自动调整。

ha1 = HeatmapAnnotation(foo1 = 1:10, annotation_name_side = "left")
ha2 = HeatmapAnnotation(bar2 = anno_barplot(1:10, height = unit(2, "cm")))
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm, top_annotation = ha1)
ht2 = Heatmap(mat2, name = "runif", col = col_runif, top_annotation = ha2)
ht3 = Heatmap(le, name = "letters", col = col_letters)
ht_list = ht1 + ht2 + ht3
draw(ht_list, ht_gap = unit(c(6, 2), "mm"))
image.png

热图和注释联系在一起

+不仅可以用于热图之间的联系,也可以用于热图和注释的联系。

ha1 = rowAnnotation(foo = 1:12, bar = anno_barplot(1:12, width = unit(4, "cm")))
ht1 = Heatmap(mat1, name = "rnorm", col = col_rnorm, row_km = 2)
ht1 + ha1
image.png

基本上热图和行的注释可以通过+来任意的组合

rowAnnotation(foo = 1:12) +
    Heatmap(mat1, name = "rnorm", col = col_rnorm, row_km = 2) + 
    rowAnnotation(bar = anno_barplot(1:12, width = unit(4, "cm"))) +
    Heatmap(mat2, name = "runif", col = col_runif)
image.png

注释之间的联系

如果热图和注释可以连接的话,那么注释和注释肯定也可以连接的

rowAnnotation(foo = 1:12) +
    rowAnnotation(bar = anno_barplot(1:12, width = unit(4, "cm")))
image.png

垂直的连接

上述讲到的连接都是水平的连接。这样的连接我们使用的是+;如果是垂直的连接的话,那就需要使用%v%号了。其他的设置其实和行一致的

mat1t = t(mat1)
mat2t = t(mat2)
ht1 = Heatmap(mat1t, name = "rnorm", col = col_rnorm, row_km = 2,
    left_annotation = rowAnnotation(foo1 = 1:10, bar1 = anno_barplot(1:10)))
ha = HeatmapAnnotation(foo = anno_barplot(1:12, height = unit(2, "cm"), 
    axis_param = list(side = "right")))
ht2 = Heatmap(mat2t, name = "runif", col = col_runif, row_km = 2,
    left_annotation = rowAnnotation(foo2 = 1:10))
ht3 = Heatmap(rbind(letters = le), name = "letters", col = col_letters)
ht_list = ht1 %v% ha %v% ht2 %v% ht3
draw(ht_list, column_km = 2)
image.png

自动调整热图的参数

当我们绘制多个热图的时候。有一些固定的设置想要进行同时的配置。这样的话,我们就可以通过ht_opt参数进行整体配置。其可以进行配置的参数可以参见主页。

ht_opt(heatmap_column_names_gp = gpar(fontface = "italic"), 
    heatmap_column_title_gp = gpar(fontsize = 10),
    legend_border = "black",
    heatmap_border = TRUE,
    annotation_border = TRUE
)
ht1 = Heatmap(mat1, name = "ht1", column_title = "Heatmap 1",
    top_annotation = HeatmapAnnotation(foo = 1:10))
ht2 = Heatmap(mat2, name = "ht2", column_title = "Heatmap 2",
    top_annotation = HeatmapAnnotation(bar = 1:10))
ht1 + ht2
image.png
上一篇下一篇

猜你喜欢

热点阅读