- 准备文件
差异甲基化分析的ZL_2_vs_ZL_1.dmr文件,将几个不同的文件合并在一起
- 脚本
library(dplyr)
library(ggplot2)
dmr_result <- read.delim("D:/槜李/结果_图/差异甲基化数目/merged.dmr") %>%
mutate(direction =
if_else(qvalue > 0.05 | abs(meth.diff) < 20 , 'NS',
if_else(meth.diff >= 20, 'hyper', 'hypo'))) %>%
mutate(direction =
factor(direction, levels = c("hyper", "hypo", "NS"))) %>%
mutate(context =
factor(context, levels = c("CpG", "CHG", "CHH")))
dmr_count = filter(dmr_result, direction != 'NS') %>%
group_by(sampleA, sampleB, context, direction) %>%
summarise(count = n()) %>%
arrange(desc(direction)) %>%
mutate(cumsum = cumsum(count))
# 加载必要的包
library(ggplot2)
# 完整绘图代码(包含手动浅色系配置)
ggplot(data = dmr_count, aes(x = sampleA, y = count)) +
geom_col(aes(fill = direction),
width = 0.7, color = "white") +
geom_text(aes(y = cumsum - 0.5 * count, label = count),
color = "white", size = 3, angle = 90) +
labs(x = NULL, y = 'Num of DMR', fill = NULL) +
facet_wrap(~context, scales = "free") +
theme_bw() +
theme(strip.background = element_blank()) + # 注意这一行末尾是+号,不是换行
# 以下是新增的手动浅色系配置(直接加在最后一个theme之后)
scale_fill_manual(values = c(
"hyper" = "#FFB6C1", # 浅粉色(对应hyper)
"hypo" = "#87CEEB", # 浅蓝色(对应hypo)
# "NS" = "#F0E68C" # 浅黄色(对应NS,不需要可删除这一行)
))- 火山图
library(cowplot)
filtered_data <- filter(dmr_result, sampleA == 'ZL_5', sampleB == 'ZL_1')
p <- ggplot(filtered_data, aes(x = meth.diff, y = -log10(qvalue))) +
geom_point(aes(color = direction), size = 1.5, alpha = 0.8, show.legend = F) +
geom_hline(yintercept = -log10(0.05), linetype = "dotdash", color = "grey30") +
geom_vline(xintercept = c(-25, 25), linetype = "dotdash", color = "grey30") +
labs(x = 'Methylation Difference', y = '-log10(qvalue)') +
# 关键:替换为浅色系(hyper浅粉/hypo浅蓝/NS浅灰)
scale_color_manual(values = c(
"hyper" = "#FFC0CB", # 浅粉色(替代原红色)
"hypo" = "#B0E0E6", # 浅青色/浅蓝色(替代原深蓝色)
"NS" = "#DCDCDC" # 浅灰色(替代原深灰色)
)) +
facet_wrap(~context, scales = "free") +
theme_cowplot() +
theme(strip.background = element_rect(fill = NULL))
# 强制输出图表
print(p)画火山图的时候在自己电脑上RStudio上画的,由于数据量比较大,需要等几分钟才出图
- 结果