R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(pheatmap)
?pheatmap
# Create test matrix
test = matrix(rnorm(200), 20, 10)
test
##             [,1]       [,2]          [,3]       [,4]        [,5]        [,6]
##  [1,]  1.7720179 -2.2312041  0.0008033192 -0.9366317  1.20319759  0.32057143
##  [2,]  0.9424521 -0.2754375  0.1137390976  1.2096740  0.41987282  0.64214789
##  [3,]  1.4973987 -1.1307243  0.2604465304 -0.4188725  0.45326260  1.36196244
##  [4,]  0.8501888 -0.8845835  0.8271287445  0.6842597 -0.08667241 -0.43180510
##  [5,]  0.0531542  1.1030540  0.6447211595 -0.7887825  1.29545924 -1.83661519
##  [6,] -1.4008452 -1.3790862 -0.0196172080 -0.9726631 -0.92771199  0.97529388
##  [7,]  1.6024032  0.3061658 -0.1803969387  0.2041019  0.59584985  1.00969506
##  [8,] -0.1760314  0.7661342  0.5896748525  0.1313049  0.22897116  1.84450921
##  [9,]  0.6573316 -0.5888971 -1.6111832291  1.5591689  2.62138415  0.67215805
## [10,] -0.9269316 -0.3591864 -0.5020076730  0.1459360 -0.25402137  0.31351954
## [11,] -0.6973361 -2.1741696 -0.3051690348  1.2746334  0.25285919  2.48917254
## [12,] -0.1099342  1.2963182  0.2678471531  1.3966800 -1.35867564 -0.11753505
## [13,]  0.9160942 -0.9285285 -0.0386133158  2.2530483  0.01620227  0.26088368
## [14,]  0.9591300  0.2151505  0.0324787284  0.4794928 -0.33793071 -0.21605758
## [15,]  0.1389228 -1.4250073 -0.8213879860  0.7838683 -0.27955335  0.73225139
## [16,] -1.1677052  1.2163652  0.5747413403  0.4404896  0.72881284  0.21330741
## [17,]  0.6545647  1.2233523 -1.2557241583  0.7058058 -0.18150514  0.02551105
## [18,]  0.4757077  2.8432732 -0.3472685253 -0.1146986 -0.76792964 -0.35725449
## [19,]  1.1643460 -0.7519785  0.5066130055  0.1553126 -0.16582043  1.48604519
## [20,]  0.3837661  0.3658713  0.8159478959 -0.7941376  0.49802058  0.05265993
##              [,7]        [,8]        [,9]       [,10]
##  [1,] -1.99620216 -0.58144691 -1.39449519  0.53160450
##  [2,]  1.55231261  0.07070202 -1.13067926  0.51877139
##  [3,]  0.78524308 -0.05228953  0.35576326  0.43212422
##  [4,]  1.74756261  0.19563622 -0.66988810  1.52996246
##  [5,]  1.22932663  0.72726560 -0.17160490 -1.44852258
##  [6,] -0.28077511 -1.34570388 -0.48839934 -0.29982641
##  [7,] -0.78594391  0.08970703  0.60995894  0.11806282
##  [8,]  0.25253274  0.68536158  1.49512323  0.44912358
##  [9,] -0.26111862 -0.64094770  0.78140770  1.08976444
## [10,] -0.13735012 -0.44924210 -0.66413751 -0.64357195
## [11,]  0.84289802 -0.12967526  0.74890295 -1.75850101
## [12,]  1.16077980 -0.24311960  0.18515907 -0.39796677
## [13,] -1.79345046  0.35133139  1.15967892 -0.02357812
## [14,]  0.08038960  0.22715220  1.53218708  0.40940664
## [15,]  1.13620192  0.68921973 -2.19263207  0.00650299
## [16,]  0.22028965  0.53116547  0.90517980  0.31454274
## [17,]  0.82787218 -0.21817403 -0.01185775 -0.52808237
## [18,] -0.83373060 -1.54174801 -0.04465615  1.81389836
## [19,] -0.09277748  0.19117808 -0.12907990 -2.55805781
## [20,] -1.86354074 -2.20944382 -1.51514298 -0.55388181
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
test
##             [,1]       [,2]        [,3]       [,4]        [,5]       [,6]
##  [1,]  4.7720179 -2.2312041  3.00080332 -0.9366317  4.20319759  0.3205714
##  [2,]  3.9424521 -0.2754375  3.11373910  1.2096740  3.41987282  0.6421479
##  [3,]  4.4973987 -1.1307243  3.26044653 -0.4188725  3.45326260  1.3619624
##  [4,]  3.8501888 -0.8845835  3.82712874  0.6842597  2.91332759 -0.4318051
##  [5,]  3.0531542  1.1030540  3.64472116 -0.7887825  4.29545924 -1.8366152
##  [6,]  1.5991548 -1.3790862  2.98038279 -0.9726631  2.07228801  0.9752939
##  [7,]  4.6024032  0.3061658  2.81960306  0.2041019  3.59584985  1.0096951
##  [8,]  2.8239686  0.7661342  3.58967485  0.1313049  3.22897116  1.8445092
##  [9,]  3.6573316 -0.5888971  1.38881677  1.5591689  5.62138415  0.6721580
## [10,]  2.0730684 -0.3591864  2.49799233  0.1459360  2.74597863  0.3135195
## [11,] -0.6973361 -0.1741696 -0.30516903  3.2746334  0.25285919  4.4891725
## [12,] -0.1099342  3.2963182  0.26784715  3.3966800 -1.35867564  1.8824649
## [13,]  0.9160942  1.0714715 -0.03861332  4.2530483  0.01620227  2.2608837
## [14,]  0.9591300  2.2151505  0.03247873  2.4794928 -0.33793071  1.7839424
## [15,]  0.1389228  4.5749927 -0.82138799  6.7838683 -0.27955335  6.7322514
## [16,] -1.1677052  7.2163652  0.57474134  6.4404896  0.72881284  6.2133074
## [17,]  0.6545647  7.2233523 -1.25572416  6.7058058 -0.18150514  6.0255111
## [18,]  0.4757077  8.8432732 -0.34726853  5.8853014 -0.76792964  5.6427455
## [19,]  1.1643460  5.2480215  0.50661301  6.1553126 -0.16582043  7.4860452
## [20,]  0.3837661  6.3658713  0.81594790  5.2058624  0.49802058  6.0526599
##              [,7]        [,8]        [,9]      [,10]
##  [1,]  1.00379784 -0.58144691  1.60550481  0.5316045
##  [2,]  4.55231261  0.07070202  1.86932074  0.5187714
##  [3,]  3.78524308 -0.05228953  3.35576326  0.4321242
##  [4,]  4.74756261  0.19563622  2.33011190  1.5299625
##  [5,]  4.22932663  0.72726560  2.82839510 -1.4485226
##  [6,]  2.71922489 -1.34570388  2.51160066 -0.2998264
##  [7,]  2.21405609  0.08970703  3.60995894  0.1180628
##  [8,]  3.25253274  0.68536158  4.49512323  0.4491236
##  [9,]  2.73888138 -0.64094770  3.78140770  1.0897644
## [10,]  2.86264988 -0.44924210  2.33586249 -0.6435719
## [11,]  0.84289802  1.87032474  0.74890295  0.2414990
## [12,]  1.16077980  1.75688040  0.18515907  1.6020332
## [13,] -1.79345046  2.35133139  1.15967892  1.9764219
## [14,]  0.08038960  2.22715220  1.53218708  2.4094066
## [15,]  1.13620192  6.68921973 -2.19263207  6.0065030
## [16,]  0.22028965  6.53116547  0.90517980  6.3145427
## [17,]  0.82787218  5.78182597 -0.01185775  5.4719176
## [18,] -0.83373060  4.45825199 -0.04465615  7.8138984
## [19,] -0.09277748  6.19117808 -0.12907990  3.4419422
## [20,] -1.86354074  3.79055618 -1.51514298  5.4461182
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")

画图

pheatmap(test)

pheatmap(test, kmeans_k = 2)

pheatmap(test, scale = "row", clustering_distance_rows = "correlation")

pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))

pheatmap(test, cluster_row = FALSE)

pheatmap(test, legend = FALSE)

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

# install.packages("RColorbrewer")
library(RColorBrewer)
pheatmap(
  test,#数据矩阵
  color = colorRampPalette(rev(brewer.pal(n = 7, name ="RdYlBu")))(100), #设定图标颜色
  kmeans_k = NA, #把一个样本所有的基因分为NA个cluster。NA可以是2,3,4等等,若不写则不聚类。
  breaks = NA, 
  border_color = "grey60",#单元格边框的颜色,如果不绘制单元格边框,则选择NA
  cellwidth = NA, #单元格的宽度,如果保留为 NA,则值取决于绘图窗口的大小。
  cellheight = NA, #单元格的高度,如果保留为 NA,则值取决于绘图窗口的大小。
  scale = "none", #按行“row”,列“column”或者无“none”标准化。
  cluster_rows = TRUE,#按行进行hclust聚类
  cluster_cols = TRUE, #按列进行hclust聚类
  clustering_distance_rows = "correlation",#用于聚类行的距离度量,距离度量有两种::Pearson相关系数的“correlation"或dist函数包含的距离度量(This must be one of "euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski". )。
  clustering_distance_cols = "euclidean", ##用于聚类列的距离度量,距离度量有两种::Pearson相关系数的“correlation"或dist函数包含的距离度量(This must be one of "euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski". )。
  clustering_method = "complete",#聚类方法同hclust(This should be (an unambiguous abbreviation of) one of "ward.D", "ward.D2", "single", "complete", "average" (= UPGMA), "mcquitty" (= WPGMA), "median" (= WPGMC) or "centroid" (= UPGMC).)
  cutree_rows = NA,#行被划分成的簇数,基于层次聚类(使用cutree),如果行没有聚类,则忽略该参数
  cutree_cols = NA,#列被划分成的簇数,基于层次聚类(使用cutree),如果行没有聚类,则忽略该参数
  treeheight_row = 50,#行聚类树的高度,默认50
  treeheight_col = 50,#列聚类树的高度,默认50
  legend = TRUE, #注释标签
  legend_breaks = NA,#将legend进行截断,例如只显示legend的前几行
  legend_labels = NA, #对上述legend_breaks进行命名,向量
  annotation_row = NA, #指定显示在热图左侧的注释的数据框。每行定义特定行的特征。数据和注释中的行使用相应的行名称进行匹配。请注意,配色方案会考虑变量是连续的还是离散的。
  annotation_col = NA, #同annotation_row,只不过是针对列的
  annotation = NA, #不推荐使用参数
  annotation_colors = NA, #手动为annotation_row和annotation_col进行配色的列表
  annotation_legend = TRUE, #注释是否画图
  annotation_names_row = TRUE, #行注释是否画图
  annotation_names_col = TRUE,#列注释是否画图
  drop_levels = TRUE, #是否未使用的level也显示在legend中
  show_rownames = T,#行名是否显示
  show_colnames = T, #列名是否显示
  main = NA,#图的标题
  fontsize = 10, #绘图基本字体的大小
  fontsize_row = 10, #行名的大小,默认与上述fontsize一致
  fontsize_col = 10,#列名的大小,默认与上述fontsize一致
  angle_col = c("270", "0", "45", "90", "315"), #列标签的角度,只能从(0, 45, 90, 270 and 315)中选。
  display_numbers = F, #单元格内是否显示数字
  number_format = "%.2f", #单元格中显示的数字的格式字符串。例如,“%.2f”显示 2 个小数位,“%.1e”显示指数表示法(更多信息请参见 sprintf)
  number_color = "grey30", #单元格中数字的颜色
  fontsize_number = 0.8, #单元格中显示字体的大小
  gaps_row = NULL, #将行进行间隙分割,如果已经聚类没法分割,可以进行cutree_rows.
  gaps_col = NULL, #同上,进行列的分割。
  labels_row = NULL, #用于代替行名的自定义标签
  labels_col = NULL, #用于代替列名
  filename = NA, #保存图片的文件路径。文件类型由路径中的扩展名决定。目前支持以下格式:png、pdf、tiff、bmp、jpeg。
  width = NA, #手动输出文件宽度,英寸为单位
  height = NA,#手动输出文件长度,英寸为单位
  silent = FALSE, #不绘制图选择TURE。
  na_col = "#DDDDDD") #单元格中NA数据的颜色