R语言绘图转录组SAS学习笔记

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

2019-07-07  本文已影响0人  郑宝童
  1. ComplexHeatmap学习笔记①Introduction to ComplexHeatmap package
  2. ComplexHeatmap学习笔记②Making A Single Heatmap
  3. ComplexHeatmap学习笔记③Making A List of Heatmaps
  4. ComplexHeatmap学习笔记④Heatmap Annotations
  5. ComplexHeatmap学习笔记⑤Heatmap and Annotation Legends

Heatmap Decoration 热图装饰


热图/热图列表(heatmap/heatmap list)的每个组件都有一个名称(唯一ID)。 您可以通过指定热图/注释名称向任何viewport添加图形。.

我们先生成一个几乎包含所有类型的热图组件的图形.

library(ComplexHeatmap)

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_column1 = HeatmapAnnotation(points = anno_points(rnorm(10)))
ht1 = Heatmap(mat, name = "ht1", km = 2, row_title = "Heatmap 1", column_title = "Heatmap 1", 
    top_annotation = ha_column1)

ha_column2 = HeatmapAnnotation(df = data.frame(type = c(rep("a", 5), rep("b", 5))),
    col = list(type = c("a" = "red", "b" = "blue")))
ht2 = Heatmap(mat, name = "ht2", row_title = "Heatmap 2", column_title = "Heatmap 2",
    bottom_annotation = ha_column2)
生成几乎包含所有组件的热图

The components (viewports) that have names are:

decorate_* functions decorate_*类函数

基本上,您可以通过seekViewport()来是定位这些组件,但是为了隐藏太低级别的细节,ComplexHeatmap包提供了decorate_*系列函数,可以很容易地将图形添加到不同的组件中。.

下面的代码添加了注释名,在热图中标记一个网格(grid ),并用两个矩形将第一个列的聚类分开.

ht_list = draw(ht_list, row_title = "Heatmap list", column_title = "Heatmap list", 
    heatmap_legend_side = "right", annotation_legend_side = "left")
decorate_annotation("points", {
    grid.text("points", unit(0, "npc") - unit(2, "mm"), 0.5, 
        default.units = "npc", just = "right")
})

decorate_heatmap_body("ht1", {
    grid.text("outlier", 1.5/10, 2.5/4, default.units = "npc")
    grid.lines(c(0.5, 0.5), c(0, 1), gp = gpar(lty = 2, lwd = 2))
}, slice = 2)

decorate_column_dend("ht1", {
    tree = column_dend(ht_list)$ht1
    ind = cutree(as.hclust(tree), k = 2)[order.dendrogram(tree)]

    first_index = function(l) which(l)[1]
    last_index = function(l) { x = which(l); x[length(x)] }
    x1 = c(first_index(ind == 1), first_index(ind == 2)) - 1
    x2 = c(last_index(ind == 1), last_index(ind == 2))
    grid.rect(x = x1/length(ind), width = (x2 - x1)/length(ind), just = "left",
        default.units = "npc", gp = gpar(fill = c("#FF000040", "#00FF0040"), col = NA))
})

decorate_row_names("ht2", {
    grid.rect(gp = gpar(fill = "#FF000040"))
}, slice = 2)

decorate_row_title("ht1", {
    grid.rect(gp = gpar(fill = "#00FF0040"))
}, slice = 1)

decorate_annotation("points", {
    grid.lines(c(0, 1), unit(c(0, 0), "native"), gp = gpar(col = "red"))
})

decorate_*类函数,给热图添加各种组件

anno_points(), anno_barplot() and anno_boxplot()这些函数创建的注释, “native” unit 可以被用在 decoration code中.

Add annotation names 添加注释名

默认情况下,注释的名称不会与热图注释一起绘制。 原因是如果绘制了注释名称,它们将位于其他热图组件的区域中,这将使得热图布局的调整变得困难。 HeatmapAnnotation() 为添加注释名称提供了一个不那么完美的[solution],但是,因为你可以通过名称定位到热图列表中的任何组件,实际上手动添加注释名称并不困难.

以下代码在列注释的两侧添加注释名称。 缺点是因为没有为注释名称设计特定组件,如果注释名称太长,它将超过图形区域(但这个问题可以通过一些技巧来解决,请参阅[** Examples **]vignette).

df = data.frame(type1 = c(rep("a", 5), rep("b", 5)),
                type2 = c(rep("A", 3), rep("B", 7)))
ha = HeatmapAnnotation(df, col = list(type1 = c("a" = "red", "b" = "blue"),
                                      type2 = c("A" = "green", "B" = "orange")))
Heatmap(mat, name = "ht", top_annotation = ha)
for(an in colnames(df)) {
    decorate_annotation(an, {
        # annotation names on the right
        grid.text(an, unit(1, "npc") + unit(2, "mm"), 0.5, default.units = "npc", just = "left")
        # annotation names on the left
        grid.text(an, unit(0, "npc") - unit(2, "mm"), 0.5, default.units = "npc", just = "right")
    })
}
添加注释名

Visualize distributions 可视化分布

使用热图装饰(heatmap decorations),实际上您可以基于热图设计新图形。 以下是一个例子::

为了可视化在矩阵或列表中的列的分布,有时我们使用boxplot or beanplot。在这里,我们还可以使用颜色映射到密度值,以可视化每列(或每个列表元素)中的分布。有时候它可以给你的数据展示出一个明晰可读的图像。

这个包有一个densityHeatmap()函数,其用法非常简单:

matrix = matrix(rnorm(100), 10); colnames(matrix) = letters[1:10]
ha = HeatmapAnnotation(df = data.frame(anno = rep(c("A", "B"), each = 5)),
    col = list(anno = c("A" = "green", "B" = "orange")),
    points = anno_points(runif(10)))
densityHeatmap(matrix, anno = ha)

可视化分布

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] GetoptLong_0.1.7      dendextend_1.9.0      dendsort_0.3.3        cluster_2.0.7-1      
##  [5] IRanges_2.16.0        S4Vectors_0.20.0      BiocGenerics_0.28.0   HilbertCurve_1.12.0  
##  [9] circlize_0.4.4        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          stringr_1.3.1          RCurl_1.95-4.11       
## [25] munsell_0.5.0          compiler_3.5.1         pkgconfig_2.0.2        shape_1.4.4           
## [29] nnet_7.3-12            tidyselect_0.2.5       gridExtra_2.3          tibble_1.4.2          
## [33] GenomeInfoDbData_1.2.0 viridisLite_0.3.0      crayon_1.3.4           dplyr_0.7.7           
## [37] MASS_7.3-51            bitops_1.0-6           gtable_0.2.0           magrittr_1.5          
## [41] scales_1.0.0           stringi_1.2.4          XVector_0.22.0         viridis_0.5.1         
## [45] flexmix_2.3-14         bindrcpp_0.2.2         robustbase_0.93-3      fastcluster_1.1.25    
## [49] HilbertVis_1.40.0      rjson_0.2.20           RColorBrewer_1.1-2     tools_3.5.1           
## [53] fpc_2.1-11.1           glue_1.3.0             trimcluster_0.1-2.1    DEoptimR_1.0-8        
## [57] purrr_0.2.5            colorspace_1.3-2       GenomicRanges_1.34.0   prabclus_2.2-6        
## [61] bindr_0.1.1            modeltools_0.2-22
上一篇下一篇

猜你喜欢

热点阅读