使用ComplexHeatmap包繪制熱圖

加載所需R包

library(ComplexHeatmap)
require(circlize)
# 設置工作路徑
setwd("/Users/Davey/Desktop/")
# 清除當前環(huán)境中的變量
rm(list=ls())

構(gòu)建測試數(shù)據(jù)集

mat = matrix(rnorm(80, 2), 8, 10)
mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
rownames(mat) = letters[1:12]
colnames(mat) = LETTERS[1:10]
head(mat)
##           A        B         C          D         E         F        G
## a 2.8012969 2.448959 1.9428114  1.5604724 1.9177726 0.2158233 3.317781
## b 0.9418123 2.209306 2.7862192  1.4398838 3.6213657 4.2668243 2.577691
## c 1.0953206 2.341528 1.7046930  2.2206123 0.5952640 1.8767222 2.788628
## d 3.3537403 2.854183 1.2874279 -0.8889426 0.3848691 1.9383945 1.960331
## e 2.6451243 1.665598 2.1922587  1.7396285 2.9845466 2.4252669 4.630288
## f 1.3739374 2.331968 0.8226535  2.6943106 2.3148574 0.8938749 2.646258
##           H         I         J
## a 0.2706951 1.5700012 1.8710984
## b 1.7695180 2.6903054 1.3462785
## c 1.9808171 0.8263468 1.7852170
## d 1.6676294 1.7265879 1.4762889
## e 0.6262087 2.4486098 0.7309366
## f 1.3369390 0.2074835 1.5872041

使用Heatmap函數(shù)繪制熱圖

Heatmap(mat) #默認對行和列都進行聚類
image.png
# col參數(shù)自定義顏色劳坑,colorRamp2函數(shù)來自于circlize包
Heatmap(mat, col = colorRamp2(c(-5, 0, 5), c("green", "white", "red")))
image.png
# name參數(shù)設定圖例標題
Heatmap(mat, name = "test")
image.png
# heatmap_legend_param參數(shù)設定圖例的格式(標題湖饱,位置肴敛,方向传惠,高度等)
Heatmap(mat, heatmap_legend_param = list(
  title= "legend", title_position = "topcenter", 
  legend_height=unit(8,"cm"), legend_direction="vertical"))
image.png
# row_title和column_title參數(shù)設定行和列的標題
Heatmap(mat, row_title = "blablabla", column_title = "blablabla")
image.png
# column_title_side參數(shù)設定列標題放置的位置,column_title_rot參數(shù)設定列標題文本旋轉(zhuǎn)的角度
Heatmap(mat, column_title = "blablabla", column_title_side = "bottom", column_title_rot = 90)
image.png
# column_title_gp參數(shù)設定列標題文本的格式(字體,大小,顏色等)
Heatmap(mat, column_title = "blablabla", column_title_gp = gpar(fontsize = 20, fontface = "bold", col="red"))
image.png
# cluster_rows和cluster_columns參數(shù)設定行或列是否聚類
Heatmap(mat, cluster_rows = FALSE, cluster_columns = FALSE)
image.png
# clustering_distance_rows參數(shù)設定行聚類的距離方法,默認為"euclidean"
Heatmap(mat, clustering_distance_rows = "pearson")
image.png
Heatmap(mat, clustering_distance_rows = function(x) dist(x))
image.png
Heatmap(mat, clustering_distance_rows = function(x, y) 1 - cor(x, y))
image.png
# clustering_method_rows參數(shù)設定行聚類的方法,默認為"complete"
Heatmap(mat, clustering_method_rows = "single")
image.png
# row_dend_side參數(shù)設定行聚類樹放置的位置
Heatmap(mat, row_dend_side = "right")
image.png
# row_dend_width參數(shù)設定行聚類樹的寬度
Heatmap(mat, row_dend_width = unit(2, "cm"))
image.png
# row_names_side和column_names_side參數(shù)設置行名和列名存放的位置
Heatmap(mat, row_names_side = "left", row_dend_side = "right", 
        column_names_side = "top", column_dend_side = "bottom")
image.png
# show_row_names參數(shù)設定是否顯示行名,show_row_dend參設設定是否顯示行聚類樹
Heatmap(mat, show_row_names = FALSE, show_row_dend = FALSE)
image.png
# row_names_gp參數(shù)設定行名文本的格式
Heatmap(mat, row_names_gp = gpar(fontsize = 20, fontface="italic", col="red"))
image.png
# km參數(shù)設定對行進行kmeans聚類分組的類數(shù)
Heatmap(mat, km = 4, row_title_gp = gpar(col=rainbow(4)), row_names_gp = gpar(col=rainbow(4), fontsize=20))
image.png

使用HeatmapAnnotation函數(shù)構(gòu)建注釋對象

annotation = data.frame(value = rnorm(10))
annotation = HeatmapAnnotation(df = annotation)
# top_annotation參數(shù)在頂部添加注釋信息
Heatmap(mat, top_annotation = annotation)
image.png
annotation = data.frame(value = rnorm(10))
value = 1:10
ha = HeatmapAnnotation(df = annotation, points = anno_points(value), 
                       annotation_height = c(1, 2))
# top_annotation_height參數(shù)設定頂部注釋信息展示的高度
Heatmap(mat, top_annotation = ha, top_annotation_height = unit(2, "cm"), 
        bottom_annotation = ha)
image.png

使用add_heatmap函數(shù)組合多個熱圖或注釋信息

annotation1 = HeatmapAnnotation(df = data.frame(type = c(rep("A", 6), rep("B", 6))))
ht1 = Heatmap(mat, name = "test1", top_annotation = annotation1)

annotation2 = HeatmapAnnotation(df = data.frame(type1 = rep(c("A", "B"), 6), 
                                               type2 = rep(c("C", "D"), each = 6)))
ht2 = Heatmap(mat, name = "test2", bottom_annotation = annotation2)
add_heatmap(ht1, ht2)
image.png
# 添加point注釋信息
ha = HeatmapAnnotation(points = anno_points(1:12, which = "row",gp= gpar(col=rainbow(12))), which = "row")
add_heatmap(ht1, ha)
image.png
# 添加barplot注釋信息
ha = HeatmapAnnotation(barplot = anno_barplot(1:12, which = "row", bar_width=0.4, gp= gpar(fill="red")), which = "row")
add_heatmap(ht1, ha)
image.png
# 添加boxplot注釋信息
ha = HeatmapAnnotation(boxplot = anno_boxplot(matrix(rnorm(60), nrow=12), which = "row", border = F, gp= gpar(fill="blue")), which = "row")
add_heatmap(ht2, ha)
image.png
# 添加histogram注釋信息
ha = HeatmapAnnotation(histogram = anno_histogram(matrix(rnorm(48), nrow=12), which = "row", gp= gpar(fill="red")), which = "row")
add_heatmap(ht2, ha)
image.png
# 添加density注釋信息
ha = HeatmapAnnotation(density = anno_density(matrix(rnorm(48), nrow=12), which = "row", type="heatmap"), which = "row")
add_heatmap(ht2, ha)
image.png

row_order和column_order函數(shù)獲得熱圖聚類后行和列對應的順序

row_order(ht1) #得到一個列表
## [[1]]
##  [1]  3  6  8  5  1  4  2  7 11 12 10  9
column_order(ht1) #得到一個向量
##  [1]  4  8 10  3  5  6  7  9  1  2
mat[row_order(ht1)[[1]],]
##            A          B          C          D          E          F
## c  1.0953206  2.3415277  1.7046930  2.2206123  0.5952640  1.8767222
## f  1.3739374  2.3319679  0.8226535  2.6943106  2.3148574  0.8938749
## h  1.9212146  2.0554681  2.2984488  2.1626922  0.7940837  1.3331693
## e  2.6451243  1.6655977  2.1922587  1.7396285  2.9845466  2.4252669
## a  2.8012969  2.4489591  1.9428114  1.5604724  1.9177726  0.2158233
## d  3.3537403  2.8541834  1.2874279 -0.8889426  0.3848691  1.9383945
## b  0.9418123  2.2093057  2.7862192  1.4398838  3.6213657  4.2668243
## g  3.2939952  1.6930804  3.6404261  1.0191843  2.6318222  3.8651897
## k -2.6225943 -1.5660520 -0.2162833 -1.5654904 -2.6135985 -3.3150684
## l -0.8009785 -0.2694706 -3.1642354 -2.1123275 -1.5482925 -1.3466843
## j -1.1355446 -2.4070908 -4.2075492 -2.3396374 -4.3409680 -2.9145455
## i -2.8700744 -2.9040941 -4.1061353 -2.6059058 -3.6201093 -2.5930123
##           G          H          I           J
## c  2.788628  1.9808171  0.8263468  1.78521698
## f  2.646258  1.3369390  0.2074835  1.58720410
## h  1.075480  1.7551531  1.0493876  0.04447524
## e  4.630288  0.6262087  2.4486098  0.73093658
## a  3.317781  0.2706951  1.5700012  1.87109836
## d  1.960331  1.6676294  1.7265879  1.47628891
## b  2.577691  1.7695180  2.6903054  1.34627848
## g  3.161174  1.7733364  0.5466857  1.22953346
## k -2.512614 -3.3644992 -2.1460842 -1.25187852
## l -0.344318 -2.3971692 -0.8973583 -1.29018334
## j -1.245340 -1.8870998 -0.5708714 -2.97128660
## i -1.561022 -2.1474388 -3.2070408 -1.63078877
mat[,column_order(ht1)]
##            D          H           J          C          E          F
## a  1.5604724  0.2706951  1.87109836  1.9428114  1.9177726  0.2158233
## b  1.4398838  1.7695180  1.34627848  2.7862192  3.6213657  4.2668243
## c  2.2206123  1.9808171  1.78521698  1.7046930  0.5952640  1.8767222
## d -0.8889426  1.6676294  1.47628891  1.2874279  0.3848691  1.9383945
## e  1.7396285  0.6262087  0.73093658  2.1922587  2.9845466  2.4252669
## f  2.6943106  1.3369390  1.58720410  0.8226535  2.3148574  0.8938749
## g  1.0191843  1.7733364  1.22953346  3.6404261  2.6318222  3.8651897
## h  2.1626922  1.7551531  0.04447524  2.2984488  0.7940837  1.3331693
## i -2.6059058 -2.1474388 -1.63078877 -4.1061353 -3.6201093 -2.5930123
## j -2.3396374 -1.8870998 -2.97128660 -4.2075492 -4.3409680 -2.9145455
## k -1.5654904 -3.3644992 -1.25187852 -0.2162833 -2.6135985 -3.3150684
## l -2.1123275 -2.3971692 -1.29018334 -3.1642354 -1.5482925 -1.3466843
##           G          I          A          B
## a  3.317781  1.5700012  2.8012969  2.4489591
## b  2.577691  2.6903054  0.9418123  2.2093057
## c  2.788628  0.8263468  1.0953206  2.3415277
## d  1.960331  1.7265879  3.3537403  2.8541834
## e  4.630288  2.4486098  2.6451243  1.6655977
## f  2.646258  0.2074835  1.3739374  2.3319679
## g  3.161174  0.5466857  3.2939952  1.6930804
## h  1.075480  1.0493876  1.9212146  2.0554681
## i -1.561022 -3.2070408 -2.8700744 -2.9040941
## j -1.245340 -0.5708714 -1.1355446 -2.4070908
## k -2.512614 -2.1460842 -2.6225943 -1.5660520
## l -0.344318 -0.8973583 -0.8009785 -0.2694706
# 得到熱圖聚類后順序的數(shù)據(jù)
mat1 = mat[row_order(ht1)[[1]],column_order(ht1)]
write.table(mat1,file="reorder.txt",quote = FALSE,sep='\t')  #輸出結(jié)果缠俺,按照熱圖中的順序

使用densityHeatmap函數(shù)繪制密度熱圖

# 構(gòu)建測試數(shù)據(jù)集
matrix = matrix(rnorm(100), 10); colnames(matrix) = letters[1:10]
# 默認不對列進行聚類拧廊,不顯示聚類樹
densityHeatmap(matrix)
image.png
# anno參數(shù)添加注釋信息
densityHeatmap(matrix, anno = rep(c("A", "B"), each = 5), cluster_columns = T, show_column_dend = T)
image.png
# col參數(shù)自定義顏色
densityHeatmap(matrix, col = c("blue", "white", "red"), anno = rep(c("A", "B"), each = 5))
image.png
# 構(gòu)建注釋對象
ha = HeatmapAnnotation(points = anno_points(runif(10), gp=gpar(col=rainbow(10))))
densityHeatmap(matrix, anno = ha)
image.png
# 構(gòu)建一個list
lt = list(rnorm(10), runif(10), rnorm(10))
densityHeatmap(lt)
image.png
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] grid      stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] circlize_0.4.4        ComplexHeatmap_1.18.1
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.18        digest_0.6.16       rprojroot_1.3-2    
##  [4] backports_1.1.2     magrittr_1.5        evaluate_0.11      
##  [7] stringi_1.2.4       GlobalOptions_0.1.0 GetoptLong_0.1.7   
## [10] rmarkdown_1.10      RColorBrewer_1.1-2  rjson_0.2.20       
## [13] tools_3.5.1         stringr_1.3.1       yaml_2.2.0         
## [16] compiler_3.5.1      colorspace_1.3-2    shape_1.4.4        
## [19] htmltools_0.3.6     knitr_1.20
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市晋修,隨后出現(xiàn)的幾起案子吧碾,更是在濱河造成了極大的恐慌,老刑警劉巖墓卦,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件倦春,死亡現(xiàn)場離奇詭異,居然都是意外死亡落剪,警方通過查閱死者的電腦和手機睁本,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來忠怖,“玉大人呢堰,你說我怎么就攤上這事》财” “怎么了枉疼?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長鞋拟。 經(jīng)常有香客問我骂维,道長,這世上最難降的妖魔是什么贺纲? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任航闺,我火速辦了婚禮,結(jié)果婚禮上猴誊,老公的妹妹穿的比我還像新娘潦刃。我一直安慰自己,他們只是感情好懈叹,可當我...
    茶點故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布乖杠。 她就那樣靜靜地躺著,像睡著了一般项阴。 火紅的嫁衣襯著肌膚如雪滑黔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天环揽,我揣著相機與錄音,去河邊找鬼庵佣。 笑死歉胶,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的巴粪。 我是一名探鬼主播通今,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼粥谬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了辫塌?” 一聲冷哼從身側(cè)響起漏策,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎臼氨,沒想到半個月后掺喻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡储矩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年感耙,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片持隧。...
    茶點故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡即硼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出屡拨,到底是詐尸還是另有隱情只酥,我是刑警寧澤,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布呀狼,位于F島的核電站层皱,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏赠潦。R本人自食惡果不足惜叫胖,卻給世界環(huán)境...
    茶點故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望她奥。 院中可真熱鬧瓮增,春花似錦、人聲如沸哩俭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽凡资。三九已至砸捏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間隙赁,已是汗流浹背垦藏。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留伞访,地道東北人掂骏。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像厚掷,于是被迫代替她去往敵國和親弟灼。 傳聞我的和親對象是個殘疾皇子级解,可洞房花燭夜當晚...
    茶點故事閱讀 45,107評論 2 356

推薦閱讀更多精彩內(nèi)容