By Gengxin, 31 January, 2026
Forums

  1. 准备文件

样本信息表、甲基化生成的merged.mCcatero.txt文件

  1. 运行代码
library(dplyr)
library(ggplot2)
library(ggsci)
library(RColorBrewer)
# 读取数据并命名列
sample_info_tbl <- read.delim("D:/槜李/重新编号数据及图表/差异甲基化/sample_info.txt", comment.char="#")
meth_stat <- read.delim(
    "D:/槜李/结果_图/merged.mCcatero.txt",
    header = FALSE,
    col.names = c('case', 'value', 'sample')
) %>%
    mutate(case = if_else(case == "CG", "CpG", case))  # 修改 'CG' 为 'CpG'

# 处理数据并绘图
meth_stat %>%
    filter(case %in% c('mCG', 'mCHG', 'mCHH')) %>%
    left_join(sample_info_tbl, by = "sample") %>%
    ggplot(aes(x = group, y = value, color = group)) +
    geom_segment(aes(x = group, xend = group, y = 0, yend = value), show.legend = FALSE) +
    geom_point(size = 4, show.legend = FALSE) +
    scale_color_brewer(palette = "Set2") +  # 核心:更换为Set2配色
    labs(x = NULL, y = 'Methylation Levels') +
    facet_wrap(~case, scales = "free") +
    theme_bw() +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

sample_info_tbl输入之后的格式

  1. 结果图