## 1.设置当前工作目录
setwd("./pheatmap/")
## 2.导入R包
library(pheatmap)
## 3.增加参数,绘制不一样的热图
### 3.1 pheatmap函数的结构
str(pheatmap)
function (mat, color = colorRampPalette(rev(brewer.pal(n = 7,
name = "RdYlBu")))(100), kmeans_k = NA, breaks = NA,
border_color = "grey60", cellwidth = NA, cellheight = NA,
scale = "none", cluster_rows = TRUE, cluster_cols = TRUE,
clustering_distance_rows = "euclidean", clustering_distance_cols = "euclidean",
clustering_method = "complete", clustering_callback = identity2,
cutree_rows = NA, cutree_cols = NA, treeheight_row = ifelse((class(cluster_rows) ==
"hclust") || cluster_rows, 50, 0), treeheight_col = ifelse((class(cluster_cols) ==
"hclust") || cluster_cols, 50, 0), legend = TRUE,
legend_breaks = NA, legend_labels = NA, annotation_row = NA,
annotation_col = NA, annotation = NA, annotation_colors = NA,
annotation_legend = TRUE, annotation_names_row = TRUE,
annotation_names_col = TRUE, drop_levels = TRUE, show_rownames = T,
show_colnames = T, main = NA, fontsize = 10, fontsize_row = fontsize,
fontsize_col = fontsize, angle_col = c("270", "0",
"45", "90", "315"), display_numbers = F, number_format = "%.2f",
number_color = "grey30", fontsize_number = 0.8 * fontsize,
gaps_row = NULL, gaps_col = NULL, labels_row = NULL,
labels_col = NULL, filename = NA, width = NA, height = NA,
silent = FALSE, na_col = "#DDDDDD", ...)
#@ 可以说虽然pheatmap包只有一个函数,但是其参数却很多。
### 3.2 构建数据矩阵
# Create test matrix
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("Sample", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
test # 查看test所有行/列内容
# Sample1 Sample2 Sample3 Sample4 Sample5 Sample6 Sample7 Sample8 Sample9 Sample10
# Gene1 3.7957568965 1.8233967 4.90560319 0.4067799 1.744039844 -0.02343351 3.48676035 -1.2406482 4.5505502 1.76937939
# Gene2 3.6552355276 -1.1993517 2.84112928 -0.3302270 2.491619748 -1.38009883 3.39422605 1.5526861 2.6797210 0.34295123
# Gene3 3.0609460815 0.3939751 4.90184120 -1.2154164 2.785525236 2.10026720 2.14067630 0.8166145 1.7517652 0.01996091
# Gene4 1.8684783946 -0.5386937 4.04685597 0.5901531 5.557510665 -0.19432126 3.31743363 -2.5144840 5.2018033 -0.04044510
# Gene5 2.0102636582 -0.9290359 2.40038679 -0.7775586 4.544655968 -1.19577000 3.84144210 -0.8475016 4.0338375 -0.54380074
# Gene6 3.0913495948 1.1772101 2.55722993 -0.6539475 2.549411967 -2.17690077 4.31288259 1.9227596 2.9604031 -0.03828152
# Gene7 3.8568183324 -0.4995627 3.57153508 0.5986590 3.122804423 1.37560199 2.43122407 -0.7429620 2.0837279 0.32902492
# Gene8 2.4509152396 -0.9611449 3.71700755 2.5553465 5.737982513 0.95604710 4.76721774 -1.2094103 2.2500003 0.35622822
# Gene9 1.6912999340 -0.2795518 4.00216213 -0.7162299 3.454144641 1.52470003 1.69047393 -1.5661280 4.0190400 0.63367912
# Gene10 0.6252480813 0.7438685 2.79662365 0.1703116 4.027609805 1.48381433 1.33705747 0.7747472 1.6031794 0.84923383
# Gene11 -0.4415825011 1.6080083 -0.19851846 2.7917330 -0.272474105 -0.10548920 -1.16025639 4.8305640 1.5770095 2.37149165
# Gene12 -0.6486606194 2.3700840 -0.77680084 1.4526694 -0.690304011 2.72308752 0.56949307 2.0399642 -0.3389819 1.40462561
# Gene13 0.0003821621 2.7113877 -0.39020907 2.8151354 -0.143181040 2.95203997 -0.85860110 2.4843515 0.9566911 2.39021211
# Gene14 -0.8011450013 4.0355510 -0.13017966 1.9029852 0.545175405 2.56665354 0.64394177 0.7000872 1.2690745 0.81502417
# Gene15 -0.5260242769 5.1100108 0.64137183 5.6425943 0.795887942 6.16224530 -0.40601303 5.8162889 0.1890412 6.15425580
# Gene16 1.1517317679 4.8100645 -0.33636956 5.2866118 0.265741749 5.11349791 -0.08855405 4.9829329 -0.8593587 7.52634773
# Gene17 -0.3555386628 6.8908341 -0.08050773 5.6959011 0.294486045 5.95486324 0.03772128 7.3472300 -1.2174207 5.96422351
# Gene18 0.5116797595 7.1544451 -0.18289447 5.2302485 -0.332464869 6.63425428 -0.54265890 5.0684743 0.7049639 7.32387247
# Gene19 1.7883182624 7.0628887 -0.43384416 4.0698986 0.003087234 5.41186588 -0.79840591 6.1149826 1.4828378 6.18828734
# Gene20 -0.5365331816 6.5070019 -1.49916738 6.3740146 0.775502704 6.29618602 -0.83917708 4.4833950 1.5096281 7.02968546
### 3.3 绘制热图
# Draw heatmaps
pheatmap(test) # 默认参数绘制热图
pheatmap(test, kmeans_k = 2) # 对基因进行kmeans = 2聚类
pheatmap(test, kmeans_k = 3) # kmeans = 3聚类
pheatmap(test, scale = "row", clustering_distance_rows = "correlation") # scale = "row",按行进行缩放,还可以按列column缩放;clustering_distance_rows = "correlation",对行进行Pearson相关性距离进行聚类,也可以按照euclidean进行聚类
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50)) # 修改默认颜色,重新绘制热图
## 4.结束
sessionInfo()
# R version 3.6.2 (2019-12-12)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 18363)
#
# Matrix products: default
#
# locale:
# [1] LC_COLLATE=Chinese (Simplified)_China.936
# [2] LC_CTYPE=Chinese (Simplified)_China.936
# [3] LC_MONETARY=Chinese (Simplified)_China.936
# [4] LC_NUMERIC=C
# [5] LC_TIME=Chinese (Simplified)_China.936
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] pheatmap_1.0.12
#
# loaded via a namespace (and not attached):
# [1] colorspace_1.4-1 scales_1.1.0 compiler_3.6.2 R6_2.4.1
# [5] tools_3.6.2 gtable_0.3.0 RColorBrewer_1.1-2 grid_3.6.2
# [9] lifecycle_0.1.0 packrat_0.5.0 munsell_0.5.0 rlang_0.4.4