使用pheatmap包繪制熱圖

加載所需R包

library(pheatmap)

設置工作路徑

setwd("/Users/Davey/Desktop/VennDiagram/")
# 清除當前環境中的變量
rm(list=ls())

構建測試數據集

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 = "")
head(test)
##          Test1      Test2    Test3      Test4    Test5       Test6
## Gene1 4.064973  0.7535271 3.024070 -2.1294440 4.407945 -0.35677097
## Gene2 2.360043  1.6974946 3.273425 -2.3341406 3.839523  0.16982944
## Gene3 3.253465 -0.9011582 1.716257 -0.2294471 4.636610 -0.24520382
## Gene4 4.070226 -0.6191941 3.734437  1.9348314 4.426825 -0.17730957
## Gene5 3.821414  0.5584876 1.871479 -0.2784607 2.633761  0.01332901
## Gene6 3.012469  0.1738285 3.652423 -2.0083435 4.124951 -0.67899611
##          Test7      Test8    Test9        Test10
## Gene1 3.602764  1.2903843 2.044119  1.826159e+00
## Gene2 3.083160  0.2642755 2.855381  1.988289e-01
## Gene3 3.417809 -0.1362079 3.858884 -8.390304e-01
## Gene4 2.911934  0.4299550 4.128398 -3.011521e+00
## Gene5 2.651758 -1.6884728 3.001079  1.861780e+00
## Gene6 1.934270  0.5811059 2.297763  6.878644e-05
# 默認繪圖
pheatmap(test)
image.png
# scale = "row"參數對行進行歸一化
pheatmap(test, scale = "row")
image.png
# clustering_method參數設定不同聚類方法,默認為"complete",可以設定為'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'
pheatmap(test,scale = "row", clustering_method = "average")
image.png
# clustering_distance_rows = "correlation"參數設定行聚類距離方法為Pearson corralation,默認為歐氏距離"euclidean"
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
image.png
# color參數自定義顏色
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
image.png
# cluster_row = FALSE參數設定不對行進行聚類
pheatmap(test, cluster_row = FALSE)
image.png
# legend_breaks參數設定圖例顯示范圍,legend_labels參數添加圖例標簽
pheatmap(test, legend_breaks = c(1:5), legend_labels = c("1.0","2.0","3.0","4.0","5.0"))
image.png
# legend = FALSE參數去掉圖例
pheatmap(test, legend = FALSE)
image.png
# border_color參數設定每個熱圖格子的邊框色
pheatmap(test, border_color = "red")
image.png
# border=FALSE參數去掉邊框線
pheatmap(test, border=FALSE)
image.png
# show_rownames和show_colnames參數設定是否顯示行名和列名
pheatmap(test,show_rownames=F,show_colnames=F)
image.png
# treeheight_row和treeheight_col參數設定行和列聚類樹的高度,默認為50
pheatmap(test, treeheight_row = 30, treeheight_col = 50)
image.png
# display_numbers = TRUE參數設定在每個熱圖格子中顯示相應的數值,number_color參數設置數值字體的顏色
pheatmap(test, display_numbers = TRUE,number_color = "blue")
image.png
# number_format = "%.1e"參數設定數值的顯示格式
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")
image.png
# 自定義數值的顯示方式
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
image.png
# cellwidth和cellheight參數設定每個熱圖格子的寬度和高度,main參數添加主標題
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
image.png
# 構建列注釋信息
annotation_col = data.frame(
  CellType = factor(rep(c("CT1", "CT2"), 5)), 
  Time = 1:5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")
head(annotation_col)
##       CellType Time
## Test1      CT1    1
## Test2      CT2    2
## Test3      CT1    3
## Test4      CT2    4
## Test5      CT1    5
## Test6      CT2    1
# 構建行注釋信息
annotation_row = data.frame(
  GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
head(annotation_row)
##       GeneClass
## Gene1     Path1
## Gene2     Path1
## Gene3     Path1
## Gene4     Path1
## Gene5     Path1
## Gene6     Path1
# annotation_col參數添加列注釋信息
pheatmap(test, annotation_col = annotation_col)
image.png
# annotation_legend = FALSE參數去掉注釋圖例
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)
image.png
# annotation_col和annotation_row參數同時添加行和列的注釋信息
pheatmap(test, annotation_row = annotation_row, annotation_col = annotation_col)
image.png
# 自定注釋信息的顏色列表
ann_colors = list(
  Time = c("white", "firebrick"),
  CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
  GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)
head(ann_colors)
## $Time
## [1] "white"     "firebrick"
## 
## $CellType
##       CT1       CT2 
## "#1B9E77" "#D95F02" 
## 
## $GeneClass
##     Path1     Path2     Path3 
## "#7570B3" "#E7298A" "#66A61E"
# annotation_colors設定注釋信息的顏色
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)
image.png
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2]) 
image.png
# gaps_row = c(10, 14)參數在第10和14行處添加gap, 要求對行不進行聚類
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))
image.png
# cutree_col = 2參數將列按聚類樹的結果分成兩部分, 要求對列進行聚類
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14),
         cutree_col = 2)
image.png
# 對行和列都不聚類,自定義劃分行和列的gap
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, cluster_cols = FALSE, 
         gaps_row = c(6, 10, 14), gaps_col = c(2, 5, 8))
image.png
# 自定義行的標簽名
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
               "", "", "Il10", "Il15", "Il1b")
# labels_row參數添加行標簽
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
image.png
# 自定義聚類的距離方法
drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
# clustering_distance_rows和clustering_distance_cols參數設定行和列的聚類距離方法
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
image.png
# fontsize參數設定標簽字體大小,filename參數設定圖片保存名稱
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")

將熱圖結果按聚類后的順序輸出

aa=pheatmap(test,scale="row")  #熱圖,歸一化,并聚類
image.png
# 簡要查看熱圖對象的信息
summary(aa)
##          Length Class  Mode   
## tree_row 7      hclust list   
## tree_col 7      hclust list   
## kmeans   1      -none- logical
## gtable   6      gtable list
order_row = aa$tree_row$order  #記錄熱圖的行排序
order_col = aa$tree_col$order    #記錄熱圖的列排序
datat = data.frame(test[order_row,order_col])   # 按照熱圖的順序,重新排原始數據
datat = data.frame(rownames(datat),datat,check.names =F)  # 將行名加到表格數據中
colnames(datat)[1] = "geneid" 
write.table(datat,file="reorder.txt",row.names=FALSE,quote = FALSE,sep='\t')  #輸出結果,按照熱圖中的順序
sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: OS X El Capitan 10.11.3
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] pheatmap_1.0.10
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.18       digest_0.6.16      rprojroot_1.3-2   
##  [4] grid_3.5.1         gtable_0.2.0       backports_1.1.2   
##  [7] magrittr_1.5       scales_1.0.0       evaluate_0.11     
## [10] stringi_1.2.4      rmarkdown_1.10     RColorBrewer_1.1-2
## [13] tools_3.5.1        stringr_1.3.1      munsell_0.5.0     
## [16] yaml_2.2.0         compiler_3.5.1     colorspace_1.3-2  
## [19] htmltools_0.3.6    knitr_1.20
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,156評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,401評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,069評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,873評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,635評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,128評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,203評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,365評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,881評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,733評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,935評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,475評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,172評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,582評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,821評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,595評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,908評論 2 372

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,781評論 18 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,623評論 25 708
  • 本章主要講演示的邏輯,與前面幾章相反,這里開始引入圖文概念。 一些有啟發的觀點: 1.理想的文章,在30秒內讓讀者...
    簡安Tina閱讀 246評論 0 0
  • 1, OOP 指什么?有哪些特性 OOP是Object Oriented Programming 縮寫,面向對象編...
    DeeJay_Y閱讀 238評論 0 0
  • “哎哎哎,怎么還這么多大短褲小裙子的???是給我們的咩?? 你連個腰都沒有,你怎么穿這小裙子。你自己說。” 這是幫...
    柚子儲儲閱讀 601評論 4 10