加載所需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