使用ComplexHeatmap包绘制热图

加载所需R包

  1. library(ComplexHeatmap)
  2. require(circlize)
  3. # 设置工作路径
  4. setwd("/Users/Davey/Desktop/")
  5. # 清除当前环境中的变量
  6. rm(list=ls())

构建测试数据集

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

使用Heatmap函数绘制热图

  1. Heatmap(mat) #默认对行和列都进行聚类
  1. # col参数自定义颜色,colorRamp2函数来自于circlize包
  2. Heatmap(mat, col = colorRamp2(c(-5, 0, 5), c("green", "white", "red")))
  1. # name参数设定图例标题
  2. Heatmap(mat, name = "test")
  1. # heatmap_legend_param参数设定图例的格式(标题,位置,方向,高度等)
  2. Heatmap(mat, heatmap_legend_param = list(
  3. title= "legend", title_position = "topcenter",
  4. legend_height=unit(8,"cm"), legend_direction="vertical"))
  1. # row_title和column_title参数设定行和列的标题
  2. Heatmap(mat, row_title = "blablabla", column_title = "blablabla")
  1. # column_title_side参数设定列标题放置的位置,column_title_rot参数设定列标题文本旋转的角度
  2. Heatmap(mat, column_title = "blablabla", column_title_side = "bottom", column_title_rot = 90)
  1. # column_title_gp参数设定列标题文本的格式(字体,大小,颜色等)
  2. Heatmap(mat, column_title = "blablabla", column_title_gp = gpar(fontsize = 20, fontface = "bold", col="red"))
  1. # cluster_rows和cluster_columns参数设定行或列是否聚类
  2. Heatmap(mat, cluster_rows = FALSE, cluster_columns = FALSE)
  1. # clustering_distance_rows参数设定行聚类的距离方法,默认为"euclidean"
  2. Heatmap(mat, clustering_distance_rows = "pearson")
  1. Heatmap(mat, clustering_distance_rows = function(x) dist(x))
  1. Heatmap(mat, clustering_distance_rows = function(x, y) 1 - cor(x, y))
  1. # clustering_method_rows参数设定行聚类的方法,默认为"complete"
  2. Heatmap(mat, clustering_method_rows = "single")
  1. # row_dend_side参数设定行聚类树放置的位置
  2. Heatmap(mat, row_dend_side = "right")
  1. # row_dend_width参数设定行聚类树的宽度
  2. Heatmap(mat, row_dend_width = unit(2, "cm"))
  1. # row_names_side和column_names_side参数设置行名和列名存放的位置
  2. Heatmap(mat, row_names_side = "left", row_dend_side = "right",
  3. column_names_side = "top", column_dend_side = "bottom")
  1. # show_row_names参数设定是否显示行名,show_row_dend参设设定是否显示行聚类树
  2. Heatmap(mat, show_row_names = FALSE, show_row_dend = FALSE)
  1. # row_names_gp参数设定行名文本的格式
  2. Heatmap(mat, row_names_gp = gpar(fontsize = 20, fontface="italic", col="red"))
  1. # km参数设定对行进行kmeans聚类分组的类数
  2. Heatmap(mat, km = 4, row_title_gp = gpar(col=rainbow(4)), row_names_gp = gpar(col=rainbow(4), fontsize=20))

使用HeatmapAnnotation函数构建注释对象

  1. annotation = data.frame(value = rnorm(10))
  2. annotation = HeatmapAnnotation(df = annotation)
  3. # top_annotation参数在顶部添加注释信息
  4. Heatmap(mat, top_annotation = annotation)
  1. annotation = data.frame(value = rnorm(10))
  2. value = 1:10
  3. ha = HeatmapAnnotation(df = annotation, points = anno_points(value),
  4. annotation_height = c(1, 2))
  5. # top_annotation_height参数设定顶部注释信息展示的高度
  6. Heatmap(mat, top_annotation = ha, top_annotation_height = unit(2, "cm"),
  7. bottom_annotation = ha)

使用add_heatmap函数组合多个热图或注释信息

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

row_order和column_order函数获得热图聚类后行和列对应的顺序

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

使用densityHeatmap函数绘制密度热图

  1. # 构建测试数据集
  2. matrix = matrix(rnorm(100), 10); colnames(matrix) = letters[1:10]
  3. # 默认不对列进行聚类,不显示聚类树
  4. densityHeatmap(matrix)
  1. # anno参数添加注释信息
  2. densityHeatmap(matrix, anno = rep(c("A", "B"), each = 5), cluster_columns = T, show_column_dend = T)
  1. # col参数自定义颜色
  2. densityHeatmap(matrix, col = c("blue", "white", "red"), anno = rep(c("A", "B"), each = 5))
  1. # 构建注释对象
  2. ha = HeatmapAnnotation(points = anno_points(runif(10), gp=gpar(col=rainbow(10))))
  3. densityHeatmap(matrix, anno = ha)
  1. # 构建一个list
  2. lt = list(rnorm(10), runif(10), rnorm(10))
  3. densityHeatmap(lt)
  1. sessionInfo()
  1. ## R version 3.5.1 (2018-07-02)
  2. ## Platform: x86_64-apple-darwin15.6.0 (64-bit)
  3. ## Running under: OS X El Capitan 10.11.3
  4. ##
  5. ## Matrix products: default
  6. ## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
  7. ## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
  8. ##
  9. ## locale:
  10. ## [1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8
  11. ##
  12. ## attached base packages:
  13. ## [1] grid stats graphics grDevices utils datasets methods
  14. ## [8] base
  15. ##
  16. ## other attached packages:
  17. ## [1] circlize_0.4.4 ComplexHeatmap_1.18.1
  18. ##
  19. ## loaded via a namespace (and not attached):
  20. ## [1] Rcpp_0.12.18 digest_0.6.16 rprojroot_1.3-2
  21. ## [4] backports_1.1.2 magrittr_1.5 evaluate_0.11
  22. ## [7] stringi_1.2.4 GlobalOptions_0.1.0 GetoptLong_0.1.7
  23. ## [10] rmarkdown_1.10 RColorBrewer_1.1-2 rjson_0.2.20
  24. ## [13] tools_3.5.1 stringr_1.3.1 yaml_2.2.0
  25. ## [16] compiler_3.5.1 colorspace_1.3-2 shape_1.4.4
  26. ## [19] htmltools_0.3.6 knitr_1.20

 

发表评论

匿名网友

拖动滑块以完成验证
加载失败