本教程相关代码已经上传至 https://github.com/iMetaScience/iMetaPlot/tree/main/221125HeatmapOfAbundance
如果你使用本代码,请引用:
Zuguang Gu. 2022. Complex heatmap visualization. iMeta 1: e43. https://doi.org/10.1002/imt2.43
代码编写及注释:农心生信工作室
接下来,我们将通过详尽的代码逐步拆解原图,最终实现对原图的复现。
R包检测和安装
01
安装核心R包ComplexHeatmap以及一些功能辅助性R包,并载入所有R包。
if (!require("BiocManager"))
install.packages('BiocManager')
if (!require("ComplexHeatmap"))
BiocManager::install('ComplexHeatmap')
# 加载包
library(ComplexHeatmap)
生成测试数据
02
基于补充文件里的一些相关数据,在这里我们通过生成随机数据来替代。最终生成一个相对丰度矩阵及class-family-species对应表,一个分组数据表。相关示例数据可以在GitHub上下载。
#读取相对丰度矩阵及class-family-species对应表
df <- read.csv("test.csv", header = T)
#读取分组数据表
group_df <- read.csv("group.csv", header = F)
#按Class和Family列对df的所有行进行排序
df <- df[order(df$Class, df$Family), ]
#获得相对丰度矩阵
mat <- df[-c(1, 2, 3)]
mat <- as.matrix(mat) #将数据框转化为矩阵
row.names(mat) <- df[, 3]
#获得class-family-species对应表
df_annotation <- df[c(1, 2, 3)]
环形热图预览
03
开始作图,首先画一个最基本的热图:
ht <- Heatmap(mat)
04
调整参数美化热图:
ht <- Heatmap(mat,
cluster_rows = F, #不按行聚类
show_column_names = F, #不展示列名
heatmap_legend_param = list(title = "Log2 relative abundance"), #设置热图图例名称
col = c("#FFFFFF", "#D32F2F")) #设置热图颜色
05
添加行分组注释和列分组注释。
#生成列分组注释
top_col <- c("Normal"="#2196F3", "Obese"="#FFEB3B", "NASH"="#E91E63")
top_anno <- HeatmapAnnotation(State = group_df[, 2],
col = list(State =top_col), #设置颜色
annotation_legend_param = list(State = list(at=c("Normal", "Obese", "NASH")))) #设置图例顺序
#生成行分组注释
left_col <- c("Bacteroidaceae"="#1976D2", "Marinifilaceae"="#2196F3", "Porphyromonadaceae"="#03A9F4", "Rikenellaceae"="#00BCD4", "Tannerellaceae"="#B3E5FC",
"Campylobacteraceae"="#F8BBD0",
"Family XI"="#D32F2F", "Family XIII"="#F44336", "Lachnospiraceae"="#E64A19", "Peptostreptococcaceae"="#FF5722", "Ruminococcaceae"="#FFCCBC",
"Enterobacteriaceae"="#4CAF50",
"Leuconostocaceae"="#FFEB3B",
"Veillonellaceae"="#673AB7") #生成行分组颜色的具名向量
left_anno <- rowAnnotation(Family = df_annotation[, 2],
show_annotation_name = F, #不展示行分组注释名
col = list(Family =left_col),
annotation_legend_param = list(
Family = list(at=c("Bacteroidaceae", "Marinifilaceae",
"Porphyromonadaceae", "Rikenellaceae",
"Tannerellaceae", "Campylobacteraceae",
"Family XI", "Family XIII", "Lachnospiraceae",
"Peptostreptococcaceae", "Ruminococcaceae",
"Enterobacteriaceae", "Leuconostocaceae",
"Veillonellaceae"))))
ht <- Heatmap(mat,
cluster_rows = F, #不按行聚类
show_column_names = F, #不展示列名
heatmap_legend_param = list(title = "Log2 relative abundance"), #设置热图图例名称
col = c("#FFFFFF", "#D32F2F"), #设置热图颜色
top_annotation = top_anno, #添加列注释
left_annotation = left_anno) #添加行注释
06
给Class单独添加图例:
class <- unique(df_annotation$Class)
#绘制class单独的图例
lgd <- Legend(labels = class, title = "Class",
legend_gp = gpar(fill = c("#1976D2", "#F8BBD0", "#D32F2F", "#4CAF50", "#FFEB3B", "#673AB7")))
#将单独图例与其他部分合并
pdf("plot.pdf", width = 11, height = 10)
draw(ht, ht_gap = unit(7, "mm"), annotation_legend_list = lgd)
dev.off()
#> quartz_off_screen
#> 2
完整代码
if (!require("BiocManager"))
install.packages('BiocManager')
if (!require("ComplexHeatmap"))
BiocManager::install('ComplexHeatmap')
# 加载包
library(ComplexHeatmap)
#读取相对丰度矩阵及class-family-species对应表
df <- read.csv("test.csv", header = T)
#读取分组数据表
group_df <- read.csv("group.csv", header = F)
#按Class和Family列对df的所有行进行排序
df <- df[order(df$Class, df$Family), ]
#获得相对丰度矩阵
mat <- df[-c(1, 2, 3)]
mat <- as.matrix(mat) #将数据框转化为矩阵
row.names(mat) <- df[, 3]
#获得class-family-species对应表
df_annotation <- df[c(1, 2, 3)]
#生成列分组注释
top_col <- c("Normal"="#2196F3", "Obese"="#FFEB3B", "NASH"="#E91E63")
top_anno <- HeatmapAnnotation(State = group_df[, 2],
col = list(State =top_col), #设置颜色
annotation_legend_param = list(State = list(at=c("Normal", "Obese", "NASH")))) #设置图例顺序
#生成行分组注释
left_col <- c("Bacteroidaceae"="#1976D2", "Marinifilaceae"="#2196F3", "Porphyromonadaceae"="#03A9F4", "Rikenellaceae"="#00BCD4", "Tannerellaceae"="#B3E5FC",
"Campylobacteraceae"="#F8BBD0",
"Family XI"="#D32F2F", "Family XIII"="#F44336", "Lachnospiraceae"="#E64A19", "Peptostreptococcaceae"="#FF5722", "Ruminococcaceae"="#FFCCBC",
"Enterobacteriaceae"="#4CAF50",
"Leuconostocaceae"="#FFEB3B",
"Veillonellaceae"="#673AB7") #生成行分组颜色的具名向量
left_anno <- rowAnnotation(Family = df_annotation[, 2],
show_annotation_name = F, #不展示行分组注释名
col = list(Family =left_col),
annotation_legend_param = list(
Family = list(at=c("Bacteroidaceae", "Marinifilaceae",
"Porphyromonadaceae", "Rikenellaceae",
"Tannerellaceae", "Campylobacteraceae",
"Family XI", "Family XIII", "Lachnospiraceae",
"Peptostreptococcaceae", "Ruminococcaceae",
"Enterobacteriaceae", "Leuconostocaceae",
"Veillonellaceae"))))
ht <- Heatmap(mat,
cluster_rows = F, #不按行聚类
show_column_names = F, #不展示列名
heatmap_legend_param = list(title = "Log2 relative abundance"), #设置热图图例名称
col = c("#FFFFFF", "#D32F2F"), #设置热图颜色
top_annotation = top_anno, #添加列注释
left_annotation = left_anno) #添加行注释
class <- unique(df_annotation$Class)
#绘制class单独的图例
lgd <- Legend(labels = class, title = "Class",
legend_gp = gpar(fill = c("#1976D2", "#F8BBD0", "#D32F2F", "#4CAF50", "#FFEB3B", "#673AB7")))
#将单独图例与其他部分合并
pdf("plot.pdf", width = 11, height = 10)
draw(ht, ht_gap = unit(7, "mm"), annotation_legend_list = lgd)
dev.off()
#> quartz_off_screen
#> 2