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::Heatmap主体热图参数
绘制热图首先需要准备已经标准化的二维矩阵作为输入,可以出图后我们就需要对主体部分进行修改,一些比较常用的参数包括单元格(颜色,边界,大小等),行/列聚类(是否聚类,是否展示聚类树,聚类树的位置,大小,颜色等),行/列注释(行/列名,位置,大小,颜色,角度等),图注信息等。
在ComplexHeatmap
主要使用了grid
绘图系统,使用gpar
调整热图参数:
-
col
控制颜色 -
fill
控制填充色 -
alpha
控制透明度 -
lty
线的类型 -
lwd
线宽 -
fontsize
字符的大小
而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")
等
控制聚类信息的参数:
-
cluster_columns
,cluster_rows
指定是否聚类 -
show_row_dend
,show_column_dend
是否展示聚类结果(进行聚类) -
row_names_side
,column_names_side
聚类树位置("left","right","top","bottom") -
clustering_method_rows
,clustering_method_columns
聚类方法 -
row_dend_width
,column_dend_height
聚类树宽/高 -
row_km
,column_km
按聚类结果拆分热图
控制单元格的参数:
-
col = circlize::colorRamp2(c(-2, 0, 2), c("navy", "white", "firebrick3"))
颜色。 -
rect_gp = gpar(col= "white")
指定单元格边界颜色 -
border
指定热图边界颜色
控制行/列标签的参数:
-
row_labels
,column_labels
行/列标签 -
show_column_names
,show_row_names
是否显示列/行名 -
row_names_side``coolumn_names_side
显示行/列名的位置("left","right","top","bottom") -
row_names_gp``column_names_gp
控制行/列名大小(gpar(fontsize = 12)) -
row_names_rot``column_names_rot
控制行/列名的角度
控制整体图形的参数:
-
heatmap_width = unit(1, "npc")
,heatmap_height = unit(1, "npc")
图形高/宽度 -
row_gap = unit(1, "mm")
,column_gap = unit(1, "mm")
子图空隙
控制图注的参数:
-
show_heatmap_legend
是否展示图注 -
heatmap_legend_param
图注参数(list(at = c(-2, 0, 2), labels =c(-2, 0, 2))#需要制定at和labels的list)
控制注释的参数:
top_annotation = NULL
bottom_annotation = NULL
left_annotation = NULL
right_annotation = NULL
HeatmapAnnotation热图注释
对于ComplexHeatmap
来说,其主旨就是绘制复杂热图,所以对热图的注释以及拼图极其重视,对于*_annotation
来说包括包括各种类型图形和文字丰富结果,并且还支持使用+
(行)和%v%
(列)对多个热图进行组装。热图注释是热图的重要组成部分,它显示与热图中的行或列关联的其他信息。 ComplexHeatmap
软件包为设置注释和定义新的注释图形提供了非常灵活的支持。注释可以放在热图的四个侧面,由top_annotation
,bottom_annotation
,left_annotation
和right_annotation
参数。
四个参数的值应在HeatmapAnnotation
类中,并应由HeatmapAnnotation()
函数构造,如果是行批注,则应由rowAnnotation()
函数构造。(rowAnnotation()
和columnAnnotation()
只是一个与HeatmapAnnotation(…,which=“row/column”)
相同的辅助函数)。HeatmapAnnotation
的简单用法如下。
-
simple annotation
最常用的注释样式,它是类似于热图或网格的图形,其中颜色用于映射到注释值
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") # 指定单元格大小
)
-
anno_block
块注释更像一个颜色块,用于在拆分热图的行或列时标识组别。
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)
-
anno_points
绘制散点图,axis_param
控制坐标轴参数,需要制定side(指定坐标轴展示的位置),at(指定坐标),labels(指定标签)
# 横向散点图
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
))
)
-
anno_lines
绘制线图,可以使用add_points = TRUE
加入散点图,也可以使用smooth = TRUE
绘制平滑后的曲线,当设置smooth = TRUE
会默认增加点图
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))
-
anno_barplot
用于绘制条形图,输入数据为二维矩阵时,则转变为堆积图
# 绘制柱状图
ha = HeatmapAnnotation(foo = anno_barplot(seq(-5, 5), baseline = "min"))
# 绘制堆积图
ha = HeatmapAnnotation(foo = anno_barplot(matrix(nc = 2, c(1:10, 10:1))))
-
anno_boxplot
绘制箱线图
ha = HeatmapAnnotation(foo = anno_boxplot(m, height = unit(4, "cm"),
gp = gpar(fill = 1:10)))
-
anno_histogram
绘制直方图
# n_breaks控制显示的柱子数量
ha = rowAnnotation(foo = anno_histogram(m, n_breaks = 20))
-
anno_density
绘制密度图
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"))
})