pheatmap
是繪制熱圖的經(jīng)典R包视搏。其中一些細(xì)節(jié)參數(shù)設(shè)置趋艘,之前每次遇到都是網(wǎng)上搜索。這次系統(tǒng)整理下常用用法凶朗,為以后繪圖提供方便瓷胧。
0、示例數(shù)據(jù)與R包加載
1棚愤、聚類相關(guān)參數(shù)
2搓萧、熱圖的顏色
3、行宛畦,列的注釋
4瘸洛、熱圖的格子相關(guān)
5、行名與列名的調(diào)整
6次和、熱圖的分割
7反肋、轉(zhuǎn)為ggplot2對象
8、按行按列歸一化
0踏施、示例數(shù)據(jù)與R包加載
(1)模擬示例數(shù)據(jù)
- 假設(shè)有兩組樣本石蔗,每組5個的30個基因的表達(dá)數(shù)據(jù)(15個上調(diào),15個下調(diào))
exp = matrix(rnorm(300), nrow = 30, ncol = 10)
exp[1:15, 1:5] = exp[1:15, 1:5] + matrix(rnorm(75,mean = 4), nrow = 15, ncol = 5)
exp[16:30, 6:10] = exp[16:30, 6:10] + matrix(rnorm(75,mean = 3), nrow = 15, ncol = 5)
exp = round(exp, 2)
colnames(exp) = paste("Sample", 1:10, sep = "")
rownames(exp) = paste("Gene", 1:30, sep = "")
head(exp)
# Sample1 Sample2 Sample3 Sample4 Sample5 Sample6 Sample7 Sample8 Sample9 Sample10
# Gene1 0.89 5.00 2.37 4.59 2.71 -0.89 0.21 -0.98 -0.41 0.19
# Gene2 4.05 5.58 3.51 5.45 5.47 -0.77 2.34 -0.67 0.83 -3.54
# Gene3 5.39 6.53 5.25 4.08 5.76 0.98 -0.56 -0.37 -0.56 1.21
# Gene4 4.16 2.29 2.58 3.87 5.17 0.92 -0.80 1.00 -1.23 0.29
# Gene5 3.66 2.30 2.54 2.90 5.37 -0.29 2.29 0.10 -0.33 0.81
# Gene6 2.07 4.19 3.87 1.82 4.82 -0.49 0.37 1.48 0.01 -0.22
(2)加載R包
# install.packages("pheatmap")
library(pheatmap)
packageVersion("pheatmap")
# [1] ‘1.0.12’
(3)基礎(chǔ)繪圖
pheatmap(exp)
1畅形、聚類相關(guān)參數(shù)
如上圖默認(rèn)會分別對行养距、列計算兩兩間的距離,再進(jìn)行聚類
1.1 聚類算法
- 對于兩兩間距離計算參數(shù):
clustering_distance_rows=
日熬,clustering_distance_cols=
棍厌。默認(rèn)為"euclidean"
,備選方法有"correlation"
- 聚類方法的參數(shù):
clustering_method=
。默認(rèn)為"average"
耘纱,備選方法有"ward.D", "ward.D2", "single", "complete", "mcquitty" (= WPGMA), "median" (= WPGMC) or "centroid" (= UPGMC)
1.2 不聚類
pheatmap(exp, cluster_row = FALSE)
1.3 聚類但不想顯示樹
pheatmap(exp, treeheight_row = 0)
其實
treeheight_row
參數(shù)是用來調(diào)整樹的顯示尺寸的敬肚;設(shè)置為0,也就是不顯示樹了束析。
1.4 提取熱圖的表達(dá)矩陣
由于聚類會調(diào)整原始數(shù)據(jù)的行列順序帘皿,如果想要獲得熱圖里的行列順序數(shù)據(jù),可如下調(diào)整
ph = pheatmap(exp)
ph$tree_row$order
ph$tree_col$order
ph_exp = exp[ph$tree_row$order, ph$tree_col$order]
ph_exp[1:4,1:4]
# Sample3 Sample2 Sample4 Sample1
# Gene2 3.51 5.58 5.45 4.05
# Gene12 6.12 5.19 4.04 3.73
# Gene3 5.25 6.53 4.08 5.39
# Gene10 5.36 4.47 4.42 4.54
2畸陡、熱圖的顏色
color=
#Default
colours = colorRampPalette(rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(100)
str(colours)
# chr [1:100] "#4575B4" "#4979B6" "#4E7DB8" "#5282BB" "#5786BD" "#5C8BBF" "#608FC2" ...
# 個性化修改
colours = colorRampPalette(c("navy", "white", "firebrick3"))(10)
str(colours)
# chr [1:10] "#3288BD" "#5FA2CB" "#8DBCDA" "#BAD7E9" "#E8F1F7" "#FAE9EB" "#F1BEC4" ...
pheatmap(exp, color = colours)
#colours = colorRampPalette(c("#3288bd", "white", "#d53e4f"))(10)
3鹰溜、行,列的注釋
- 為
annotation_col=
丁恭,annotation_row=
參數(shù)提供data.frame曹动,行名與表達(dá)矩陣的行名與列名相同;列的內(nèi)容為分組信息牲览。
# 構(gòu)建列注釋信息(行名與表達(dá)矩陣的列名col保持一致)
annotation_col = data.frame(
group = rep(c("Group_A", "Group_B"), each = 5),
row.names = colnames(exp))
head(annotation_col)
# 構(gòu)建行注釋信息(行名與表達(dá)矩陣的行名row保持一致)
annotation_row = data.frame(
Type = rep(c("Up", "Down"), each = 15),
row.names = rownames(exp))
head(annotation_row)
pheatmap(exp,
annotation_col = annotation_col,
annotation_row = annotation_row)
- 為
ann_colors=
參數(shù)提供一個list對象墓陈,可修改注釋分組的顏色
#修改注釋標(biāo)簽的顏色
ann_colors = list(
group = c(Group_A = "#e66101", Group_B = "#5e3c99"),
Type = c(Up = "#e7298a", Down = "#66a61e"))
pheatmap(exp,
annotation_col = annotation_col,
annotation_row = annotation_row,
annotation_colors = ann_colors)
4、熱圖的格子相關(guān)
- 格子的邊框顏色
border_color=
第献;無邊框border=F
- 格子的長寬
cellwidth = 15
,cellheight = 12
pheatmap(exp, border_color = "white",
cellwidth = 9, cellheight = 9)
- 格子內(nèi)的文本注釋
pheatmap(exp, display_numbers = TRUE,
number_color = "blue", number_format = "%.1e") #default "%.2f"
pheatmap(exp, display_numbers = matrix(ifelse(exp > 5, "*", ""),
nrow(exp)))
pheatmap(exp, display_numbers = matrix(ifelse(exp > 5, exp, ""),
nrow(exp)))
5贡必、行名與列名的調(diào)整
- (1)不顯示行名與列名
pheatmap(exp,show_rownames=F,show_colnames=F)
- (2)僅特定顯示部分行名
注意:由于聚類的原因,一定要注意對應(yīng)的順序庸毫!
labels_row = rep("", nrow(exp))
labels_row[c(5,8,16)]=c("gene_A","gene_B","gene_C")
pheatmap(exp, labels_row = labels_row)
-
fontsize_row = 12
,fontsize_col = 8
可修改行名與列名的大凶心狻; -
angle_col = 45
可修改列名的角度飒赃;但只能是修改行名利花,并且只能是270”, “0”, “45”, “90”, “315”中的一種
6、熱圖的分割
- (1)根據(jù)聚類樹载佳,按指定數(shù)目進(jìn)行分割
pheatmap(exp,
cutree_cols = 2,
cutree_rows = 4)
- (2)指定行/列數(shù)進(jìn)行分割炒事,前提是要取消聚類
pheatmap(exp, cluster_rows = FALSE,cluster_cols = FALSE,
gaps_row = c(10, 20),
gaps_col = 5)
7、轉(zhuǎn)為ggplot2對象
library(ggplot2)
library(ggplotify)
g = as.ggplot(pheatmap(exp))
g + ggtitle("This is a ggplot object")
8蔫慧、按行按列歸一化
- 默認(rèn)按照原始值可視化挠乳,可通過
scale =
參數(shù)設(shè)置按照行或者列進(jìn)行歸一化之后的結(jié)果繪圖
pheatmap(exp, scale = "row")
# scale_rows = function(x){
# m = apply(x, 1, mean, na.rm = T)
# s = apply(x, 1, sd, na.rm = T)
# return((x - m) / s)
# }