R包pheatmap:用参数一步步详细绘制热图

R包pheatmap:用参数一步步详细绘制热图

经常会想到用热图来展示某个基因或某些基因的表达量问题,今天用R中pheatmap包一步步绘制热图。

第一步:公众路径设置,调用包pheatmap,读取目的文件,查看文件。

rm(list=ls())
setwd("D:/VIP/ARSTUDYLOCATION/heatmap/heatmap/")
getwd()

library(pheatmap)
library(ggplot2)

data <- read.table("test.FPKM.txt",header=T,row.names=1,sep="\t")
dim(data)
head(data)

第二步:逐步深入绘制热图(图1-图15)

一建热图(图1)
p<-pheatmap(data)
设置标准化方向scale,对其横向标准化
p<-pheatmap(data,scale="row")
设置边框为白色,横向纵向聚类为无;border="white;cluster_cols = F;cluster_rows = F
p<-pheatmap(data,scale="row",border="white",cluster_cols = F,cluster_rows = F)
图1

去掉横纵坐标中的id;show_rownames = F,show_colnames = F(图2)
p<-pheatmap(data,scale="row",show_rownames = F,show_colnames = F,border="white",cluster_cols = F,cluster_rows = F)

图2 去掉横轴和纵轴的名称
去掉右上角图例;legend = F(图3)
p<-pheatmap(data,scale="row",show_rownames = F,show_colnames = F,legend = F,border="white",cluster_cols = F,cluster_rows = F)




图3 将右上角的图例去掉

设置右上角图例的范围;legend_breaks=c(-1,1)(图4)
p<-pheatmap(data,scale="row",legend_breaks=c(-1,0,1),show_rownames = F,show_colnames = F,legend = T,border="white",cluster_cols = F,cluster_rows = F)
p<-pheatmap(data,scale="row",legend_breaks=c(-2,0,2),show_rownames = F,show_colnames = F,legend = T,border="white",cluster_cols = F,cluster_rows = F)
图4 更改图例范围
设置图中字的大小;fondsize(图5)
#图表字体:命令:fondsize=2/8
p<-pheatmap(data,scale="row",fontsize = 2,show_rownames = T,show_colnames = T,legend = F,border="white",cluster_cols = F,cluster_rows = F)
p2<-pheatmap(data,scale="row",fontsize = 8,show_rownames = T,show_colnames = T,legend = F,border="white",cluster_cols = F,cluster_rows = F)


图5 设置字的大小

改变横向和纵向字体大小;fontsize_row = 8,fontsize_col=12 (图6)
p<-pheatmap(data,scale="row",fontsize_row = 8,fontsize_col = 12,show_rownames = T,show_colnames = T,legend=T,border="white",cluster_cols = T,cluster_rows = T)
p<-pheatmap(data,scale="row",fontsize_row = 12,fontsize_col = 8,show_rownames = T,show_colnames = T,legend=T,border="white",cluster_cols = T,cluster_rows = T)

图6 改变横向和纵向字体的大小

设置横向纵向的树高;treeheight_col = 20,treeheight_row = 15(图7)
p<-pheatmap(data,scale="row",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 15)
p<-pheatmap(data,scale="row",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图7 设置横向和纵向聚类热图的树形高度

设置聚类的距离类型clustering_distance_rows(图8)
clustering_distance_rows分为如下几类
'correlation', 'euclidean', 'maximum', 'manhattan', 'canberra', 'binary', 'minkowski'
p<-pheatmap(data,scale="row",clustering_distance_rows = "correlation",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

p<-pheatmap(data,scale="row",clustering_distance_rows = "minkowski",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图8 聚类的距离类型(minkowski)

调整聚类的方法;clustering_method="single"(图9)
#'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'.
p<-pheatmap(data,scale="row",clustering_distance_rows = "minkowski",clustering_method="single",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
p<-pheatmap(data,scale="row",clustering_distance_rows = "minkowski",clustering_method="complete",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图9 调整聚类的方法
分组类别类型,斜的?45°?(图10)
angle_col=315
可选“270”, “0”, “45”, “90”, “315”
p<-pheatmap(data,scale="row",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
p<-pheatmap(data,scale="row",angle_col = 45,clustering_distance_rows = "minkowski",clustering_method="complete",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图10 分组是否斜体,旋转多少度
给图形增加标题;main="Gene1"(图11)
p<-pheatmap(data,scale="row",main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图11 图片添加主题
调整热图方块宽度和高度,cellwidth = 8,cellheight = 6(图12)
p<-pheatmap(data,scale="row",cellwidth = 6,cellheight = 6,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
p<-pheatmap(data,scale="row",cellwidth = 14,cellheight = 10,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图12 调整热图的宽度和高度
根据热图聚类对其进行区块儿划分(图13)
cutree_cols = 3,cutree_rows =4
p<-pheatmap(data,scale="row",cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",border="white",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
注:此处我去掉了热图块儿的大小;cellwidth = 8,cellheight = 6

图13 对聚类热图进化划分

在图13基础上增加边缘线(图13.1)
p<-pheatmap(data,scale="row",border="#8B0A50",cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图13.1 给热图块儿边缘添加颜色  
调整FPKM的数值(图14)
#是否展示数值,大小和颜色,大小以及数值展示类型
#display_numbers = T,fontsize_number = 2,number_color="black"
#number_format:
图14.1添加fpkm数值至热图方块儿
图14.2 设置数值的大小(放大还是缩小)
图14.3 设置值的颜色
图14.4 设置值的类型,保留小数?科学计数?
图14.5 筛选条件基因进行展示
p<-pheatmap(data,scale="row",border="#8B0A50",display_numbers = T,cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
p<-pheatmap(data,scale="row",border="#8B0A50",fontsize_number = 2,display_numbers = T,cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
p<-pheatmap(data,scale="row",border="#8B0A50",fontsize_number = 8,display_numbers = T,cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
p<-pheatmap(data,scale="row",number_color="red",border="#8B0A50",fontsize_number = 8,display_numbers = T,cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
p<-pheatmap(data,scale="row",number_color="red",number_format="%.2e",border="#8B0A50",fontsize_number = 8,display_numbers = T,cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图14.1 添加fpkm至热图方块儿内

图14.2 添加数值大小(放大缩小)

14.3 设置值的颜色

14.4 设置值的类型,小数?科学计数?

对热图方块儿进行标记;display_numbers,如果该值大于1,则为+,否则为-(图15)
p<-pheatmap(data,scale="row",number_color="red",number_format="%.2e",border="#8B0A50",fontsize_number = 8,display_numbers = matrix(ifelse(data > 1, "+", ""), nrow(data)),cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
p<-pheatmap(data,scale="row",number_color="red",number_format="%.2e",border="#8B0A50",fontsize_number = 8,display_numbers = matrix(ifelse(data > 2, "++", "-"), nrow(data)),cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图15 为热图方块儿内容添加标记

第三步:构建分组信息(这一步已经是微信推文的热图展示了)(图16-图18)

构建纵向和横向分组信息(有3个处理,分别是:盐、干旱和热应激);以及时间:0-3day,对3类基因21个基因进行分组,分别是:”WRKY”, “AP2”, “YABBY”

构建纵向分组信息(图16)
annotation_col = data.frame(Deal_with = factor(rep(c("Salt", "Drought","Heat"), 3)),Day=factor(rep(c("Day1", "Day2","Day3"), 3)))
rownames(annotation_col)
colnames(data)
rownames(annotation_col) <- colnames(data)
head(annotation_col)
纵向分组热图
pheatmap(data, annotation_col = annotation_col)
p<-pheatmap(data,scale="row",annotation_col = annotation_col,number_color="red",number_format="%.2e",border="#8B0A50",fontsize_number = 8,display_numbers = matrix(ifelse(data > 2, "++", "-"), nrow(data)),cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图16 图纵向分组热图
横向分组信息(图17)
annotation_row = data.frame(GeneClass = factor(rep(c("WRKY", "AP2", "YABBY"),7)))
rownames(annotation_row) <- rownames(data)
head(annotation_row)
横向分组热图
pheatmap(data, annotation_row =annotation_row)
p<-pheatmap(data,scale="row",number_color="red", annotation_row =annotation_row,number_format="%.2e",border="#8B0A50",fontsize_number = 8,display_numbers = matrix(ifelse(data > 2, "++", "-"), nrow(data)),cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)

图17 横向分组热图
共同组合二者(图18)
p<-pheatmap(data,annotation_col = annotation_col,annotation_row = annotation_row)
p<-pheatmap(data,scale="row",number_color="red",annotation_col = annotation_col,annotation_row = annotation_row,number_format="%.2e",border="#8B0A50",fontsize_number = 8,display_numbers = matrix(ifelse(data > 2, "++", "-"), nrow(data)),cutree_cols = 3,cutree_rows =4,main="Gene1",angle_col = 0,clustering_distance_rows = "minkowski",clustering_method="complete",cluster_cols = T,treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
保存为pdf/jpeg/png格式,当然建议保存为pdf/jpeg,因为二者清晰度高
filename = "基因家族热图.pdf",width = 6,height = 8
p<-pheatmap(data,annotation_col = annotation_col,annotation_row = annotation_row,filename = "基因家族热图.pdf",width = 6,height = 8)
p<-pheatmap(data,annotation_col = annotation_col,annotation_row = annotation_row,filename = "基因家族热图.jpeg",width = 6,height = 8)
p<-pheatmap(data,annotation_col = annotation_col,annotation_row = annotation_row,filename = "基因家族热图.png",width = 6,height = 8)

图18 横向和纵向分组信息添加

注:


如若侵犯您版权或者出错,请您及时联系我,谢谢!

如需测试数据,关注微信公众号后,请回复:”pheatmap“或”热图数据“或”heatmap“,即可获得我绘制图形的数据和保存的图片,用自己的数据同样可以哦!,有疑问可联系我或者回复:”加群“。

欢迎关注微信公众号:蚂蚁生信
smyang2018

跳至工具栏