- 单个注释
- 使用注释函数进行注释
- 空白注释
- 分组注释
- 点的注释
- 线条注释
- 柱状图注释
- 箱式图注释
- 条形图注释
- 密度图注释
- 峰峦图注释
- 水平注释
- 文字注释
- 注释标记
- 总结注释
- 放大注释
- 多重注释
- 注释图的大小
上一篇我们介绍了单个热图的绘制。这次我们来介绍对于热图注释的绘制。
-
热图的注释主要是通过
HeatmapAnnotation
来完成的。 -
complexheatmap
通过top_annotation
,bottom_annotation
,left_annotation
andright_annotation
来对热图的上下作用进行注释。 -
注释的结果是需要先通过
HeatmapAnnotation
构造好的。然后在传入注释的位置。
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.
## ========================================
set.seed(123)
mat = matrix(rnorm(100), 10)
rownames(mat) = paste0("R", 1:10)
colnames(mat) = paste0("C", 1:10)
column_ha = HeatmapAnnotation(foo1 = runif(10), bar1 = anno_barplot(runif(10)))
row_ha = rowAnnotation(foo2 = runif(10), bar2 = anno_barplot(runif(10)))
Heatmap(mat, name = "mat", top_annotation = column_ha, right_annotation = row_ha)

注释的显示顺序也是基于之前定义的来的。
单个注释
所谓的单个注释就是热图最基本的注释。单个注释使用的是HeatmapAnnotation
,通过变量名对数据进行赋值,通过col
来对不同的注释进行配色。通过na_col
指定缺失值的颜色。
## 绘制foo和bar两个变量的注释。
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.
## ========================================
col_fun = colorRamp2(c(0, 5, 10), c("blue", "white", "red"))
ha = HeatmapAnnotation(
foo = c(1:4, NA, 6:10),
bar = c(NA, sample(letters[1:3], 9, replace = TRUE)),
col = list(foo = col_fun,
bar = c("a" = "red", "b" = "green", "c" = "blue")
),
na_col = "black"
)

fig1
一个变量如果通过
cbind
来进行链接的话,则可以显示两列。如果分别指定变量名,那就是各自命名,如果没有命名,则可以就是之前的命名
通过
simple_anno_size
可以指定变量的高度。
##如果cbind里面没有命名a,b。则会在两个数据注释中间显示foo
ha = HeatmapAnnotation(
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")
),
simple_anno_size = unit(1, "cm")
)

fig2
我们可以通过
gp
来指定边框的颜色。如果是使用border
的话,那么接受逻辑值,只是是否显示边框。
ha = HeatmapAnnotation(
foo = 1:10,
bar = sample(letters[1:3], 10, replace = TRUE),
col = list(foo = col_fun,
bar = c("a" = "red", "b" = "green", "c" = "blue")
),
gp = gpar(col = "black")
)

fig3
使用注释函数进行注释
通过使用anno_simple
函数,可以对数据进行个性化的注释。主要是函数里面的pch
选择数据的形状;pt_gp
自定义数据的参数;pt_size
自定义数据的大小;
library(circlize)
set.seed(123)
pvalue = 10^-runif(10, min = 0, max = 3)
is_sig = pvalue < 0.01
pch = rep("*", 10)
pch[!is_sig] = NA
# color mapping for -log10(pvalue)
pvalue_col_fun = colorRamp2(c(0, 2, 3), c("green", "white", "red"))
ha = HeatmapAnnotation(
pvalue = anno_simple(-log10(pvalue), col = pvalue_col_fun, pch = pch),
annotation_name_side = "left")
ht = Heatmap(matrix(rnorm(100), 10), name = "mat", top_annotation = ha)
# now we generate two legends, one for the p-value
# see how we define the legend for pvalue
lgd_pvalue = Legend(title = "p-value", col = pvalue_col_fun, at = c(0, 1, 2, 3),
labels = c("1", "0.1", "0.01", "0.001"))
# and one for the significant p-values
lgd_sig = Legend(pch = "*", type = "points", labels = "< 0.01")
# these two self-defined legends are added to the plot by `annotation_legend_list`
draw(ht, annotation_legend_list = list(lgd_pvalue, lgd_sig))

空白注释
空白注释就是画一个空白的边框。我们可以通过decorate_annotation
来给边框当中添加数据。
ha = HeatmapAnnotation(foo = anno_empty(border = TRUE))

empty
分组注释
通过anno_block
来对数据进行分组注释给数据进行分组
Heatmap(matrix = matrix(rnorm(100), 10),
top_annotation = HeatmapAnnotation(foo = anno_block(
gp = gpar(fill = 2:4), labels = c("g1", "g2", "g3"),
labels_gp = gpar(col = "white", fontsize = 10)
)), column_km = 3)

点的注释
通过anno_point
来对点进行注释。如果是多组数据的话,可以形成一个数据来进行显示
ha = HeatmapAnnotation(foo = anno_points(matrix(runif(20), nc = 2),
pch = 1:2, gp = gpar(col = 2:3)))

point
我们可以对坐标轴进行自定义
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")
)))

线条注释
通过anno_lines
我们可以进行线条的注释,其中基本的设置都差不多
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)))

figure
同时我们也可以利用anno_lines
进行线性拟合。
ha = HeatmapAnnotation(foo = anno_lines(runif(10), smooth = TRUE))

柱状图注释
通过anno_barplot
我们可以对条形图进行注释。 我们可以通过bar_width
来定义柱子的宽度;通过baseline
柱子的起始位置。如果是多组数据的话,则会按照堆砌图进行排列。
ha = HeatmapAnnotation(foo = anno_barplot(1:10, bar_width = 1))

ha = HeatmapAnnotation(foo = anno_barplot(seq(-5, 5), baseline = "min"))
ha = HeatmapAnnotation(foo = anno_barplot(seq(-5,5), baseline = 0))


m = matrix(runif(4*10), nc = 4)
m = t(apply(m, 1, function(x) x/sum(x)))
ha = HeatmapAnnotation(foo = anno_barplot(m, gp = gpar(fill = 2:5),
bar_width = 1, height = unit(6, "cm")))

箱式图注释
通过anno_boxplot
来可视化树箱式图,对于小数据而言,我们可以通过箱式图来进行可视化,但是如果是大数据的话,箱式图不是一个很好的可视化方式。
set.seed(12345)
m = matrix(rnorm(100), 10)
ha = HeatmapAnnotation(foo = anno_boxplot(m, height = unit(4, "cm")))

同样的我们可以通过gp
自定义颜色, 通过boxwidth
自定义箱宽度;通过height
自定义整个图形的高度。通过outlier
自定义是否显示离群值
ha = HeatmapAnnotation(foo = anno_boxplot(m, height = unit(4, "cm"),
gp = gpar(fill = 1:10)))
条形图注释
通过anno_histogram
我们来定义数据的分布
m = matrix(rnorm(1000), nc = 100)
ha = rowAnnotation(foo = anno_histogram(m)) # apply `m` on rows

对于柱状图的调整,我们可以通过n_breaks
调整柱子的个数。
密度图注释
通过anno_density
我们可以来对密度图进行注释。
ha = HeatmapAnnotation(foo = anno_density(m))

通过type
参数,我们可以改变密度图的可视化方式,如果是violin
则为小提琴图。如果是heatmap
则为热图来显示数据。一般大数据的可视化,可以通过heatmap
来可视化
m2 = matrix(rnorm(50*10), nrow = 50)
ha = rowAnnotation(foo = anno_density(m2, type = "heatmap", width = unit(6, "cm"),
heatmap_colors = c("white", "orange")))
峰峦图注释
通过anno_joyplot
。 注意如果输入是矩阵,则anno_joyplot始终应用于列。其中x坐标对应于1:nrow(矩阵),矩阵中的每列对应于一个分类中的分布 如果是列表,其中每个数据包含有两列,对应于x坐标和y坐标。
m = matrix(rnorm(1000), nc = 10)
lt = apply(m, 2, function(x) data.frame(density(x)[c("x", "y")]))
ha = rowAnnotation(foo = anno_joyplot(lt, width = unit(4, "cm"),
gp = gpar(fill = 1:10), transparency = 0.75))

水平注释
水平注释通过anno_horizon
来实现。需要注释的是这个注释只能在行进行注释。
lt = lapply(1:20, function(x) cumprod(1 + runif(1000, -x/100, x/100)) - 1)
ha = rowAnnotation(foo = anno_horizon(lt))

数据我们可以通过x/max(abs(x))来进行标准化,通过gpar(pos_fill, neg_fill)
来进行颜色标注。我们可以指定单一的颜色,也可以指定每一列的颜色。同时可以通过negative_from_top
来指定数据是否反转。
lt = lapply(1:20, function(x) cumprod(1 + runif(1000, -x/100, x/100)) - 1)
ha = rowAnnotation(foo = anno_horizon(lt))

ha = rowAnnotation(foo = anno_horizon(lt,
gp = gpar(pos_fill = "orange", neg_fill = "darkgreen")))

ha = rowAnnotation(foo = anno_horizon(lt,
gp = gpar(pos_fill = rep(c("orange", "red"), each = 10),
neg_fill = rep(c("darkgreen", "blue"), each = 10))))

ha = rowAnnotation(foo = anno_horizon(lt, negative_from_top = TRUE))

文字注释
anno_text
可以来进行文字注释,其中我们可以通过location
以及just
来文字位置进行对齐,通过rot
文字的方向进行调整。在gpar
中可以通过fill
设置填充色; col
设置字体颜色; border
设置边框颜色; fontsize
设置字体大小。
ha = rowAnnotation(foo = anno_text(month.name, location = 0.5, just = "center",
gp = gpar(fill = rep(2:4, each = 4), col = "white", border = "black"),
width = max_text_width(month.name)*1.2))

注释标记
有时候我们需要把其中某几行进行标记,其他的不显示。这个时候可以用到anno_mark
在这个函数中,通过at
指定标记的位置; lables
指定标记的名字;
m = matrix(rnorm(1000), nrow = 100)
ha = rowAnnotation(foo = anno_mark(at = c(1:4, 20, 60, 97:100), labels = month.name[1:10]))
Heatmap(m, name = "mat", cluster_rows = FALSE, right_annotation = ha)

总结注释
anno_summary
对于单个变量的热图进行统计注释。一般来说我们可以对单个分类变量进行``柱状图注释,也可以对单个连续性变量进行
箱式图注释。总结注释的时候需要一个
split`参数来说明注释分几个组
m = matrix(rnorm(50*10), nrow = 50)
ht_list = Heatmap(m, name = "main_matrix")
ha = HeatmapAnnotation(summary = anno_summary(height = unit(3, "cm")))
v = sample(letters[1:2], 50, replace = TRUE)
ht_list = ht_list + Heatmap(v, name = "mat1", top_annotation = ha, width = unit(1, "cm"))
ha = HeatmapAnnotation(summary = anno_summary(gp = gpar(fill = 2:3),
height = unit(3, "cm")))
v = rnorm(50)
ht_list = ht_list + Heatmap(v, name = "mat2", top_annotation = ha, width = unit(1, "cm"))
split = sample(letters[1:2], 50, replace = TRUE)
lgd_boxplot = Legend(labels = c("group a", "group b"), title = "group",
legend_gp = gpar(fill = c("red", "blue")))
draw(ht_list, row_split = split, ht_gap = unit(5, "mm"),
heatmap_legend_list = list(lgd_boxplot))

放大注释
有些时候我们想要放大某一亚组的数据。这个时候就可以用到anno_zoom
函数了
set.seed(123)
m = matrix(rnorm(100*10), nrow = 100)
subgroup = sample(letters[1:3], 100, replace = TRUE, prob = c(1, 5, 10))
rg = range(m)
panel_fun = function(index, nm) {
pushViewport(viewport(xscale = rg, yscale = c(0, 2)))
grid.rect()
grid.xaxis(gp = gpar(fontsize = 8))
grid.boxplot(m[index, ], pos = 1, direction = "horizontal")
popViewport()
}
anno = anno_zoom(align_to = subgroup, which = "row", panel_fun = panel_fun,
size = unit(2, "cm"), gap = unit(1, "cm"), width = unit(4, "cm"))
Heatmap(m, name = "mat", right_annotation = rowAnnotation(foo = anno), row_split = subgroup)

其中有几个主要的参数: align_to
定义如何对应所在热图的绘图区域。 panel_fun
一个自定义函数,这个函数当中必须包括一个index。这个index对应的是该数据狂所对应的行/列。同时也可以包括第二个参数nm
。这个参数对应的是align_to
size
绘图的大小 gap
热图和注释图之间间隔的大小。
多重注释
我们把上述多个注释的函数放到注释函数里面就可以得到多重注释函数。我们可以通过show_legend
来决定是否显示注释;通过gp
自定义边框;通过border
决定显示哪个边框;通过gap
决定注释之间的宽度。
ha = HeatmapAnnotation(foo = 1:10,
bar = cbind(1:10, 10:1),
pt = anno_points(1:10),
show_legend = c("bar" = FALSE),
gp = gpar(col = "red"),
border = c(foo = T),
gap = unit(2, "mm")
)
Heatmap(matrix(rnorm(100), 10), name = "mat", top_annotation = ha)

注释图的大小
默认情况下。系统会自己调整大小的。如果觉得不满意。我们可以通过height
; width
; annotation_height
; annotation_width
来修改其高度和宽度。
网友评论