pheatmap交互
最近ComplexHeatmap
更新可以將pheatmap
對象進行參數(shù)轉(zhuǎn)換。新增的ComplexHeatmap::pheatmap()
的功能與pheatmap::pheatmap()
完全一致辖佣,其返回一個Formal class Heatmap
對象,這意味著我們可以使用ComplexHeatmap
中的其他函數(shù)對其進行操作并繪制更加復雜的熱圖。
# 模擬數(shù)據(jù)
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主體熱圖參數(shù)
繪制熱圖首先需要準備已經(jīng)標準化的二維矩陣作為輸入蚁堤,可以出圖后我們就需要對主體部分進行修改幔戏,一些比較常用的參數(shù)包括單元格(顏色,邊界旬渠,大小等)俱萍,行/列聚類(是否聚類,是否展示聚類樹告丢,聚類樹的位置枪蘑,大小,顏色等),行/列注釋(行/列名岳颇,位置照捡,大小,顏色话侧,角度等)栗精,圖注信息等。
在ComplexHeatmap
主要使用了grid
繪圖系統(tǒng)瞻鹏,使用gpar
調(diào)整熱圖參數(shù):
-
col
控制顏色 -
fill
控制填充色 -
alpha
控制透明度 -
lty
線的類型 -
lwd
線寬 -
fontsize
字符的大小
而unit
參數(shù)則主要控制一些距離參數(shù)悲立,包括子圖間的空隙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")
等
控制聚類信息的參數(shù):
-
cluster_columns
薪夕,cluster_rows
指定是否聚類 -
show_row_dend
,show_column_dend
是否展示聚類結(jié)果(進行聚類) -
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
按聚類結(jié)果拆分熱圖
控制單元格的參數(shù):
-
col = circlize::colorRamp2(c(-2, 0, 2), c("navy", "white", "firebrick3"))
顏色粤策。 -
rect_gp = gpar(col= "white")
指定單元格邊界顏色 -
border
指定熱圖邊界顏色
控制行/列標簽的參數(shù):
-
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
控制行/列名的角度
控制整體圖形的參數(shù):
-
heatmap_width = unit(1, "npc")
,heatmap_height = unit(1, "npc")
圖形高/寬度 -
row_gap = unit(1, "mm")
丙唧,column_gap = unit(1, "mm")
子圖空隙
控制圖注的參數(shù):
-
show_heatmap_legend
是否展示圖注 -
heatmap_legend_param
圖注參數(shù)(list(at = c(-2, 0, 2), labels =c(-2, 0, 2))#需要制定at和labels的list)
控制注釋的參數(shù):
top_annotation = NULL
bottom_annotation = NULL
left_annotation = NULL
right_annotation = NULL
HeatmapAnnotation熱圖注釋
對于ComplexHeatmap
來說愈魏,其主旨就是繪制復雜熱圖,所以對熱圖的注釋以及拼圖極其重視想际,對于*_annotation
來說包括包括各種類型圖形和文字豐富結(jié)果培漏,并且還支持使用+
(行)和%v%
(列)對多個熱圖進行組裝。熱圖注釋是熱圖的重要組成部分胡本,它顯示與熱圖中的行或列關聯(lián)的其他信息牌柄。 ComplexHeatmap
軟件包為設置注釋和定義新的注釋圖形提供了非常靈活的支持。注釋可以放在熱圖的四個側(cè)面侧甫,由top_annotation
珊佣,bottom_annotation
蹋宦,left_annotation
和right_annotation
參數(shù)。
四個參數(shù)的值應在HeatmapAnnotation
類中咒锻,并應由HeatmapAnnotation()
函數(shù)構造冷冗,如果是行批注,則應由rowAnnotation()
函數(shù)構造虫碉。(rowAnnotation()
和columnAnnotation()
只是一個與HeatmapAnnotation(…贾惦,which=“row/column”)
相同的輔助函數(shù))。HeatmapAnnotation
的簡單用法如下敦捧。
-
simple annotation
最常用的注釋樣式须板,它是類似于熱圖或網(wǎng)格的圖形,其中顏色用于映射到注釋值
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
控制坐標軸參數(shù)甜奄,需要制定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
用于繪制條形圖烟阐,輸入數(shù)據(jù)為二維矩陣時,則轉(zhuǎn)變?yōu)槎逊e圖
# 繪制柱狀圖
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控制顯示的柱子數(shù)量
ha = rowAnnotation(foo = anno_histogram(m, n_breaks = 20))
-
anno_density
繪制密度圖
ha = rowAnnotation(foo = anno_density(m))
示例
# 示例數(shù)據(jù)
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)
# 行注釋 右側(cè) 條形圖
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"))
})