美文网首页生信绘图R语言绘图工作生活
🎨[可视化|R包]ComplexHeatmap学习笔记⑤Heat

🎨[可视化|R包]ComplexHeatmap学习笔记⑤Heat

作者: 郑宝童 | 来源:发表于2019-07-07 13:05 被阅读0次
  1. ComplexHeatmap学习笔记①Introduction to ComplexHeatmap package
  2. ComplexHeatmap学习笔记②Making A Single Heatmap
  3. ComplexHeatmap学习笔记③Making A List of Heatmaps
  4. ComplexHeatmap学习笔记④Heatmap Annotations

Heatmap and Annotation Legends 热图和注释图例


热图的图例由彩条,标签和标题组成。 ComplexHeatmap根据输入矩阵和注释自动生成图例,同时还提供灵活的方式自定义和添加新图例。

Basic settings 基本设置

所有热图和行注释的图例都画在一起,所有列注释的图例都画在一起。 热图的图例和注释的图例是存放在独立的viewports.中。.

library(ComplexHeatmap)
library(circlize)

set.seed(123)
mat = matrix(rnorm(80, 2), 8, 10)
mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
rownames(mat) = paste0("R", 1:12)
colnames(mat) = paste0("C", 1:10)

ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep("a", 5), rep("b", 5))),
    col = list(type1 = c("a" =  "red", "b" = "blue")))
ha_row = rowAnnotation(df = data.frame(type2 = c(rep("A", 6), rep("B", 6))),
    col = list(type2 = c("A" =  "green", "B" = "orange")), width = unit(1, "cm"))

ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column)
ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2")
ht_list = ht1 + ht2 + ha_row

draw(ht_list)

image.png

通过 heatmap_legend_side and annotation_legend_side可以设置图例放置的侧边,.

draw(ht_list, heatmap_legend_side = "left", annotation_legend_side = "bottom")
图例存放的侧边

show_heatmap_legend and show_annotation_legend 设置图例可见性.

draw(ht_list, show_heatmap_legend = FALSE, show_annotation_legend = FALSE)
显示图例与否

当你在整合single heatmap时,你可以通过设置 show_heatmap_legend的TURE/FALSE,来选择性的展示一部分热图的图例。同样的,HeatmapAnnotation() (或者快捷的函数 columnAnnotation() and rowAnnotation()) 也提供了show_legend参数来控制图例是否可见.

ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep("a", 5), rep("b", 5))),
    col = list(type1 = c("a" =  "red", "b" = "blue")), show_legend = FALSE)
ha_row = rowAnnotation(df = data.frame(type2 = c(rep("A", 6), rep("B", 6))),
    col = list(type2 = c("A" =  "green", "B" = "orange")), show_legend = FALSE, width = unit(1, "cm"))

ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column)
ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", show_heatmap_legend = FALSE)
ht1 + ht2 + ha_row
控制图例是否可见

Customization of legends

图例本事是可以灵活自定义的. 通过heatmap_legend_param (for heatmap)或 annotation_legend_param (for annotations)为图例设置参数。参数可以设置成下方那样:

  • title: 图例的标题
  • title_gp: 图例标题的图形参数
  • title_position: 标题相对相对于图例的位置, 可用的值有 topcenter, topleft, leftcenter, lefttop.
  • color_bar: 颜色调的类型 i.e. continuous or discrete连续或者离散
  • grid_height: 颜色条格子的高度, 只适用于离散色条
  • grid_width: 颜色条宽度
  • grid_border: 颜色条边界
  • at: break values shown on the legend
  • labels: labels which correspond to the breaks values
  • labels_gp: 图例labels的图形参数
  • nrow and ncol:如果图例太多,他们可以被放进一个数组 . 这两个参数控制图例的行或列的数目
  • legend_direction: 控制图例方向, 可用值有 vertical and horizontal. 对continuous and discrete连续和离散值都生效
  • legend_width and legend_height: 但 color_barcontinuous(连续的),这个连个参数设置图例的width or height

下方的例子改变了label和title的默认图参数:

df = data.frame(type = c(rep("a", 5), rep("b", 5)))
ha = HeatmapAnnotation(df = df, col = list(type = c("a" =  "red", "b" = "blue")),
    annotation_legend_param = list(type = list(title = "TYPE", title_gp = gpar(fontsize = 14),
                                               labels_gp = gpar(fontsize = 8))))
ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha)
ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2",
    heatmap_legend_param = list(title = "Heatmap2", title_gp = gpar(fontsize = 8),
        labels_gp = gpar(fontsize = 14)))
ht1 + ht2

修改默认图参数的例子

你可以通过heatmap_legend_param and annotation_legend_paramatlabels指定break values和 break labels (不管是字符类型值和数值类型值都可以) 注意 at也可以是character break values.

ha = HeatmapAnnotation(df = df, col = list(type = c("a" =  "red", "b" = "blue")),
    annotation_legend_param = list(type = list(title = "TYPE", title_gp = gpar(fontsize = 14),
        labels_gp = gpar(fontsize = 8), at = c("a", "b"), labels = c("A", "B"))))
ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha,
    heatmap_legend_param = list(at = c(-3, 0, 3), labels = c("-three", "zero", "+three")))
ht1 + ht2
image.png

如果注释或矩阵中有许多levels,则可以通过指定nrow或`ncol'参数将所有levels 放入数组中:

ha_chr = rowAnnotation(chr = sample(paste0("chr", 1:20), nrow(mat), replace = TRUE),
    annotation_legend_param = list(chr = list(ncol = 2, title = "chromosome", title_position = "topcenter")),
    width = unit(5, "mm"))
ht1 = Heatmap(mat, name = "ht1")
ht1 + ha_chr
image.png

Or 放置在热图底部:

ha_chr = rowAnnotation(chr = sample(paste0("chr", 1:20), nrow(mat), replace = TRUE),
    annotation_legend_param = list(chr = list(nrow = 2, title = "chr", title_position = "leftcenter")),
    width = unit(5, "mm"))
ht1 = Heatmap(mat, name = "ht1", show_heatmap_legend = FALSE)
draw(ht1 + ha_chr, heatmap_legend_side = "bottom")

image.png

如果你想按列排列离散的图例,请使用direction参数:

ha_chr = rowAnnotation(chr = sample(paste0("chr", 1:20), nrow(mat), replace = TRUE),
    annotation_legend_param = list(chr = list(nrow = 2, title = "chr", title_position = "leftcenter", legend_direction = "vertical")),
    width = unit(5, "mm"))
ht1 = Heatmap(mat, name = "ht1", show_heatmap_legend = FALSE)
draw(ht1 + ha_chr, heatmap_legend_side = "bottom")
按列排列离散图例

如果将color_bar指定为discrete,则可以将离散颜色条用于连续值。 对于包含连续值的简单注释,color_bar也可以设置为discrete.

ha = HeatmapAnnotation(df = data.frame(value = runif(10)),
    col = list(value = colorRamp2(c(0, 1), c("white", "blue"))),
    annotation_legend_param = list(color_bar = "discrete", at = c(0, 0.5, 1)))
Heatmap(mat, name = "ht1", top_annotation = ha, heatmap_legend_param = list(color_bar = "discrete"))
指定颜色离散

有些用户喜欢将图例放在热图的底部.

ht = Heatmap(mat, name = "ht1", heatmap_legend_param = list(legend_direction = "horizontal",
    legend_width = unit(5, "cm"), title_position = "lefttop"))
draw(ht, heatmap_legend_side = "bottom")
图例放置在热图底部

同样的,如果图例是垂直的,图例的高度也可以通过legend_height进行调整

Heatmap(mat, name = "ht1", heatmap_legend_param = list(legend_height = unit(5, "cm")))

image.png

如果要更改所有热图/注释的默认设置,可以通过ht_global_opt()全局设置它.

ht_global_opt(heatmap_legend_title_gp = gpar(fontsize = 16), annotation_legend_labels_gp = gpar(fontface = "italic"))
ha = HeatmapAnnotation(df = data.frame(value = runif(10)),
    col = list(value = colorRamp2(c(0, 1), c("white", "blue"))))
ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha)
ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", heatmap_legend_param = list(title_gp = gpar(fontsize = 8)))
ht1 + ht2
全局更改所有热图/注释的默认设置
ht_global_opt(RESET = TRUE)

Add new legends 增加新的图例

ComplexHeatmap仅生成热图和简单注释的图例。 自定义的图例也可以作为grob对象列表 并通过heatmap_legend_listannotation_legend_list参数来传递。.

grid包提供legendGrob()来构造具有特定样式的图例grob,但样式仍然有限。 对于高级用户,他们可以通过frameGrob()placeGrob()完全构建一个图例grob.

ha = HeatmapAnnotation(points = anno_points(rnorm(10)))
ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", top_annotation = ha, show_heatmap_legend = FALSE)
lgd = legendGrob(c("dots"), pch = 16)
draw(ht1 + ht2, annotation_legend_list = list(lgd))
image.png

从版本1.9.7开始,ComplexHeatmap包提供了一个Legend()函数,它可以生成grob格式的图例(实际上包中的所有图例都是由Legend()函数实现的)。 在下面的示例中,我们有几个包含点的列注释,我们还想显示这些非热图图形的图例.

ha = HeatmapAnnotation(points = anno_points(rnorm(10), gp = gpar(col = rep(2:3, each = 5))))
ht = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", top_annotation = ha)
lgd = Legend(at = c("class1", "class2"), title = "points", type = "points", legend_gp = gpar(col = 2:3))
draw(ht, annotation_legend_list = list(lgd))

image.png

Also check this blog link 获取更多案例.

Session info

sessionInfo()

## R version 3.5.1 Patched (2018-07-24 r75008)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server 2012 R2 x64 (build 9600)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=C                           LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
##  [1] stats4    parallel  grid      stats     graphics  grDevices utils     datasets  methods  
## [10] base     
## 
## other attached packages:
##  [1] dendextend_1.9.0      dendsort_0.3.3        cluster_2.0.7-1       IRanges_2.16.0       
##  [5] S4Vectors_0.20.0      BiocGenerics_0.28.0   HilbertCurve_1.12.0   circlize_0.4.4       
##  [9] ComplexHeatmap_1.20.0 knitr_1.20            markdown_0.8         
## 
## loaded via a namespace (and not attached):
##  [1] mclust_5.4.1           Rcpp_0.12.19           mvtnorm_1.0-8          lattice_0.20-35       
##  [5] png_0.1-7              class_7.3-14           assertthat_0.2.0       mime_0.6              
##  [9] R6_2.3.0               GenomeInfoDb_1.18.0    plyr_1.8.4             evaluate_0.12         
## [13] ggplot2_3.1.0          highr_0.7              pillar_1.3.0           GlobalOptions_0.1.0   
## [17] zlibbioc_1.28.0        rlang_0.3.0.1          lazyeval_0.2.1         diptest_0.75-7        
## [21] kernlab_0.9-27         whisker_0.3-2          GetoptLong_0.1.7       stringr_1.3.1         
## [25] RCurl_1.95-4.11        munsell_0.5.0          compiler_3.5.1         pkgconfig_2.0.2       
## [29] shape_1.4.4            nnet_7.3-12            tidyselect_0.2.5       gridExtra_2.3         
## [33] tibble_1.4.2           GenomeInfoDbData_1.2.0 viridisLite_0.3.0      crayon_1.3.4          
## [37] dplyr_0.7.7            MASS_7.3-51            bitops_1.0-6           gtable_0.2.0          
## [41] magrittr_1.5           scales_1.0.0           stringi_1.2.4          XVector_0.22.0        
## [45] viridis_0.5.1          flexmix_2.3-14         bindrcpp_0.2.2         robustbase_0.93-3     
## [49] fastcluster_1.1.25     HilbertVis_1.40.0      rjson_0.2.20           RColorBrewer_1.1-2    
## [53] tools_3.5.1            fpc_2.1-11.1           glue_1.3.0             trimcluster_0.1-2.1   
## [57] DEoptimR_1.0-8         purrr_0.2.5            colorspace_1.3-2       GenomicRanges_1.34.0  
## [61] prabclus_2.2-6         bindr_0.1.1            modeltools_0.2-22

相关文章

网友评论

    本文标题:🎨[可视化|R包]ComplexHeatmap学习笔记⑤Heat

    本文链接:https://www.haomeiwen.com/subject/eekdhctx.html