欢迎关注R语言数据分析指南
❝最近VIP群内有朋友询问火山图的绘制方法,那么本节就来详细介绍在R中如何使用「ggplot2绘制火山图」,小编添加了详细的注释希望各位观众老爷能够喜欢。 ❞
小编已经搭建了一套稳定的真核转录组分析流程,可以完成「从原始数据分析到最终出结果分析文档」基本包含目前RNA_seq文章的所有分析内容。「有数据分析需求的朋友可联系小编进行咨询」
导入数据
代码语言:javascript复制df <- read.delim("data.xls", header = TRUE, sep = "t")
数据清洗
代码语言:javascript复制plot_data <- df %>%
janitor::clean_names() %>% # 使用 janitor 包的 clean_names() 函数将列名转换为小写
mutate(fold_change = 2^log_fc) %>% # 计算折叠变化值 fold_change = 2 的 log_fc 次幂
select(entrezid, 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"
)
)
代码语言:javascript复制plot_data %>% count(gene_type) # 统计 plot_data 数据框中各个 gene_type 出现的频数
筛选需要展示的基因
代码语言:javascript复制sig_genes <- plot_data %>% filter(symbol %in% c("Il15", "Il34", "Slc22a3"))
up_genes <- plot_data %>% filter(symbol == "Slc22a3")
down_genes <- plot_data %>% filter(symbol %in% c("Il15", "Il34"))
数据可视化
代码语言:javascript复制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 = 0.6, shape = 16, size = 1)
# 从 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)
# 设置 gene_type 对应的颜色映射
scale_color_manual(values = c("up" = "#ffad73", "down" = "#26b3ff", "ns" = "grey"),
labels = c('down 1245', 'ns 12578', "up 981"))
# 设置 x 轴的刻度和范围
scale_x_continuous(breaks = c(seq(-10, 10, 2)), limits = c(-10, 10))
# 设置 x 轴和 y 轴的标签
labs(x = "log2(fold change)", y = "-log10(adjusted P-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.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) # 设置图例位置为右上角
)