pheatmap

一、 創(chuàng)建一個(gè)隨機(jī)矩陣10x20

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 = "")

二、作圖
1.基本作圖

pheatmap(test)                       #基本圖
pheatmap(test, kmeans_k = 2)    #將行聚為2類
pheatmap(test, scale = "row", clustering_distance_rows = "correlation") #標(biāo)準(zhǔn)化
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(10))
 #設(shè)置顏色,后面括號里的數(shù)字表示梯度
pheatmap(test, cluster_row = FALSE)     #是否顯示行的聚類
pheatmap(test, legend = FALSE)          #是否顯示圖例

scale是指對數(shù)值進(jìn)行均一化處理,在基因表達(dá)量的數(shù)據(jù)中,有些基因表達(dá)量極低,有些基因表達(dá)量極高,因此把每個(gè)基因在不同處理和重復(fù)中的數(shù)據(jù)轉(zhuǎn)換為平均值為0,方差為1的數(shù)據(jù),可以看出每個(gè)基因在某個(gè)處理和重復(fù)中表達(dá)量是高還是低,一般選擇做row均一化。
2.顯示色塊的數(shù)值、文本

pheatmap(test, display_numbers = TRUE)                      #基本用法
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")
# "%.1e"用科學(xué)計(jì)數(shù)法顯示保留1位小數(shù);"%.3f"用小數(shù)顯示保留3位小數(shù)
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test))) 
#以*賦值矩陣> 5的色塊
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, 
legend_labels = c("0", "1e-4", "1e-3", "1e-2", "1e-1", "1"))
#legend_breaks設(shè)置圖例的顯示范圍,默認(rèn)間隔為1;legend_labels重寫刻度的標(biāo)簽, 需與legend_breaks同時(shí)使用。

3.調(diào)整色塊或文本大小

pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap",
 fontsize = 8, filename = "test.pdf")
#參數(shù)分別表示:色塊的寬度、色塊的高度、標(biāo)題、行列名
#及圖例字體的大小、保存為當(dāng)前工作目錄下的圖片的文件名

4.行列注釋
首先創(chuàng)建annotation_col 與annotation_row

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 = "")

顯示行、列注釋信息

pheatmap(test)
pheatmap(test, annotation_col = annotation_col)
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)
pheatmap(test, annotation_col = annotation_col, annotation_row =
annotation_row)

5.改變列名文本角度

pheatmap(test, annotation_col = annotation_col, 
annotation_row = annotation_row, angle_col = "45")
pheatmap(test, annotation_col = annotation_col, angle_col = "0")

6.自定義注釋色塊的顏色

ann_colors = list(
  Time = c("white", "firebrick"),
  CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
  GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)     #注意ann_colors是列表
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors, main = "Title")
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,  annotation_colors = ann_colors)
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2]) 

7.切分熱圖

pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), 
         cutree_col = 2)      
#gaps_row有效的前提是cluster_rows = F;cutree_col有效的前提是cluster_cols = T

8.自定義顯示哪些行列的名字

labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
               "", "", "Il10", "Il15", "Il1b")
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)

9.用距離矩陣的方法來聚類

drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
callback = function(hc, mat){
  sv = svd(t(mat))$v[,1]
  dend = reorder(as.dendrogram(hc), wts = sv)
  as.hclust(dend)
}
pheatmap(test, clustering_callback = callback)
  1. 取消邊框或更改其顏色
pheatmap(test, border_color = NA)
pheatmap(test, border_color = 'red')

三、參數(shù)調(diào)整:
1. 顏色參數(shù):
默認(rèn)值為colorRampPalette(rev(brewer.pal(n = 7, name ="RdYlBu")))(100),RdYlBu也就是Rd紅色,Yi黃色,Bu藍(lán)色的過度,則主調(diào)色為紅黃藍(lán)。
顏色大全網(wǎng)址:https://www.color-hex.com/color-names.html
2 數(shù)據(jù)變換參數(shù):
scale,是指對數(shù)值進(jìn)行均一化處理,在基因表達(dá)量的數(shù)據(jù)中,有些基因表達(dá)量極低,有些基因表達(dá)量極高,因此把每個(gè)基因在不同處理和重復(fù)中的數(shù)據(jù)轉(zhuǎn)換為平均值為0,方差為1的數(shù)據(jù),可以看出每個(gè)基因在某個(gè)處理和重復(fù)中表達(dá)量是高還是低,一般選擇做row均一化。
clustering_method,表示聚類方法,值可以是hclust的任何一種,如"ward.D","single", "complete", "average", "mcquitty", "median", "centroid", "ward.D2"。
cluster_rows,表示行是否聚類,值可以是FALSE或TRUE
clustering_distance_rows,行距離度量的方法,如歐氏距離
cutree_rows,行聚類數(shù)
treeheight_row,行聚類樹的高度,默認(rèn)為50
gaps_row,對行進(jìn)行分割,就不應(yīng)對相應(yīng)的行進(jìn)行聚類
cluster_cols,表示列是否聚類,值可以是FALSE或TRUE
clustering_distance_cols,列距離度量的方法
cutree_cols,列聚類數(shù)
treeheight_col,列聚類樹的高度,默認(rèn)為50
gaps_col,對列進(jìn)行分割,就不應(yīng)對相應(yīng)的列進(jìn)行聚類
3.色度條--就是熱圖右上角那個(gè)小小的長方條
legend,邏輯值,是否顯示色度條,默認(rèn)為T
legend_breaks,顯示多少個(gè)顏色數(shù)值段
legend_labels,對色度條上對應(yīng)位置的字符進(jìn)行修改
4.注釋條
annotation = NA
annotation_colors,對標(biāo)簽的顏色進(jìn)行修改
annotation_legend,是否顯示標(biāo)簽注釋條
annotation_row,數(shù)據(jù)框格式,用來定義熱圖所在行的注釋條
annotation_names_row,邏輯值,是否顯示行標(biāo)簽名稱
annotation_col,數(shù)據(jù)框格式,用來定義熱圖所在列的注釋條
annotation_names_col,邏輯值,是否顯示列標(biāo)簽名稱
5.其他修改參數(shù)
main,設(shè)置圖的標(biāo)題
fontsize,是設(shè)置所有除主圖以外的標(biāo)簽的大小
number_color,字體的顏色
show_rownames,是否顯示行名
fontsize_row,行名的字體大小
labels_row,X軸坐標(biāo)名設(shè)置
show_colnames,是否顯示列名
fontsize_col,列名的字體大小
labels_col,y軸坐標(biāo)名設(shè)置
6.小格子參數(shù)設(shè)置--熱圖是由一個(gè)個(gè)的小四方格子組成的,每一個(gè)小格子代表一個(gè)基因在一個(gè)樣本內(nèi)的表達(dá)情況
fontsize_number,小格子中數(shù)字大小
display_numbers,是否在小格子中顯示數(shù)字,邏輯值
number_format,小格子中數(shù)字顯示形式,但僅有在display_numbers=T時(shí)才能使用
na_col,設(shè)置小格子為缺失值時(shí)的顏色
cellwidth,表示每個(gè)小格子的寬度
cellheight, 表示每個(gè)小格子的高度
7.輸出文件參數(shù)設(shè)置,一般可以直接將畫好的熱圖以png格式或者pdf格式進(jìn)行寫出
filename,輸出圖畫的文件名
width,輸出圖畫的寬度
height,輸出圖畫的高度

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容