缘由

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

碎碎念

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

原图 & 文章

Liao C, Glodowski CR, Fan C, Liu J, Mott KR, Kaushik A, Vu H, Locasale JW, McBrayer SK, DeBerardinis RJ, Perou CM, Zhang Q. Integrated Metabolic Profiling and Transcriptional Analysis Reveals Therapeutic Modalities for Targeting Rapidly Proliferating Breast Cancers. Cancer Res. 2022 Feb 15;82(4):665-680. doi: 10.1158/0008-5472.CAN-21-2745. PMID: 34911787; PMCID: PMC8857046.

功能:同时展示了差异丰度图和热图。

炫图

下面对代码进行逐段解释,涵盖每一部分的关键功能和目的,以便发布时可以直接复制使用:


代码

library(ggplot2)
library(grid)
library(ggh4x)
library(aplot)
set.seed(123)

生成数据

pathway_ids <- 1:20
pathway_names <- paste("Pathway", pathway_ids)
 
score <- runif(20, min = -1, max = 1)
cluster1 <- sample(1:10, 20, replace = TRUE)
cluster2 <- sample(1:10, 20, replace = TRUE)
dected <- sample(1:15, 20, replace = TRUE)
 
pathway_category <- sample(c("Metabolism", "Signal Transduction", "Cell Cycle", "Gene Expression"), 
                           20, replace = TRUE)
 
draw <- data.frame(
    Pathway_ID = pathway_ids,
    Pathway_Name = pathway_names,
    score = score,
    Cluster1 = cluster1,
    Cluster2 = cluster2,
    Dected = dected,
    Pathway_Category = pathway_category
)
 
draw$type <- ifelse(draw$score > 0, "up", "down")
draw$Pathway_Category <- factor(draw$Pathway_Category)
draw <- draw  %>% reshape::melt(c("Pathway_ID", "Pathway_Name", "score", "Pathway_Category", "type")) %>%
    mutate(x = case_when(
        variable == "Cluster1" ~ -1,
        variable == "Cluster2" ~ 0,
        variable == "Dected" ~ 1
    ))
    
draw$Pathway_Name <- factor(draw$Pathway_Name,
                            levels = unique(draw$Pathway_Name[order(draw$score, decreasing = TRUE)]))

图形1:文本标注

p1 <- ggplot(data = draw, aes(score, Pathway_Name)) +
    geom_text(aes(label = Pathway_Name, color = Pathway_Category), x = 1, vjust = 0, hjust = 1) +
    scale_colour_brewer(palette = "Dark2", guide = "stringlegend") +
    theme_minimal() + theme(
        plot.margin = margin(2, 1, 2, 0, "cm"),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        panel.grid = element_blank()
    ) +
    guides(position = "none") + labs(x = NULL, y = NULL) +
    coord_cartesian(clip = "off") +
    annotation_custom(
        grob = grid::segmentsGrob(
            y0 = unit(1.05, "native"),
            y1 = unit(1.05, "native"),
            arrow = arrow(angle = 90, length = unit(.2, "cm"), ends = "both"),
            gp = grid::gpar(lwd = 3, col = "black")
        ),
        xmin = 0.5, xmax = 1
    ) +
    annotation_custom(
        grob = grid::textGrob(
            label = "Metabolite Pathways",
            gp = grid::gpar(col = "black", fontsize = 8),
            x = 0.75, y = 1.05 * 1.025
        )
    )
  • 文本绘制:
    • 每行显示 Pathway_Name,用颜色区分 Pathway_Category。
  • 注释:
    • 使用 annotation_custom 添加箭头和文本,标明上下方向。

图形2:柱状图

p2 <- ggplot(data = draw %>% distinct(Pathway_Name, .keep_all = TRUE),
             aes(score, Pathway_Name)) +
    geom_col(aes(fill = type)) +
    scale_fill_manual(values = c(down = "#00bfc4", up = "#f8766d")) +
    labs(x = NULL, y = NULL) +
    coord_cartesian(clip = "off") +
    annotation_custom(
        grob = grid::segmentsGrob(
            y0 = unit(1.05, "native"),
            y1 = unit(1.05, "native"),
            arrow = arrow(angle = 45, length = unit(.2, "cm"), ends = "first"),
            gp = grid::gpar(lwd = 3, col = "#00bfc4")
        ),
        xmin = 0, xmax = -1
    ) +
    annotation_custom(
        grob = grid::textGrob(
            label = "Cluster1",
            gp = grid::gpar(col = "#00bfc4", fontsize = 8, face = "bold"),
            x = 0.25, y = 1.05 * 1.025
        )
    ) +
    theme(
        panel.border = element_rect(color = "black", linewidth = 0.5),
        panel.grid = element_blank(),
        axis.text.y = element_blank(),
        legend.position = "none"
    )
  • 柱状图绘制:
    • 按照 score 绘制柱状图,颜色区分 up 和 down。

图形3:多变量文本

p3 <- ggplot(draw, aes(x, Pathway_Name)) +
    geom_text(aes(label = value, color = variable)) +
    labs(x = NULL, y = NULL) +
    scale_x_continuous(
        position = "top", breaks = c(-1, 0, 1), labels = c("Detected", "Cluster1", "Cluster2")
    ) +
    scale_color_manual(values = c("black", "#0186fe", "red")) +
 
    theme(
        axis.text.x = element_text(angle = 60, size = 8, face = "bold"),
        panel.grid = element_blank(),
        axis.ticks = element_blank()
    )
  • 显示多变量值:
    • 使用文本显示 Cluster1、Cluster2 和 Detected 的数值。

拼接图表

p4 <- p1 %>%
    insert_right(p2, width = 1.2) %>%
    insert_right(p3, width = 1.2)
 
print(p4)
  • 使用 aplot 将 p1、p2 和 p3 水平拼接成综合图。

此代码生成了一组直观的图形,结合文本、柱状图和多变量展示,为复杂数据提供了直观的表达方式。

ALL

#!/usr/env/ Rscript
 
library(ggplot2)
library(grid)
library(ggh4x)
library(aplot)
set.seed(123)
 
# 定义Pathway_ID和Pathway_Name
pathway_ids <- 1:20
pathway_names <- paste("Pathway", pathway_ids)
 
# 生成score(在-1到1之间)
score <- runif(20, min = -1, max = 1)
 
# 生成cluster1, cluster2, dected(正整数)
cluster1 <- sample(1:10, 20, replace = TRUE)
cluster2 <- sample(1:10, 20, replace = TRUE)
dected <- sample(1:15, 20, replace = TRUE)
 
# 生成pathway category列(随机选择几类)
pathway_category <- sample(c("Metabolism", "Signal Transduction", "Cell Cycle", "Gene Expression"), 
                           20, replace = TRUE)
 
# 创建数据框
draw <- data.frame(
    Pathway_ID = pathway_ids,
    Pathway_Name = pathway_names,
    score = score,
    Cluster1 = cluster1,
    Cluster2 = cluster2,
    Dected = dected,
    Pathway_Category = pathway_category
)
 
 
draw$type <- ifelse(draw$score>0, "up", "down")
draw$Pathway_Category <- factor(draw$Pathway_Category)
draw <- draw  %>% reshape::melt(c("Pathway_ID","Pathway_Name", "score", "Pathway_Category", "type")) %>%
    mutate(x=case_when(variable=="Cluster1"~-1,
                       variable=="Cluster2"~0,
                       variable=="Dected"~1))
 
draw$Pathway_Name <- factor(draw$Pathway_Name, 
                            levels = unique(draw$Pathway_Name[order(draw$score,decreasing =T)]) )
 
 
##part1 
 
p1 <- ggplot(data=draw, aes(score,Pathway_Name))+
    geom_text(aes(label=Pathway_Name, color=Pathway_Category), x=1, vjust=0, hjust=1) +
    scale_colour_brewer(palette = "Dark2", guide = "stringlegend")+
    theme_minimal()+theme(
        plot.margin = margin(2, 1, 2, 0, "cm"), # 增加外部边距,
        axis.text.x=element_blank(),
        axis.text.y=element_blank(),
        panel.grid = element_blank(),
    ) +
    guides(postion="none")+labs(x=NULL, y=NULL)+ 
    coord_cartesian(clip = "off") +
    # annotation_custom(
    #     grob = grid::segmentsGrob(
    #         x0 = unit(c(0, 0, 0.2), "native"),
    #         x1 = unit(c(0.2, 0, 0.2), "native"),
    #         y0 = unit(c(1.05, 1.05, 1.05), "native"),
    #         y1 = unit(c(1.05, 1, 1), "native"),
    #         gp = gpar(col = "black", lwd = 2)
    #     )
    # ) +
    annotation_custom(
        grob = grid::segmentsGrob(
            y0 = unit(1.05, "native"),        # 起点的 y 坐标
            y1 = unit(1.05, "native"),        # 终点的 y 坐标
            arrow = arrow(
                angle = 90, length = unit(.2, "cm"),  ends = "both"),
            gp = grid::gpar(lwd = 3, col = "black")
        ),
        xmin = 0.5, xmax = 1,  
    )+
    annotation_custom(
        grob = grid::textGrob(
            label = "Metabolite Pathways",
            gp = grid::gpar(col = "black", fontsize =8),
            x=0.75, y=1.05*1.025)
    )
 
p2 <- ggplot(data=draw %>% distinct(Pathway_Name, .keep_all = T),
             aes(score,Pathway_Name))+geom_col(aes(fill=type))+
    scale_fill_manual(values=c(down="#00bfc4", up="#f8766d"))+labs(x=NULL, y=NULL)+
    coord_cartesian(clip = "off")+ annotation_custom(
        grob = grid::segmentsGrob(
            y0 = unit(1.05, "native"),        # 起点的 y 坐标
            y1 = unit(1.05, "native"),        # 终点的 y 坐标
            arrow = arrow(
                angle = 45, length = unit(.2, "cm"), ends = "first"),           # 箭头出现在起点),
            gp = grid::gpar(lwd = 3, col = "#00bfc4" )
            ),
        xmin = 0,  xmax = -1
        )+
    annotation_custom(
        grob = grid::segmentsGrob(
            y0 = unit(1.05, "native"),        # 起点的 y 坐标
            y1 = unit(1.05, "native"),
            arrow = arrow(
                angle =45, length = unit(.2, "cm"), ends = "last"            # 箭头出现在起点
            ),
            gp = grid::gpar(lwd = 3, col = "#f8766d"  )
        ),
        xmin = 0, xmax = 1,# 终点的 y 坐标
    )+
    annotation_custom(
        grob = grid::textGrob(
            label = "Cluster1",
            gp = grid::gpar(col = "#00bfc4", fontsize =8,face="bold"),
            x=0.25, y=1.05*1.025
        )
    )+ annotation_custom(
        grob = grid::textGrob(
            label = "Cluster2",
            gp = grid::gpar(col = "#f8766d", fontsize =8,face="bold"),
            x=0.75, y=1.05*1.025,
        )
    )+theme( panel.border = element_rect(color="black", linewidth =0.5),
                        panel.grid = element_blank(),
                        axis.ticks.x=element_blank(),
                        axis.text.x=element_text(face="bold"),
                        axis.text.y = element_blank(),
                        legend.position = "none"
    )+ theme(plot.margin = margin(2, 1, 2, 0, "cm"))+
    guides(postion="none")
 
p3 <- ggplot(draw, aes(x, Pathway_Name)) +
    geom_text(aes(label = value, color = variable)) +labs(x=NULL, y=NULL)+xlim(-1,1.5)+
    scale_x_continuous(
        position = "top", breaks = c(-1, 0, 1), labels = c("Detected", "Cluster1", "Cluster2") 
    ) +
    
    scale_color_manual(values = c("black", "#0186fe", "red")) +
    theme(
        panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.ticks = element_blank(),
        axis.line.x = element_line(color = "black"),  # Adds red line to the top axis
        axis.text.x = element_text(angle=60, hjust=-0,vjust=0, size=8, face = "bold"),
        axis.text.y = element_blank(),
        #plot.margin = margin(0.5, -1, 1, 1, "cm"), 
        legend.position = "none"
    ) + geom_hline(yintercept = 0.45, color = "black", size = 0.5) 
 
#拼图
 
p4 <- p1 %>% 
    insert_right(p2, width = 1.2) %>%  # 将 p2 放在 p1 的右边
    insert_right(p3, width = 1.2)     # 将 p3 放在右边(再插入一次)
 
print(p4)