setwd("C:\Users\admin\Desktop")
library('pheatmap')
library('openxlsx')
df1<-read.xlsx("aa.xlsx",colNames = T,rowNames = T, sheet = 1) ##colnames和rownames均为T
列注释信息
col_order<-read.xlsx("aa.xlsx",colNames = T,rowNames = T, sheet = 2) ##colnames和rownames均为T
行注释信息
row_order<-read.xlsx("aa.xlsx",colNames = T,rowNames = T, sheet = 3) ##colnames和rownames均为T



自定义单元格颜色
mycol<-colorRampPalette(c("white","red"))(100)
常规出图
p<-pheatmap(df1,
scale ="row", ###none column row 均一化设置
# clustering_method参数设定不同聚类方法,默认为"complete",可以设定为'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'
clustering_method = "complete",
# cluster_row = FALSE参数设定不对行进行聚类
cluster_row = FALSE,
# clustering_distance_rows = "correlation"参数设定行聚类距离方法为Pearson correlation,默认为欧氏距离"euclidean"
clustering_distance_rows = "correlation",
col=mycol,
# legend_breaks参数设定图例显示范围,legend_labels参数添加图例标签
legend = FALSE,
legend_breaks = c(1:5), legend_labels = c("1.0","2.0","3.0","4.0","5.0"),
# border_color参数设定每个热图格子的边框色
border_color ="blue",
show_rownames=T,
show_colnames = T,
treeheight_row=40, ## 聚类树的高度设置
treeheight_col=40, #### 聚类树的高度设置
# display_numbers = TRUE参数设定在每个热图格子中显示相应的数值,number_color参数设置数值字体的颜色
display_numbers = F,number_color = "blue",
# cellwidth和cellheight参数设定每个热图格子的宽度和高度,main参数添加主标题
cellwidth = 10, cellheight = 40, main = "Example heatmap",
fontsize=10, ##字体设置
fontsize_col=10,
angle_col = 90
)
p

添加分组标记
######### a:根据自定义分组进行标记(excel中)
构建标记颜色
anno_colors<-list(Group_1=c(good="red",bad="blue"),
Group_2=c(a="yellow",b="grey"))
p1<-pheatmap(df1,
scale ="row", ###none column row 均一化设置
# clustering_method参数设定不同聚类方法,默认为"complete",可以设定为'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'
clustering_method = "complete",
# cluster_row = FALSE参数设定不对行进行聚类
cluster_row = FALSE,
# clustering_distance_rows = "correlation"参数设定行聚类距离方法为Pearson correlation,默认为欧氏距离"euclidean"
clustering_distance_rows = "correlation",
col=mycol,
# legend_breaks参数设定图例显示范围,legend_labels参数添加图例标签
legend = FALSE,
legend_breaks = c(1:5), legend_labels = c("1.0","2.0","3.0","4.0","5.0"),
# border_color参数设定每个热图格子的边框色
border_color ="blue",
show_rownames=T,
show_colnames = T,
treeheight_row=40, ## 聚类树的高度设置
treeheight_col=40, #### 聚类树的高度设置
# display_numbers = TRUE参数设定在每个热图格子中显示相应的数值,number_color参数设置数值字体的颜色
display_numbers = F,number_color = "blue",
# cellwidth和cellheight参数设定每个热图格子的宽度和高度,main参数添加主标题
cellwidth = 10, cellheight = 40, main = "Example heatmap",
fontsize=10, ##字体设置
fontsize_col=10,
angle_col = 90,
annotation_col = col_order,
annotation_row=row_order,
annotation_colors=anno_colors
)
p1

b 根据聚类进行标记 (强行注释)
构建列注释信息
annotation_col = data.frame(
cluster = factor(c(rep("CT1",5),rep("CT2",18)))) #### 可根据聚类结果自行调整
rownames(annotation_col) = paste("T", p[["tree_col"]][["order"]], sep = "")
head(annotation_col)
构建标记颜色
ann_colors = list(
cluster = c(CT1 = "#1B9E77", CT2 = "#D95F02"))
p1<-pheatmap(df1,
scale ="row", ###none column row 均一化设置
# clustering_method参数设定不同聚类方法,默认为"complete",可以设定为'ward', 'ward.D', 'ward.D2', 'single', 'complete', 'average', 'mcquitty', 'median' or 'centroid'
clustering_method = "complete",
# cluster_row = FALSE参数设定不对行进行聚类
cluster_row = FALSE,
# clustering_distance_rows = "correlation"参数设定行聚类距离方法为Pearson correlation,默认为欧氏距离"euclidean"
clustering_distance_rows = "correlation",
col=mycol,
# legend_breaks参数设定图例显示范围,legend_labels参数添加图例标签
legend = FALSE,
legend_breaks = c(1:5), legend_labels = c("1.0","2.0","3.0","4.0","5.0"),
# border_color参数设定每个热图格子的边框色
border_color ="blue",
show_rownames=T,
show_colnames = T,
treeheight_row=40, ## 聚类树的高度设置
treeheight_col=40, #### 聚类树的高度设置
# display_numbers = TRUE参数设定在每个热图格子中显示相应的数值,number_color参数设置数值字体的颜色
display_numbers = F,number_color = "blue",
# cellwidth和cellheight参数设定每个热图格子的宽度和高度,main参数添加主标题
cellwidth = 10, cellheight = 40, main = "Example heatmap",
fontsize=10, ##字体设置
fontsize_col=10,
angle_col = 90,
annotation_col = annotation_col,
annotation_colors = ann_colors
)
p1

