缘由

生信之路,道阻且长,记录经验,但渡有缘人。

碎碎念

及时当勉励,岁月不饶人 ——陶渊明

原图 & 文章

Zhi Feng, Kaifang Fan, Zhi Yao, Hui Wang, Xiuzhong Wu, Longping Tang, Qiye Wang, Yuanqing Wang, Yiqiang Wang, Meng Li, Integrative metabolome and transcriptome analysis reveals GbKCS and GbMYB involved in the biosynthesis of ginkgolic acids, Industrial Crops and Products,Volume 220, 2024,

功能:同时展示了差异丰度图和通路信息。

炫图

代码

1. 加载必要的 R 包

library(ggplot2)
library(reshape2)
library(dplyr)
library(patchwork)

2. 数据生成与预处理

set.seed(42)
 
# 生成代谢物名称和分类
metabolites <- paste0("metabolite_", 1:20)
categories <- sample(c("category_a", "category_b", "category_c", 
                       "category_d", "category_e", "category_f", 
                       "category_g", "category_h"), 20, replace = TRUE)
samples <- c("sample_1", "sample_2", "sample_3")
 
# 随机生成表达水平数据
expression_levels <- matrix(sample(1000:10000, 60, replace = TRUE), nrow = 20, ncol = 3)
colnames(expression_levels) <- samples
rownames(expression_levels) <- metabolites

3. 数据标准化

# 对数据进行行 Z-score 标准化
zscore <- function(x) {
    (x - mean(x)) / sd(x)
}
expression_levels <- t(apply(expression_levels, 1, zscore))

4. 数据框格式转换

data <- as.data.frame(expression_levels)
data$metabolite <- metabolites
data$category <- categories
 
# 将宽格式数据转换为长格式
data$category <- factor(data$category, levels = unique(categories))
long_data <- melt(data, id.vars = c("metabolite", "category"), variable.name = "sample", value.name = "expression")
long_data  <- long_data %>% arrange(category)
long_data$metabolite <- factor(long_data$metabolite, levels = unique(long_data$metabolite))

5. 绘制表达水平热图

p1 <- ggplot(long_data, aes(x = sample, y = metabolite, fill = expression)) +
    geom_tile() +
    scale_fill_gradientn(colors=c(low = "#15883c",  mid="white", high = "#b32478")) +
    scale_y_discrete(position="right", expand=c(0,0))+
    scale_x_discrete(expand=c(0,0))+
    theme_minimal() +
    labs(title = NULL, x = NULL, y = NULL) +
    theme(
        axis.text.y = element_text(size = 10),
        axis.text.x = element_text(angle = -45, hjust = 1, vjust = 0, size = 10),
        axis.ticks = element_blank()
    )
  • geom_tile:绘制矩形热图单元格。
  • scale_fill_gradientn:定义颜色渐变范围,绿色表示低表达,白色表示中等,紫色表示高表达。
  • theme_minimal:简化主题样式,突出数据。

6. 绘制分类标注

p2 <- ggplot(long_data, aes(x = 0, y = category, fill = category)) +
    geom_tile(show.legend = FALSE)+
    scale_x_discrete(expand = c(0, 0)) +
    scale_y_discrete(expand = c(0, 0)) +
    theme(
        panel.background = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_blank(),
        plot.margin = margin(0, -0.05, 0, 0, "cm"),
        strip.text.y = element_text(
            angle = 0, size = 10, color = "black",
            hjust = 0,
            margin = margin(b = 5, t = 5, l = 5, r = 5)
        ),
        strip.background = element_rect(colour = NULL, fill = 'white'),
        panel.spacing = unit(0.5, "mm")
    ) +
    scale_fill_manual(values = colorRampPalette(RColorBrewer::brewer.pal(8, "Set2"))(8))+labs(x=NULL,y=NULL)
  • geom_tile:绘制分类标注的矩形条。
  • scale_fill_manual:使用 8 种对比明显的颜色表示不同分类。
  • theme:调整布局,隐藏多余的坐标轴和背景。

7. 合并热图与分类标注

p2 + p1 + plot_layout(ncol = 2, widths = c(0.05, 2))

ALL

# 加载必要的包
library(ggplot2)
library(reshape2)
library(dplyr)
library(patchwork)

# 设置随机种子
set.seed(42)

# 生成代谢物数据
metabolites <- paste0("metabolite_", 1:20)
categories <- sample(c("category_a", "category_b", "category_c", 
                       "category_d", "category_e", "category_f", 
                       "category_g", "category_h"), 20, replace = TRUE)
samples <- c("sample_1", "sample_2", "sample_3")

# 生成表达水平数据
expression_levels <- matrix(sample(1000:10000, 60, replace = TRUE), nrow = 20, ncol = 3)
colnames(expression_levels) <- samples
rownames(expression_levels) <- metabolites

# 对数据进行行 Z-score 标准化
zscore <- function(x) {
    (x - mean(x)) / sd(x)
}
expression_levels <- t(apply(expression_levels, 1, zscore))

# 转换为长格式数据框
data <- as.data.frame(expression_levels)
data$metabolite <- metabolites
data$category <- categories

# 提前将因子处理
data$category <- factor(data$category, levels = unique(categories))
long_data <- melt(data, id.vars = c("metabolite", "category"), variable.name = "sample", value.name = "expression")
long_data  <- long_data %>% arrange(category)
long_data$metabolite <- factor(long_data$metabolite, levels = unique(long_data$metabolite))


# 绘制热图
p1 <- ggplot(long_data, aes(x = sample, y = metabolite, fill = expression)) +
    geom_tile() +
    scale_fill_gradientn(colors=c(low = "#15883c",  mid="white", high = "#b32478")) +
    scale_y_discrete(position="right", expand=c(0,0))+
    scale_x_discrete(expand=c(0,0))+
    theme_minimal() +
    labs(title = NULL, x = NULL, y = NULL) +
    theme(
        axis.text.y = element_text(size = 10),
        axis.text.x = element_text(angle = -45, hjust = 1, vjust = 0,size = 10),
        axis.ticks = element_blank()
    )

# 绘制分类标注
p2 <- ggplot(long_data, aes(x=0, y = category, fill = category)) +
    geom_tile(show.legend = FALSE)+
    scale_x_discrete(expand = c(0, 0)) +
    scale_y_discrete(expand = c(0, 0)) +
    theme(
        panel.background = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_blank(),
        plot.margin = margin(0, -0.05, 0, 0, "cm"),
        strip.text.y = element_text(
            angle = 0, size = 10, color = "black",
            hjust = 0,
            margin = margin(b = 5, t = 5, l = 5, r = 5)
        ),
        strip.background = element_rect(colour = NULL, fill = 'white'),
        panel.spacing = unit(0.5, "mm")
    ) +
    scale_fill_manual(values = colorRampPalette(RColorBrewer::brewer.pal(8, "Set2"))(8))+labs(x=NULL,y=NULL)


p2 + p1+ plot_layout(ncol = 2, widths = c( 0.05,2))