一文学会pheatmap热图绘制

文摘   科技   2024-10-27 09:05   上海  

往期回顾


此前我们也花过几篇推送的篇幅初步介绍了热图绘制方法

三行命令搞定热图

热图绘制2.0

给你安排一个懂生信的工具人(四):手把手教你用在线 pheatmap 绘制热图

ComplexHeatmap学习手册


这次推送制作了视频带领大家如何熟练地用pheatmap去绘制一张满意的热图:
视频教程(也可点击文末原文链接跳转):

https://www.bilibili.com/video/BV1RT4y1Q7kX/



代码如下:


if(!require(pheatmap))install.packages('pheatmap')
## Loading required package: pheatmap
library(pheatmap)
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 = "")
test[1:5,1:5]
##          Test1       Test2    Test3       Test4    Test5
## Gene1 3.409634 2.17099900 3.414056 1.35232356 3.102758
## Gene2 1.833532 0.03594884 2.473906 -0.30569208 5.240678
## Gene3 3.598428 0.60195254 2.687834 -0.27628582 3.178967
## Gene4 4.490910 -1.78836287 1.715514 0.04071328 1.591885
## Gene5 2.158555 1.18389240 2.702553 0.10480618 2.565704

画一张最简单的热图

pheatmap(test)

如果你想指定Kmean聚类数量

pheatmap(test, kmeans_k = 4)

标准化

pheatmap(test, scale = "row")

pheatmap(test, scale = "column")

聚类方法

pheatmap(test, cluster_row = FALSE) #是否聚类 =FALSE不聚类,cols为列

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

 #列标准化,#clustering_distance_rows = "correlation"参数设定行聚类距离方法为Pearson corralation
pheatmap(test, clustering_method = "average") #选择聚类方法,可以选择'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'

drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols) #让行与列按不同的算法聚类

自定义颜色

pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50)) #设置渐变色及等级

#https://www.sioe.cn/yingyong/yanse-rgb-16/ #颜色代码网页

一些标签和图例的展示

pheatmap(test, legend = FALSE) #是否设置标签

pheatmap(test, display_numbers = TRUE) #是否显示数值

pheatmap(test, display_numbers = TRUE, number_format = "%.1e") #以科学计数法展现数值,%.2f以两位小数展示,number_color = "black"#设置字体颜色

#几位小数就是几f
pheatmap(test, display_numbers = matrix(ifelse(test > 0.01, "*", ""), nrow(test))) #条件判断+*

pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0","1e-4", "1e-3", "1e-2", "1e-1", "1")) #设置图例的范围

#legend_breaks参数设定图例显示范围,legend_labels参数添加图例标签
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap") #设置方格大小,主标题 fonsize = #设置字体大小,fontsize_row = 设置行名大小

pheatmap(test, border_color = "green") #设置格子边界的颜色 border = FALSE 去除边界线

pheatmap(test,show_rownames=F,show_colnames=F) # show_rownames和show_colnames参数设定是否显示行名和列名

pheatmap(test, treeheight_row = 30, treeheight_col = 50) # treeheight_row和treeheight_col参数设定行和列聚类树的高度,默认为50

pheatmap(test, angle_col = "45") #列名45°展示

pheatmap(test, cluster_rows = FALSE, gaps_row = c(4, 4))   #横向分割热图

pheatmap(test,labels_row = c("1", "2", "3", "4", "5", "6", "7", "8")) #自定义行的名称

行与列的注释

annotation_col = data.frame(
CellType = factor(rep(c("CT1", "CT2"), 5)),
Time = 1:5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")

annotation_row = data.frame(
GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
ann_colors = list(
Time = c("white", "firebrick"),
CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)

pheatmap(test, annotation_col = annotation_col,annotation_row = annotation_row, annotation_colors = ann_colors, main = "Title")

如何联系我们

公众号后台消息回复不便,这里给大家留一下领取资料及免费服务器(足够支持你完成硕博生涯的生信环境)的微信号,方便各位随时交流、提建议(别问在么,添加时直接说来意)。此外呼声一直很高的交流群也建好了,欢迎大家入群讨论:

永久免费的生信、科研交流群

大家可以阅读完这几篇之后添加
给生信入门初学者的小贴士
如何搜索公众号过往发布内容

您点的每个赞和在看,我都认真当成了喜欢


Biomamba 生信基地
本人为在读博士研究生,此公众号旨在分享生信知识及科研经验与体会,欢迎各位同学、老师与专家的批评指正,也欢迎各界人士的合作与交流。
 最新文章