R绘制带显著性标记的热图

今天小编接着来介绍R绘制带有显著性的热图。

测试数据文件:
①table1.txt
在这里插入图片描述
②col_group.txt
在这里插入图片描述
③row_group.txt
R绘制带显著性标记的热图_第1张图片
1.导入pheatmap包,读取数据;

#导入包
library(pheatmap)
#读取数据
data<-read.table("table1.txt",header=T,row.names = 1,sep="\t")

2.直接作图;

#直接作图
pheatmap(data)

R绘制带显著性标记的热图_第2张图片
3.对相关参数指标进行调整;

# scale = "row"参数对行进行归一化
pheatmap(data, scale = "row")
# clustering_method参数设定不同聚类方法,默认为"complete",可以设定为'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'
pheatmap(data,scale = "row", clustering_method = "average")
# clustering_distance_rows = "correlation"参数设定行聚类距离方法为Pearson corralation,默认为欧氏距离"euclidean"
pheatmap(data, scale = "row", clustering_distance_rows = "correlation")
# cluster_row = FALSE参数设定不对行进行聚类
pheatmap(data, cluster_row = FALSE)
# legend = FALSE参数去掉图例
pheatmap(data, legend = FALSE)
# border_color参数设定每个热图格子的边框色
pheatmap(data, border_color = "gray")
# border=FALSE参数去掉边框线
pheatmap(data, border=FALSE)
# show_rownames和show_colnames参数设定是否显示行名和列名
pheatmap(data,show_rownames=F,show_colnames=F)
# treeheight_row和treeheight_col参数设定行和列聚类树的高度,默认为50
pheatmap(data, treeheight_row = 30, treeheight_col = 50)
# display_numbers = TRUE参数设定在每个热图格子中显示相应的数值,number_color参数设置数值字体的颜色
pheatmap(data, display_numbers = TRUE,number_color = "gray")
# number_format = "%.1e"参数设定数值的显示格式
pheatmap(data, display_numbers = TRUE, number_format = "%.1e")
# 自定义数值的显示方式,带显著性标记的热图
pheatmap(data, display_numbers = matrix(ifelse(data > 0.12, "*", ""), nrow(data)))

R绘制带显著性标记的热图_第3张图片

# legend = FALSE参数去掉图例
pheatmap(data, legend = FALSE)
# border_color参数设定每个热图格子的边框色
pheatmap(data, border_color = "gray")
# border=FALSE参数去掉边框线
pheatmap(data, border=FALSE)
# show_rownames和show_colnames参数设定是否显示行名和列名
pheatmap(data,show_rownames=F,show_colnames=F)
# treeheight_row和treeheight_col参数设定行和列聚类树的高度,默认为50
pheatmap(data, treeheight_row = 30, treeheight_col = 50)
# display_numbers = TRUE参数设定在每个热图格子中显示相应的数值,number_color参数设置数值字体的颜色
pheatmap(data, display_numbers = TRUE,number_color = "gray")
# number_format = "%.1e"参数设定数值的显示格式
pheatmap(data, display_numbers = TRUE, number_format = "%.1e")
# 自定义数值的显示方式,带显著性标记的热图
pheatmap(data, display_numbers = matrix(ifelse(data > 0.12, "*", ""), nrow(data)))
# cellwidth和cellheight参数设定每个热图格子的宽度和高度,main参数添加主标题
pheatmap(data, cellwidth = 15, cellheight = 12, main = " heatmap")
colgroup=read.table("col_group.txt",sep="\t",header=F,row.names=1,check.names=F,quote="")
rowgroup=read.table("row_group.txt",sep="\t",header=F,row.names=1,check.names=F,quote="")
# annotation_col参数添加列注释信息
pheatmap(data, annotation_col = colgroup)
colnames(colgroup)=c("col")
# annotation_col参数添加行注释信息
pheatmap(data, annotation_row = rowgroup)
colnames(rowgroup)=c("row")
# annotation_legend = FALSE参数去掉注释图例
pheatmap(data, annotation_col = colgroup, annotation_legend = FALSE)
# annotation_col和annotation_row参数同时添加行和列的注释信息
pheatmap(data,annotation_col = colgroup,annotation_row=rowgroup)

R绘制带显著性标记的热图_第4张图片
4.对注释信息进行颜色、聚类等相关设置,绘制出带有显著性标记的热图;

# 自定注释信息的颜色列表
ann_colors = list(
  col = c(A = "#7570B3", B = "#E7298A", C = "#66A61E"),
  row = c(a = "#FF0000", b = "#3A5FCD", c = "#7CCD7C",d="#8A2BE2",e="#CDAD00",f="#EEEE00",g="#8B6914",h="#FF00FF")
)
# annotation_colors设定注释信息的颜色
pheatmap(data,annotation_col = colgroup,annotation_row=rowgroup, annotation_colors = ann_colors, main = "Title")
# gaps_row = c(10, 4)参数在第10和14行处添加gap, 要求对行不进行聚类
pheatmap(data, annotation_col = colgroup, cluster_rows = FALSE, gaps_row = c(10, 4))
# cutree_col = 2参数将列按聚类树的结果分成两部分, 要求对列进行聚类
pheatmap(data, annotation_col = colgroup, cluster_rows = FALSE, gaps_row = c(10, 5),
         cutree_col = 2)
# 对行和列都不聚类,自定义划分行和列的gap
pheatmap(data, annotation_col = colgroup, cluster_rows = FALSE, cluster_cols = FALSE, 
         gaps_row = c(6, 10, 14), gaps_col = c(2, 5, 8),display_numbers = matrix(ifelse(data > 0.1, "*", ""), nrow(data)))

R绘制带显著性标记的热图_第5张图片
END

你可能感兴趣的:(R绘制带显著性标记的热图)