多组火山图
数据准备:
将CSV文件同一在一个路径下,用代码合并
确保文件列名正确
library(fs)
library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)
library(ggfun)
library(ggrepel)# 获取文件列表
file_paths <- dir_ls(path = "E:\\proteomic\\fuxian_bacteria\\multivolcano", pattern = "_DEG\\.csv$", full.names = T)
# 检查文件列表
print(file_paths)# 读取和处理数据
all_deg <- map(file_paths, function(x){# 检查文件扩展名,忽略大小写if (str_ends(str_to_lower(x), ".csv")) {cat("Processing file:", x, "\n") # 打印正在处理的文件名data <- read_delim(file = x, col_names = T, delim = ",")if (is.data.frame(data)) {group <- str_remove(basename(x), pattern = "_DEG.*")return(dplyr::mutate(data, group = group))} else {cat("Failed to read file:", x, "\n")return(NULL)}} else {cat("Skipping non-CSV file:", x, "\n")return(NULL)}
}) %>%compact() %>% # 移除列表中的 NULL 元素do.call(rbind, .)ggplot(data = all_deg) +geom_jitter(data = all_deg %>% dplyr::filter(change == "Normal"),aes(x = group, y = logFC, color = change, size = abs(logFC), alpha = abs(logFC)),width = 0.4) +geom_jitter(data = all_deg %>% dplyr::filter(change != "Normal"),aes(x = group, y = logFC, color = change, size = abs(logFC), alpha = abs(logFC)),width = 0.4) +geom_jitter(data = all_deg %>% dplyr::group_by(group) %>%dplyr::arrange(desc(abs(logFC))) %>%dplyr::slice_head(n = 15) %>%dplyr::ungroup() %>%na.omit(),aes(x = group, y = logFC, size = abs(logFC)),width = 0.4, shape = 21, fill = "#e77381") +geom_text_repel(data = all_deg %>% dplyr::group_by(group) %>%dplyr::arrange(desc(abs(logFC))) %>%dplyr::slice_head(n = 15) %>%dplyr::ungroup() %>%na.omit(),aes(x = group, y = logFC, label = SYMBOL)) +geom_tile(aes(x = group, y = 0, fill = group), height = 0.4) +geom_text(data = all_deg %>% dplyr::select(group) %>% dplyr::distinct(group, .keep_all = T),aes(x = group, y = 0, label = group), size = 6) +geom_hline(yintercept = c(-log2(1.5), log2(1.5))) +scale_y_continuous(limits = c(-5, 5)) +scale_size(range = c(1, 10)) +scale_alpha(range = c(0.1, 1)) +scale_color_manual(values = c("Up" = "#f46d43", "Normal" = "#bdbdbd", "Down" = "#3288bd")) +scale_fill_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3')) +theme_bw() +theme(axis.text = element_text(color = "#000000", size = 12),axis.title = element_text(color = "#000000", size = 15),panel.grid = element_blank(),legend.background = element_roundrect(color = "#969696")) +labs(y = "log2(Fold Change)")