||
#教程借鉴于R包pheatmap:用参数一步步详细绘制热图 – 我的工作墙 (gzxuexi.xyz)
#第一步:公众路径设置,调用包pheatmap,读取目的文件,查看文件。
rm(list=ls())
#setwd("")
#getwd()
library(pheatmap)
library(ggplot2)
data <- read.csv("heatmap.csv",header=T,row.names=1)
dim(data)
head(data)
#第二步:逐步深入绘制热图(图1-图15)
#一建热图(图1)
p1.1<-pheatmap(data)
#设置标准化方向scale,对其横向标准化
p1.2<-pheatmap(data,scale="row")
#设置边框为白色,横向纵向聚类为无;border="white;
cluster_cols = F;cluster_rows = F
p1.3<-pheatmap(data,scale="row",border="white",cluster_cols = F,cluster_rows = F)
#去掉横纵坐标中的id;show_rownames = F,show_colnames = F(图2)
p2<-pheatmap(data,scale="row",show_rownames = F,show_colnames = F,
border="white",cluster_cols = F,cluster_rows = F)
#去掉右上角图例;legend = F(图3)
p3<-pheatmap(data,scale="row",show_rownames = F,show_colnames = F,legend = F,
border="white",cluster_cols = F,cluster_rows = F)
#设置右上角图例的范围;legend_breaks=c(-1,1)(图4)
p4.1<-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)
p4.2<-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)
#设置图中字的大小;fondsize(图5)
#图表字体:命令:fondsize=2/8
p5.1<-pheatmap(data,scale="row",fontsize = 2,show_rownames = T,
show_colnames = T,legend = F,border="white",
cluster_cols = F,cluster_rows = F)
p5.2<-pheatmap(data,scale="row",fontsize = 8,show_rownames = T,
show_colnames = T,legend = F,border="white",
cluster_cols = F,cluster_rows = F)
#改变横向和纵向字体大小;fontsize_row = 8,fontsize_col=12 (图6)
p6.1<-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)
p6.2<-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)
#设置横向纵向的树高;treeheight_col = 20,treeheight_row = 15(图7)
p7.1<-pheatmap(data,scale="row",border="white",cluster_cols = T,
treeheight_col = 20,cluster_rows = T,treeheight_row = 15)
p7.2<-pheatmap(data,scale="row",border="white",cluster_cols = T,
treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
#设置聚类的距离类型clustering_distance_rows(图8)
#clustering_distance_rows分为如下几类
#'correlation', 'euclidean', 'maximum', 'manhattan', 'canberra',
#'binary', 'minkowski'
p8.1<-pheatmap(data,scale="row",clustering_distance_rows = "correlation",
border="white",cluster_cols = T,treeheight_col = 20,
cluster_rows = T,treeheight_row = 20)
p8.2<-pheatmap(data,scale="row",clustering_distance_rows = "minkowski",
border="white",cluster_cols = T,treeheight_col = 20,
cluster_rows = T,treeheight_row = 20)
#调整聚类的方法;clustering_method="single"(图9)
#'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average',
#''mcquitty', 'median' or 'centroid'.
p9.1<-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)
p9.2<-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)
#分组类别类型,斜的?45°?(图10)
#angle_col=315
#可选“270”, “0”, “45”, “90”, “315”
p10.1<-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)
p10.2<-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)
#给图形增加标题;main="Gene1"(图11)
p11<-pheatmap(data,scale="row",main="Gene1",angle_col = 45,
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(图12)
p12.1<-pheatmap(data,scale="row",cellwidth = 6,cellheight = 6,main="Gene1",
angle_col = 45,clustering_distance_rows = "minkowski",
clustering_method="complete",border="white",cluster_cols = T,
treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
p12.2<-pheatmap(data,scale="row",cellwidth = 14,cellheight = 10,main="Gene1",
angle_col = 45,clustering_distance_rows = "minkowski",
clustering_method="complete",border="white",cluster_cols = T,
treeheight_col = 20,cluster_rows = T,treeheight_row = 20)
#根据热图聚类对其进行区块儿划分(图13)
#cutree_cols = 3,cutree_rows =4
p13.1<-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)
#在图13.1基础上增加边缘线(图13.2)
p13.2<-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)
#调整Gene的数值
#是否展示数值,大小和颜色,大小以及数值展示类型
#display_numbers = T,fontsize_number = 2,number_color="black"
#number_format:
p14.1<-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)
p14.2<-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)
p14.3<-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)
p14.3<-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)
p14.4<-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)
#对热图方块儿进行标记;display_numbers,如果该值大于10,则为+/**,否则为-/*(图15)
p15.1<-pheatmap(data,scale="row",number_color="red",number_format="%.2e",
border="#8B0A50",fontsize_number = 8,display_numbers =
matrix(ifelse(data > 10, "+", "-"), 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)
p15.2<-pheatmap(data,scale="row",number_color="red",number_format="%.2e",
border="#8B0A50",fontsize_number = 8,display_numbers =
matrix(ifelse(data > 10, "**", "*"), 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-图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)
rownames(annotation_col)
head(annotation_col)
#纵向分组热图
pheatmap(data, annotation_col = annotation_col)
p16<-pheatmap(data,scale="row",annotation_col = annotation_col,
number_color="red",number_format="%.2e",border="#8B0A50",
fontsize_number = 8,display_numbers =
matrix(ifelse(data > 10, "**", "*"), 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)
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)
p17<-pheatmap(data,scale="row",number_color="red",
annotation_row =annotation_row,number_format="%.2e",
border="#8B0A50",fontsize_number = 8,
display_numbers = matrix(ifelse(data > 10, "++", "-"), 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)
#共同组合二者(图18)
pheatmap(data,annotation_col = annotation_col,
annotation_row = annotation_row)
p18<-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 > 10, "++", "-"), 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)
setwd("C:/Users/53446/Desktop/heatmap")
ggsave(p1.1,filename = "p1.1.jpeg",width = 6,height = 8)
ggsave(p1.2,filename = "p1.2.jpeg",width = 6,height = 8)
ggsave(p1.3,filename = "p1.3.jpeg",width = 6,height = 8)
ggsave(p10.1,filename = "p10.1.jpeg",width = 6,height = 8)
ggsave(p10.2,filename = "p10.2.jpeg",width = 6,height = 8)
ggsave(p11,filename = "p11.jpeg",width = 6,height = 8)
ggsave(p12.1,filename = "p12.1.jpeg",width = 6,height = 8)
ggsave(p12.2,filename = "p12.2.jpeg",width = 6,height = 8)
ggsave(p13.1,filename = "p13.1.jpeg",width = 6,height = 8)
ggsave(p13.2,filename = "p13.2.jpeg",width = 6,height = 8)
ggsave(p14.1,filename = "p14.1.jpeg",width = 6,height = 8)
ggsave(p14.2,filename = "p14.2.jpeg",width = 6,height = 8)
ggsave(p14.3,filename = "p14.3.jpeg",width = 6,height = 8)
ggsave(p14.4,filename = "p14.4.jpeg",width = 6,height = 8)
ggsave(p15.1,filename = "p15.1.jpeg",width = 6,height = 8)
ggsave(p15.2,filename = "p15.2.jpeg",width = 6,height = 8)
ggsave(p16,filename = "p16.jpeg",width = 6,height = 8)
ggsave(p17,filename = "p17.jpeg",width = 6,height = 8)
ggsave(p18,filename = "p18.jpeg",width = 6,height = 8)
ggsave(p2,filename = "p2.jpeg",width = 6,height = 8)
ggsave(p3,filename = "p3.jpeg",width = 6,height = 8)
ggsave(p4.1,filename = "p4.1.jpeg",width = 6,height = 8)
ggsave(p4.2,filename = "p4.2.jpeg",width = 6,height = 8)
ggsave(p5.1,filename = "p5.1.jpeg",width = 6,height = 8)
ggsave(p5.2,filename = "p5.2.jpeg",width = 6,height = 8)
ggsave(p6.1,filename = "p6.1.jpeg",width = 6,height = 8)
ggsave(p6.2,filename = "p6.2.jpeg",width = 6,height = 8)
ggsave(p7.1,filename = "p7.1.jpeg",width = 6,height = 8)
ggsave(p7.2,filename = "p7.2.jpeg",width = 6,height = 8)
ggsave(p8.1,filename = "p8.1.jpeg",width = 6,height = 8)
ggsave(p8.2,filename = "p8.2.jpeg",width = 6,height = 8)
ggsave(p9.1,filename = "p9.1.jpeg",width = 6,height = 8)
ggsave(p9.2,filename = "p9.2.jpeg",width = 6,height = 8)
pheatmap(mat, color = colorRampPalette(rev(brewer.pal(n = 7, name =
"RdYlBu")))(100), kmeans_k = NA, breaks = NA, border_color = "grey60",
cellwidth = NA, cellheight = NA, scale = "none", cluster_rows = TRUE,
cluster_cols = TRUE, clustering_distance_rows = "euclidean",
clustering_distance_cols = "euclidean", clustering_method = "complete",
clustering_callback = identity2, cutree_rows = NA, cutree_cols = NA,
treeheight_row = ifelse((class(cluster_rows) == "hclust") || cluster_rows,
50, 0), treeheight_col = ifelse((class(cluster_cols) == "hclust") ||
cluster_cols, 50, 0), legend = TRUE, legend_breaks = NA,
legend_labels = NA, annotation_row = NA, annotation_col = NA,
annotation = NA, annotation_colors = NA, annotation_legend = TRUE,
annotation_names_row = TRUE, annotation_names_col = TRUE,
drop_levels = TRUE, show_rownames = T, show_colnames = T, main = NA,
fontsize = 10, fontsize_row = fontsize, fontsize_col = fontsize,
angle_col = c("270", "0", "45", "90", "315"), display_numbers = F,
number_format = "%.2f", number_color = "grey30", fontsize_number = 0.8
* fontsize, gaps_row = NULL, gaps_col = NULL, labels_row = NULL,
labels_col = NULL, filename = NA, width = NA, height = NA,
silent = FALSE, na_col = "#DDDDDD", ...)
## 函数解释
mat # 需要绘制热图的数字矩阵。
color # 表示颜色,赋值渐变颜色调色板colorRampPalette属性,选择"蓝,白,红"渐变,分为100个等级
# 例 color = colorRampPalette(c("navy", "white", "red"))(100)
kmeans_k # 绘制热图的行聚类数,如果是NA,那么行不会聚类。
breaks # 设置mat数值范围的数字序列
border_color # 表示热图上单元格边框的颜色,如果不绘制边框,则使用NA
cellwidth # 表示每个单元格的宽度,若选择NA则表示适应窗口
cellheight # 表示每个单元格的高度,若选择NA则表示适应窗口
scale # 表示值均一化的方向,或者按照行或列,或者没有,值可以是"row", “column” 或者"none"
cluster_rows # 表示进行行的聚类,值可以是FALSE或TRUE
cluster_cols # 表示进行列的聚类,值可以是FALSE或TRUE
clustering_distance_rows # 表示行距离度量的方法
clustering_distance_cols # 表示列距离度量的方法
clustering_method # 表示聚类方法,值可以是hclust的任何一种,
# 如"ward.D",“single”, “complete”, “average”, “mcquitty”, “median”, “centroid”, “ward.D2”
clustering_callback # 回调函数来修改聚类
cutree_rows # 基于层次聚类(使用cutree)划分行的簇数(如果未聚集行,则忽略参数)
cutree_cols # 基于层次聚类(使用cutree)划分列的簇数(如果未聚集行,则忽略参数)
treeheight_row # 行的树的高度,
treeheight_col # 列的树的高度
legend # TRUE或者FALSE,表示是否显示图例
legend_breaks # 设置图例的断点,如legend_breaks = -1:4
legend_labels # legend_breaks对应的标签例:legend_breaks = -1:4, legend_labels = c(“0”,“1e-4”, “1e-3”, “1e-2”, “1e-1”, “1”)
annotation_row # 行的分组信息,需要使用相应的行名称来匹配数据和注释中的行,注意之后颜色设置会考虑离散值还是连续值,格式要求为数据框
annotation_col # 列的分组信息,需要使用相应的列名称来匹配数据和注释中的列,格式要求为数据框
annotation_colors # 用于手动指定annotation_row和annotation_col track颜色的列表。
annotation_legend # 是否显示图例的名称。
annotation_names_row # 是否显示行注释的名称。
annotation_names_col # 是否显示列注释的名称。
show_rownames # 是否显示行名
show_colnames # 是否显示列名
main # 图的名字
fontsize # 图的字体大小
fontsize_row # 行名的字体大小,默认与图的字体大小相同
fontsize_col # 列名的字体大小,默认与图的字体大小相同
angle_col # 列标签的角度,可选择 (0, 45, 90, 270 and 315)
display_numbers # 表示是否将数值显示在热图的格子中,如果这是一个矩阵(与原始矩阵具有相同的尺寸),则显示矩阵的内容而不是原始值。
number_format # 设置显示数值的格式,较常用的有"%.2f"(保留小数点后两位),"%.1e"(科学计数法显示,保留小数点后一位)
number_color # 设置显示内容的颜色
fontsize_number # 设置显示内容的字体大小
labels_row # 代替行名的自定义标签
labels_col # 代替列名的自定义标签
filename # 图片保存位置以及文件名
width # 手动设置输出文件的宽度(单位:英寸)
height # 手动设置输出文件的高度(单位:英寸)
silent # 不绘制热图
na_col # 缺失值的颜色
#计算相关性
cor = corr.test(lefse, plant, method="spearman", adjust="none")
#提取相关性、p值
cmt <-cor$r
pmt <- cor$p
#判断显著性
if (!is.null(pmt)){
ssmt <- pmt< 0.01
pmt[ssmt] <-'**'
smt <- pmt >0.01& pmt <0.05
pmt[smt] <- '*'
pmt[!ssmt&!smt]<- ''
} else {
pmt <- F
}
#自定义颜色范围
mycol<-colorRampPalette(c("blue","white","tomato"))(800)
#可视化
pheatmap(cmt,scale = "none", cluster_row = F, cluster_col = F, border=NA, fontsize_row=8, fontsize_col=8, display_numbers = pmt, fontsize_number = 10, number_color = "white", cellwidth = 13, cellheight =13,color=mycol, filename="plant.lefse.jpg")
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-24 11:48
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社