1. R包的调用及工作路径设置
library(janitor)
library(dplyr)
library(ggplot2)
library(ggrepel)
setwd("E:/R/demo/火山图")
2. 数据导入及数据清洗
df <- read.csv("DEG.csv") #导入DEG文件,必须列为GeneID、pvalue、adj_p_val、logfc等
plot_data <- df %>%
janitor::clean_names() %>% # 使用 janitor 包的 clean_names() 函数将列名转换为小写
mutate(fold_change = 2^log_fc) %>% # 计算折叠变化值 fold_change = 2 的 log_fc 次幂
select(symbol, fold_change, adj_p_val) %>% # 根据自己需要保留指定的列
mutate(
gene_type = case_when(
fold_change >= 2 & adj_p_val <= 0.05 ~ "up", # 当 fold_change 大于等于 2 且 adj_p_val 小于等于 0.05 时,gene_type 设置为 "up"
fold_change <= 0.5 & adj_p_val <= 0.05 ~ "down", # 当 fold_change 小于等于 0.5 且 adj_p_val 小于等于 0.05 时,gene_type 设置为 "down"
TRUE ~ "ns" # 其他情况下,gene_type 设置为 "ns"
)
)
3. 统计不同变化的频数并挑选拟标注的显著基因
plot_data %>% count(gene_type) # 统计 plot_data 数据框中各个 gene_type 出现的频数
gene_type n
1 down 449
2 ns 1309
3 up 476
sig_genes <- plot_data %>% filter(symbol %in% c("RS11985", "RS0790", "RS505170","RS18965","H7R77_RS12285","RS13450"))
up_genes <- plot_data %>% filter(symbol == "H7R77_RS11985")
down_genes <- plot_data %>% filter(symbol %in% c("RS12285","RS13450"))
4. 绘制火山图
plot_data %>%
ggplot(aes(x = log2(fold_change), y = -log10(adj_p_val))) +
# 绘制基础散点图,并根据 gene_type 对点的颜色进行分类,设置点的透明度 (alpha=0.6),形状 (shape = 16),大小 (size = 1)
geom_point(aes(color = gene_type), alpha = 1, shape = 19, size = 1.2) +
# 从 up_genes 数据框中绘制特定形状的散点图,填充颜色为红色,边框颜色为黑色,大小为 2
geom_point(data = up_genes, shape = 21, size = 2, fill = "red", colour = "black") +
# 从 down_genes 数据框中绘制特定形状的散点图,填充颜色为钢蓝色,边框颜色为黑色,大小为 2
geom_point(data = down_genes, shape = 21, size = 2, fill = "steelblue", colour = "black") +
# 添加水平虚线,y 轴截距为 -log10(0.05),表示显著性阈值为 0.05
geom_hline(yintercept = -log10(0.05), linetype = "dashed") +
# 添加垂直虚线,x 轴截距为 log2(0.5) 和 log2(2),表示折叠变化范围为 0.5 到 2
geom_vline(xintercept = c(log2(0.5),log2(2)), linetype = "dashed") +
# 在图中显示 sig_genes 数据框中基因符号的标签
#geom_label_repel(data = sig_genes, aes(label = symbol), force = 2, nudge_y = 1)
geom_label_repel(data = up_genes, aes(label = symbol), force = 2, nudge_y = 1)+
# 设置 gene_type 对应的颜色映射
scale_color_manual(values = c("up" = "#FC4E07", "down" = "#00AFBB", "ns" = "grey"),
labels = c('Down 1245', 'Ns 12578', "Up 981")) +
# 设置 x 轴的刻度和范围
scale_x_continuous(breaks = c(seq(-6, 6, 2)), limits = c(-8, 8)) +
# 设置 x 轴和 y 轴的标签
labs(x = "log2FC", y = "-log10(Q-value)", colour = "Expression change") +
# 调整图例外观,将图例大小设为 5,位置设置为右上角
guides(color = guide_legend(override.aes = list(size = 5))) +
theme_bw() + # # 设置图的主题为白色背景
# 设置图的主题样式,包括边框、网格线、背景等
theme(panel.border = element_rect(colour = "black", fill = NA, size = 0.5),#外边框参数
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.background = element_blank(),
axis.title = element_text(face = "bold", color = "black", size = 10),
axis.text = element_text(color = "black", size = 9, face = "bold"),
legend.background = element_blank(),
legend.title = element_text(face = "bold", color = "black", size = 10),
legend.text = element_text(face = "bold", color = "black", size = 9),
legend.spacing.x = unit(0, "cm"),
legend.position = c(0.88, 0.89) # 设置图例位置为右上角
)
火山图