加載所需R包
library(pheatmap)
設(shè)置工作路徑
setwd("/Users/Davey/Desktop/VennDiagram/")
# 清除當(dāng)前環(huán)境中的變量
rm(list=ls())
構(gòu)建測(cè)試數(shù)據(jù)集
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
# 默認(rèn)繪圖
pheatmap(test)
# scale = "row"參數(shù)對(duì)行進(jìn)行歸一化
pheatmap(test, scale = "row")
# clustering_method參數(shù)設(shè)定不同聚類(lèi)方法戚揭,默認(rèn)為"complete",可以設(shè)定為'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'
pheatmap(test,scale = "row", clustering_method = "average")
# clustering_distance_rows = "correlation"參數(shù)設(shè)定行聚類(lèi)距離方法為Pearson corralation,默認(rèn)為歐氏距離"euclidean"
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
# color參數(shù)自定義顏色
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
# cluster_row = FALSE參數(shù)設(shè)定不對(duì)行進(jìn)行聚類(lèi)
pheatmap(test, cluster_row = FALSE)
# legend_breaks參數(shù)設(shè)定圖例顯示范圍,legend_labels參數(shù)添加圖例標(biāo)簽
pheatmap(test, legend_breaks = c(1:5), legend_labels = c("1.0","2.0","3.0","4.0","5.0"))
# legend = FALSE參數(shù)去掉圖例
pheatmap(test, legend = FALSE)
# border_color參數(shù)設(shè)定每個(gè)熱圖格子的邊框色
pheatmap(test, border_color = "red")
# border=FALSE參數(shù)去掉邊框線
pheatmap(test, border=FALSE)
# show_rownames和show_colnames參數(shù)設(shè)定是否顯示行名和列名
pheatmap(test,show_rownames=F,show_colnames=F)
# treeheight_row和treeheight_col參數(shù)設(shè)定行和列聚類(lèi)樹(shù)的高度胚泌,默認(rèn)為50
pheatmap(test, treeheight_row = 30, treeheight_col = 50)
# display_numbers = TRUE參數(shù)設(shè)定在每個(gè)熱圖格子中顯示相應(yīng)的數(shù)值与殃,number_color參數(shù)設(shè)置數(shù)值字體的顏色
pheatmap(test, display_numbers = TRUE,number_color = "blue")
# number_format = "%.1e"參數(shù)設(shè)定數(shù)值的顯示格式
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")
# 自定義數(shù)值的顯示方式
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
# cellwidth和cellheight參數(shù)設(shè)定每個(gè)熱圖格子的寬度和高度,main參數(shù)添加主標(biāo)題
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
# 構(gòu)建列注釋信息
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
# 構(gòu)建行注釋信息
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參數(shù)添加列注釋信息
pheatmap(test, annotation_col = annotation_col)
# annotation_legend = FALSE參數(shù)去掉注釋圖例
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)
# annotation_col和annotation_row參數(shù)同時(shí)添加行和列的注釋信息
pheatmap(test, annotation_row = annotation_row, annotation_col = annotation_col)
# 自定注釋信息的顏色列表
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設(shè)定注釋信息的顏色
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])
# gaps_row = c(10, 14)參數(shù)在第10和14行處添加gap, 要求對(duì)行不進(jìn)行聚類(lèi)
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))
# cutree_col = 2參數(shù)將列按聚類(lèi)樹(shù)的結(jié)果分成兩部分, 要求對(duì)列進(jìn)行聚類(lèi)
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14),
cutree_col = 2)
# 對(duì)行和列都不聚類(lèi),自定義劃分行和列的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))
# 自定義行的標(biāo)簽名
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "Il10", "Il15", "Il1b")
# labels_row參數(shù)添加行標(biāo)簽
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
# 自定義聚類(lèi)的距離方法
drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
# clustering_distance_rows和clustering_distance_cols參數(shù)設(shè)定行和列的聚類(lèi)距離方法
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
# fontsize參數(shù)設(shè)定標(biāo)簽字體大小森篷,filename參數(shù)設(shè)定圖片保存名稱
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")
將熱圖結(jié)果按聚類(lèi)后的順序輸出
aa=pheatmap(test,scale="row") #熱圖薛耻,歸一化营罢,并聚類(lèi)
# 簡(jiǎn)要查看熱圖對(duì)象的信息
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]) # 按照熱圖的順序,重新排原始數(shù)據(jù)
datat = data.frame(rownames(datat),datat,check.names =F) # 將行名加到表格數(shù)據(jù)中
colnames(datat)[1] = "geneid"
write.table(datat,file="reorder.txt",row.names=FALSE,quote = FALSE,sep='\t') #輸出結(jié)果饼齿,按照熱圖中的順序
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