使用pheatmap包繪制熱圖

先新建個矩陣

rm(list = ls())
# Create test matrix
test = matrix(rnorm(200), 20, 10) #創(chuàng)建20列、10行的矩陣test
# 調(diào)整下數(shù)據(jù)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3 #1到10行 1 3 5 7 9列數(shù)值加一
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 = "") #改變行名
# Draw heatmaps 畫熱圖
library(pheatmap)
pheatmap(test) #默認(rèn)參數(shù)畫熱圖
image.png
pheatmap(test, kmeans_k = 2) #如果要在繪制熱圖之前聚合行,需要生成的kmeans集群的數(shù)量。如果是NA,則不聚合行。此處聚合兩行
image.png
pheatmap(test, kmeans_k = 4) #我們試一下聚合四行
image.png
pheatmap(test, scale = "row", clustering_distance_rows = "correlation") #scale 設(shè)置值應(yīng)該居中并按行或列方向縮放,或者不按行或列方向縮放。對應(yīng)的值為“row”,“列”和“沒有”。clustering_distance_rows 聚類行中使用的距離度量??赡艿闹凳荘earson相關(guān)的“correlation”,dist支持的所有距離,如“euclidean”等。如果該值不滿足上述條件,則假定提供了距離矩陣
image.png
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50)) #設(shè)置顏色 
image.png
pheatmap(test, cluster_row = FALSE) #布爾值決定行是應(yīng)該集群cluster還是hclust對象  默認(rèn)cluster
image.png
pheatmap(test, legend = FALSE) #不畫圖例
image.png
# Show text within cells
pheatmap(test, display_numbers = TRUE) #顯示數(shù)字
image.png
pheatmap(test, display_numbers = TRUE, number_format = "%.1e") #用指數(shù)計數(shù)法e表示
image.png
![image.png](https://upload-images.jianshu.io/upload_images/15510633-04ac0a7266884e0e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
image.png
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test))) #標(biāo)記值大于5的test值
image.png
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",
                                                                            "1e-4", "1e-3", "1e-2", "1e-1", "1")) #不顯示行cluster ;legend_breaks用于圖例的斷點(diǎn)向量
image.png
# Fix cell sizes and save to file with correct size
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap") #設(shè)置單元格寬度和高度 設(shè)置標(biāo)題
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf") #圖的基本字體大小為8 保存圖為pdf文件
圖1
# Generate annotations for rows and columns
annotation_col = data.frame(
  CellType = factor(rep(c("CT1", "CT2"), 5)),
  Time = 1:5
) #建立數(shù)據(jù)框 注意自動補(bǔ)齊
rownames(annotation_col) = paste("Test", 1:10, sep = "")
annotation_row = data.frame(
  GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
) #建立數(shù)據(jù)框
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
image.png

image.png
# Display row and color annotations
pheatmap(test, annotation_col = annotation_col) #指定熱圖上側(cè)顯示的注釋的數(shù)據(jù)幀。每一行定義特定行的特性。數(shù)據(jù)和注釋中的行使用相應(yīng)的列名稱進(jìn)行匹配。注意,配色方案考慮了變量是連續(xù)的還是離散的。
image.png
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE) #不顯示注釋圖例
image.png
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row) #同時顯示行、列注釋
image.png
# Change angle of text in the columns
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45") #列標(biāo)簽的角度,現(xiàn)在只能從幾個預(yù)定義選項(0、45、90、270和315)中選擇
pheatmap(test, annotation_col = annotation_col, angle_col = "0") #同上 橫行顯示列標(biāo)簽
image.png
# Specify colors
ann_colors = list(
  Time = c("white", "firebrick"),
  CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
  GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors, main = "Title") #改變列注釋顏色
image.png
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])
image.png
image.png
# Gaps in heatmaps
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14)) #第10 row和14 row之后分割 熱圖
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14),
         cutree_col = 2) #分割成兩列  cutree_row 亦相同
image.png

image.png
# Show custom strings as row/col names 顯示自定義字符串作為行/col名稱
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
               "", "", "Il10", "Il15", "Il1b")
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row) #見圖
image.png
# Specifying clustering from distance matrix 從距離矩陣指定聚類
drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
image.png
# Modify ordering of the clusters using clustering callback option 使用群集回調(diào)選項修改群集的順序
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)
image.png

復(fù)制粘貼好累....


參考來源:生信技能樹

友情鏈接:

課程分享
生信技能樹全球公益巡講
https://mp.weixin.qq.com/s/E9ykuIbc-2Ja9HOY0bn_6g
B站公益74小時生信工程師教學(xué)視頻合輯
https://mp.weixin.qq.com/s/IyFK7l_WBAiUgqQi8O7Hxw
招學(xué)徒:
https://mp.weixin.qq.com/s/KgbilzXnFjbKKunuw7NVfw

歡迎關(guān)注公眾號:青島生信菜鳥團(tuán)

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

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

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