
文章原图:

(reference:Convergent molecular, cellular, and cortical neuroimaging signatures of major depressive disorder)
完整的复现效果:

读入数据
setwd("/Users/ks_ts/Documents/公众号文章/2022-11-22Complexheatmap热图聚类注释/")
Enrich_df<- read.csv("./Enrich_df.csv", header = T, row.names = 1)
library(dendextend)
library(ComplexHeatmap)
library(tibble)
library(ggplot2)
head(Enrich_df)
行聚类信息
Enrich_df1 <- as.data.frame(t(Enrich_df))#转置
group_info <- as.data.frame(rownames(Enrich_df1))#做一个分组信息数据
dend1 = cluster_between_groups(Enrich_df, group_info$`rownames(Enrich_df1)`)#聚类
dend1 = color_branches(dend1, k = 4, col = c("#F5A089",'#CBA53C','#88CFC8','#66A5CA'))#设置聚类数,颜色
dend1 = dend1 %>% set("branches_lwd", 2) #聚类树的宽度
dend1查看聚类情况
HM <- Heatmap(as.matrix(Enrich_df1),cluster_rows = dend1, cluster_columns = F)#做一个热图看看效果
HM 
画热图
#颜色设置,需要是原始行名顺序
group_info$cluser = cutree(dend1, k = 4)
group_info$color = ifelse(group_info$cluser=='1', "#F5A089",
ifelse(group_info$cluser=='2','#CBA53C',
ifelse(group_info$cluser=='3','#88CFC8','#66A5CA')))
col_cluster = group_info$color
names(col_cluster) <- group_info$`rownames(Enrich_df1)`
#-----作图
Heatmap(as.matrix(Enrich_df1),
cluster_rows = dend1,#行聚类用前面设置好的、这样颜色也对应设置
cluster_columns = F,
show_column_names = T,
show_row_names = T,
row_names_side = 'left',
column_title = NULL,
heatmap_legend_param = list(
title=' ',
labels=c('Present','Absent'),
direction = "horizontal",
labels_gp = gpar(fontsize = 10),
border='black'), #legend设置
col = c('#F2F2F0','#5A8FCA'),
rect_gp = gpar(col = "grey", lwd = 1),
row_names_gp = gpar(col=col_cluster, fontsize=10),#设置文字颜色
column_names_gp = gpar(fontsize = 10))

不过学习重在举一反三,我突然想到,有一点内容是可以用到和拓展的,那就是对于行列的注释,之前我们一直使用图形颜色注释,看起来费劲,那么是否可以直接将文字注释为不用的颜色呢?显然可以:
row_info = rowAnnotation(foo = anno_text(rownames(Enrich_df1),
location = 0,
just = "left",
gp = gpar(col=col_cluster, fontsize=10)))
Heatmap(as.matrix(Enrich_df1),
cluster_rows = T,
cluster_columns = F,
show_column_names = T,
show_row_names = F,
column_title = NULL,
heatmap_legend_param = list(
title=' ',
labels=c('Present','Absent'),
labels_gp = gpar(fontsize = 10),
border='black'),
col = c('#F2F2F0','#5A8FCA'),
rect_gp = gpar(col = "grey", lwd = 1),
row_names_gp = gpar(col=col_cluster, fontsize=10),
column_names_gp = gpar(fontsize = 10))+
row_info
这样就完成注释了,不同的颜色代表不同组!
ggplot2绘制
到这里,估计还有小伙伴想问ggplot2做怎么弄,就简单演示一下吧:ggplot2同样能够完成热图绘制,只不过没有那么灵活,需要将宽数据转化为长数据:
library(tidyr)
library(dplyr)
# 转换为长格式
df_long <- Enrich_df1 %>%
rownames_to_column("pathway") %>%
pivot_longer(cols = -pathway, names_to = "gene", values_to = "value")
#将value转化为因子,固定x轴基因顺序
df_long$value <- as.factor(df_long$value)
df_long$gene <- factor(df_long$gene,levels=colnames(Enrich_df1))
#plot
ggplot(data=df_long,aes(x=gene,y=pathway))+
geom_tile(aes(fill=value),color="grey")+
theme_minimal()+
theme(panel.border = element_rect(fill=NA,color="black", size=1, linetype="solid"),
panel.grid = element_blank(),
axis.ticks.y = element_blank(),
axis.title = element_blank(),
axis.text.x = element_text(angle=90,hjust=1, vjust=0.5,colour = 'black', size = 10),
axis.text.y = element_text(colour = 'black', size = 10),
plot.margin=unit(c(0.4,0.4,0.4,0.4),units=,"cm"))+
scale_fill_manual(values = c('#F2F2F0','#5A8FCA'))
绘制聚类:
library(ggtree)
##行聚类
phr <- hclust(dist(Enrich_df1),method = "ward.D2")
#plot聚类树
p_cluster <- ggtree(phr, branch.length ="none")
p_cluster
组合:
#将value转化为因子,固定x轴基因顺序
df_long$value <- as.factor(df_long$value)
df_long$gene <- factor(df_long$gene,levels=colnames(Enrich_df1))
#plot
p = ggplot(data=df_long,aes(x=gene,y=pathway))+
geom_tile(aes(fill=value),color="grey")+
theme_minimal()+
theme(panel.border = element_rect(fill=NA,color="black", size=1, linetype="solid"),
panel.grid = element_blank(),
axis.ticks.y = element_blank(),
axis.title = element_blank(),
axis.text.x = element_text(angle=90,hjust=1, vjust=0.5,colour = 'black', size = 10),
axis.text.y = element_text(colour = 'black', size = 10),
plot.margin=unit(c(0.4,0.4,0.4,0.4),units=,"cm"))+
scale_fill_manual(
values = c("0" = '#F2F2F0', "1" = '#5A8FCA'),
labels = c("0" = "Absent", "1" = "Present"), # 修改legend标签
name = "" # 修改legend标题
)
library(aplot)
p %>% insert_left(p_cluster, width =0.2)
关于ggtree的修饰以及ggplot2行名的修饰参考:
https://mp.weixin.qq.com/s/zNqvndqcqFQAScV-JXPJEg,https://mp.weixin.qq.com/s/4ol45q5D-zHSq4P7fmDrpg。