R-pheatmap绘制热图 -学习笔记

相关参数学习

mat	用于绘制热图的数据集
color	表示热图颜色,colorRampPalette(rev(brewer.pal(n = 7, name = “RdYlBu”)))(100)表示颜色渐变调色板,“n” 的数量取决于调色板中颜色的数量,“name” 为调色板的名称,(100)表示100个等级;color = colorRampPalette(c(“blue”, “white”, “red”))(100)则是通过设置三种不同的颜色进行渐变显示
scale	表示进行均一化的方向,值为 “row”, “column” 或者"none"
kmeans_k	默认为NA,即不会对行进行聚类;如果想在进行层次聚类之前,先对行特征(因子)进行 k-means 聚类,则可在此调整热图的行聚类数
cluster_rows	表示仅对行聚类,值为TRUE或FALSE
cluster_cols	表示仅对列聚类,值为TRUE或FALSE
clustering_distance_rows	表示行聚类使用的度量方法,默认为欧式距离“euclidean”,也可选用其他度量方法,如可选用 "correlation"表示按照 Pearson correlation方法进行聚类
clustering_distance_cols	表示列聚类使用的度量方法,与行聚类的度量方法一致
clustering_method	表示聚类方法,包括:‘ward’, ‘ward.D’, ‘ward.D2’, ‘single’, ‘complete’, ‘average’, ‘mcquitty’, ‘median’, ‘centroid’
clustering_callback	修饰聚类的回调函数,默认为 “identity2”
cutree_rows	若进行了行聚类,根据行聚类数量分隔热图行
cutree_cols	若进行了列聚类,根据列聚类数量分隔热图列
treeheight_row	若进行了行聚类,其热图行的聚类树高度,默认为 “50”
treeheight_col	若进行了列聚类,其热图列的聚类树高度,默认为 “50”
breaks	用来定义数值和颜色的对应关系,默认为 “NA”
border_color	表示热图每个小的单元格边框的颜色,默认为 “NA”
cellwidth	表示单个单元格的宽度,默认为 “NA”,即根据窗口自动调整
cellheight	表示单个单元格的高度,默认为 “NA”,即根据窗口自动调整
fontsize	表示热图中字体大小
fontsize_row	表示行名字体大小,默认与fontsize一致
fontsize_col	表示列名字体大小,默认与fontsize一致
fontsize_number	表示热图上显示数字的字体大小
angle_col	表示列标签的角度,可选择 “0”,“45”,“90”,“270”,“315”
display_numbers	表示是否在单元格上显示原始数值或按照特殊条件进行区分标记
number_format	表示热图单元格上显示的数据格式,如 “%.2f” 表示两位小数; “%.1e” 表示科学计数法
number_color	表示热图单元格上显示的数据字体颜色
legend	表示是否显示图例,值为TRUE或FALSE
legend_breaks	表示图例断点的设置,默认为NA
legend_labels	表示图例断点的标签
annotation_row	表示是否对行进行注释
annotation_col	表示是否对列进行注释
annotation_colors	表示行注释及列注释的颜色
annotation_legend	表示是否显示注释的图例信息
annotation_names_row	表示是否显示行注释的名称
annotation_names_col	表示是否显示列注释的名称
show_rownames	表示是否显示行名
show_colnames	表示是否显示列名
main	表示热图的标题名字
gaps_row	仅在未进行行聚类时使用,表示在行方向上热图的隔断位置,如 gaps_row = c(2, 4)表示在第2与第4列进行隔断
gaps_col	仅在未进行列聚类时使用,表示在列方向上热图的隔断位置,同 gaps_row
labels_row	表示使用行标签代替行名
labels_col	表示使用列标签代替列名
filename	表示保存图片的位置及命名
width	表示输出绘制热图的宽度
height	表示输出绘制热图的高度
silent	表示不绘制热图
margins	表示热图距画布的空白距离
参考链接:https://blog.csdn.net/qq_43210428/article/details/120020284

基因和样本都可以单独聚类,排序,聚类再分组,行列注释,配色调整,调整聚类线以及单元格的宽度和高度均可实现。

#R包
BiocManager::install("pheatmap")
library(pheatmap)
# 构建测试数据 
set.seed(1234)
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[,1:6])
数据格式如上
#基础绘制热图
pheatmap(test)

基本参数

# scale = "row"参数对行进行归一化
# clustering_method参数设定不同聚类方法,默认为"complete",可以设定为'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'

pheatmap(test,scale = "row", clustering_method = "average")

#表示行聚类使用皮尔森相关系数聚类,默认为欧氏距离"euclidean"

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

#行 列是否聚类,cluster_row ,cluster_col

pheatmap(test, cluster_row = FALSE,cluster_col = TRUE)

# treeheight_row和treeheight_col参数设定行和列聚类树的高度,默认为50

pheatmap(test, treeheight_row = 30, treeheight_col = 50)

# 设定cell 的大小

pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 10)

设定 text

热图中展示数值

# display_numbers = TRUE参数设定在每个热图格子中显示相应的数值,#number_color参数设置数值字体的颜色

pheatmap(test, display_numbers = TRUE,number_color = "blue")

# 设定数值的显示格式

pheatmap(test, display_numbers = TRUE, number_format = "%.1e")

#设定条件式展示

pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))

设置 legend

设定legend展示的值
#legend_breaks参数设定图例显示范围,legend_labels参数添加图例标签

pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0", "1e-4", "1e-3", "1e-2", "1e-1", "1"))

#去掉legend

pheatmap(test, legend = FALSE)

设定 color

自定义颜色

#colorRampPalette

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

# border_color参数设定每个热图格子的边框色

# border=TRIUE/FALSE参数是否要边框线

设定 annotations

# 生成行 列的注释

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 = "")
#添加列的注释
pheatmap(test, annotation_col = annotation_col)

#添加行 列的注释

#angle_col 改变列标签的角度

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")

# 根据聚类结果,自定义注释分组及颜色

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")

设定 gap
#根据聚类结果,设定行gap

pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))

#根据聚类结果,设定列gap

pheatmap(test,annotation_col = annotation_col, cluster_rows = FALSE,cutree_col = 2)

#展示行或者列的label

labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Il10", "Il15", "Il1b")

pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)

热图汇总

pheatmap(test,annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors,gaps_row = c(10, 14),cutree_col = 2,main = "Pheatmap")
输出结果

输出结果

A = pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, annotation_colors = ann_colors,gaps_row = c(10, 14),cutree_col = 2,main = "Pheatmap") #记录热图的行排序
order_row = A$tree_row$order 
 #记录热图的列排序
order_col = A$tree_col$order   
# 按照热图的顺序,重新排原始数据