跟着 Nature Genetics 学绘图,从单细胞转录组对象出发,观察疾病相关的细胞组成变化。
红色代表 Diseases 富集,紫色代表 Donor 富集;左图白色、右图灰色表示未达到显著性阈值。
UMAP 画完了,疾病组和供体组的细胞看起来分布不同,但哪些细胞状态的相对丰度发生了变化?这一期,我们用 R、Seurat 和 Milo,把成纤维细胞的分析结果画成“邻域网络图+蜂群图”。先看懂颜色和散点,再拆解代码,最后把整套流程迁移到自己的数据。
今天开始一个新系列:《论文图鉴》。每期拆解一张论文图,读懂它,再用自己的数据画出来。
这期适合已有 Seurat 对象、细胞类型注释和不同处理组数据的读者。最终输出一张双联图,保存为 PNG 和 PDF;完整代码见文末。
01 论文里的图为什么值得学
比较疾病组和正常组的细胞比例,传统做法是先聚类、再数每个簇里两组各有多少细胞。但这样有个盲区:簇内部的变化会被平均掉。比如成纤维细胞整体比例没变,其实可能是靠近炎症信号的那一部分在疾病中变多了,另一部分稳态细胞变少了,一多一少刚好抵消。
Milo 的思路是不再以整个簇为单位,而是在细胞的 k 近邻图上随机选一批索引细胞,每个索引细胞连同它最近的 k 个邻居构成一个“邻域”,也就是一个局部细胞状态。邻域之间可以重叠,数量通常有几千个,就像用许多小圆盘覆盖整个细胞状态空间。随后统计每个样本在各邻域里的细胞数,用负二项 GLM 对每个邻域分别检验疾病与正常的差异,得到 logFC 和校正后的 p 值。
本期绘图灵感来自 Nature Genetics 论文 A human single-cell atlas identifies OLR1+ scar-associated macrophages as a potential therapeutic target for chronic liver disease 的 Figure 2d[1]。原文用 Milo 邻域图与蜂群图展示健康和慢性肝病样本间的细胞组成差异。
来源:Papachristoforou 等,Nature Genetics,2026,DOI 10.1038/s41588-026-02774-w
图片来自论文原文:Papachristoforou E, et al. A human single-cell atlas identifies OLR1+ scar-associated macrophages as a potential therapeutic target for chronic liver disease. Nature Genetics 58, 2550–2565 (2026),CC BY 4.0;已从论文 Figure 2 中截取 d 面板、缩放并转换为 WebP 格式
把两张图放在一起的价值很直接:左图定位发生变化的局部细胞状态,右图按亚群汇总这些邻域的变化方向。它们展示的是同一份差异丰度结果,而不是两次互不相关的分析。原论文研究肝脏单核吞噬细胞,比较健康与慢性肝病样本;本短文提取本地对象中的成纤维细胞,比较 Donor 与合并后的 Diseases。常规 UMAP 可以展示细胞分布,Milo 则进一步结合样本信息进行差异丰度检验。
02 左图先看颜色 再看连线
一个点不是一个细胞,而是一个“邻域”:表达特征相近的一组细胞。邻域之间可以共享细胞,因此有些点会被线连接起来。
圆点越大,邻域包含的细胞越多;连线越粗,两个邻域共享的细胞越多。这里的线不是细胞通讯证据,UMAP 位置也不是组织的真实空间坐标。红色表示 Diseases 中显著富集,紫色表示 Donor 中显著富集,白色表示未达到阈值。图中的红色区域提示某些局部细胞亚群在疾病组占比更高,上方紫色区域则部分细胞亚群在对照组占比更高。
03 右图读方向 也读亚群内部差异
彩色点代表着通过 SpatialFDR < 0.05 阈值的邻域,黑线为该亚群邻域 logFC 的均值。
先找零点:左侧负值偏向 Diseases,右侧正值偏向 Donor。再看颜色:彩色点表示显著邻域,灰点表示当前分析未达到显著性阈值。灰色不等于两组完全一样。每一行上下展开,只是让散点更容易看清,不代表另一个生物学指标。黑色短线是均值,不是亚群整体的显著性标记。邻域会重叠,因此散点数也不等于独立样本数。如果一行两边都有彩色点,即使均值接近零,也可能存在真实的内部异质性。只看均值,容易漏掉这些方向相反的局部变化。
04 这次的结果重点看哪些亚群
- Fibroblast_0:零点两侧都有彩色邻域,均值接近零,提示亚群内部存在双向变化。
- Fibroblast_1:许多红点集中在负值区,说明较多邻域在 Diseases 中相对富集。
- Fibroblast_2:正值区有较多紫点,说明较多邻域在 Donor 中相对富集。
- Fibroblast_3:中间以灰点为主,两侧有彩色点;部分邻域发生变化,但整体方向不突出。
- Fibroblast_4:红点集中、整体偏负,疾病富集方向较一致。
- Fibroblast_5:邻域点较少,多数为灰色,差异证据有限。
- Fibroblast_6:图上基本为灰色,当前分析未见明显显著差异。
Fibroblast_1 和 Fibroblast_4 值得优先做后续分析。可以检查它们的标志基因、功能特征,以及各患者的亚群比例,判断这种差异是否在多个患者中都能观察到。这些标签目前只是聚类编号,不是已经确认的功能亚型。仅凭本图,不能把它们命名为“促纤维化细胞”,也不能推断疾病因果机制。
05 完整 R 示例代码
#首次使用时安装依赖
#install.packages(c("Seurat","harmony","ggplot2","ggbeeswarm","patchwork","BioManager"))
#BioManager::install(c("miloR","SingleCellExperiment"))
#加载所需的包
library(Seurat)
library(harmony)
library(miloR)
library(SingleCellExperiment)
library(ggplot2)
library(ggbeeswarm)
library(patchwork)
#设置路径和参数
infile <- "./GSE226314_global.rds"
outdir <- "./"
sample_col <- "PatientID"
batch_col <- "orig.ident"
subtype_col <- NULL
dir.create(outdir, recursive = TRUE, showWarnings = FALSE)
set.seed(123)
#读取数据并创建Conditions和PatientID
obj <- readRDS(infile)
obj$Conditions <- factor(ifelse(obj$condition == "Donor", "Donor", "Diseases"), levels = c("Diseases", "Donor"))
obj$PatientID <- sub("-(R|NR)-(pre|post)$", "", as.character(obj$orig.ident))
#提取成纤维细胞并整理RNA层
Fibroblast <- subset(obj, subset = cell.type == "Fibroblast")
DefaultAssay(Fibroblast) <- "RNA"
if (inherits(Fibroblast[["RNA"]], "Assay5")) Fibroblast <- JoinLayers(Fibroblast, assay = "RNA")
Fibroblast$SampleID <- as.character(Fibroblast@meta.data[[sample_col]])
#标准化、PCA和Harmony去批次
Fibroblast <- NormalizeData(Fibroblast, verbose = FALSE)
Fibroblast <- FindVariableFeatures(Fibroblast, nfeatures = 3000, verbose = FALSE)
Fibroblast <- ScaleData(Fibroblast, features = VariableFeatures(Fibroblast), verbose = FALSE)
Fibroblast <- RunPCA(Fibroblast, features = VariableFeatures(Fibroblast), npcs = 30, verbose = FALSE)
Fibroblast <- RunHarmony(Fibroblast, group.by.vars = batch_col, reduction.use = "pca", dims.use = 1:30, verbose = FALSE)
#UMAP降维和成纤维细胞亚群聚类
Fibroblast <- RunUMAP(Fibroblast, reduction = "harmony", dims = 1:30, seed.use = 123, verbose = FALSE)
Fibroblast <- FindNeighbors(Fibroblast, reduction = "harmony", dims = 1:30, verbose = FALSE)
Fibroblast <- FindClusters(Fibroblast, resolution = 0.3, random.seed = 123, verbose = FALSE)
Fibroblast$Fibroblast_subtype <- if (is.null(subtype_col)) paste0("Fibroblast_", Fibroblast$seurat_clusters) else as.character(Fibroblast@meta.data[[subtype_col]])
subtypes <- if (is.null(subtype_col)) paste0("Fibroblast_", sort(unique(as.integer(as.character(Fibroblast$seurat_clusters))))) else unique(Fibroblast$Fibroblast_subtype)
#构建Milo对象,使用Harmony空间进行邻域分析
sce <- SingleCellExperiment(assays = list(counts = GetAssayData(Fibroblast, assay = "RNA", layer = "counts")), colData = S4Vectors::DataFrame(Fibroblast@meta.data))
reducedDim(sce, "PCA") <- Embeddings(Fibroblast, "harmony")[, 1:30]
reducedDim(sce, "UMAP") <- Embeddings(Fibroblast, "umap")
milo <- Milo(sce)
milo <- buildGraph(milo, k = 30, d = 30)
milo <- makeNhoods(milo, prop = 0.1, k = 30, d = 30, refined = TRUE)
milo <- countCells(milo, meta.data = as.data.frame(colData(milo)), samples = "SampleID")
milo <- calcNhoodDistance(milo, d = 30)
#建立患者级设计矩阵,同一患者pre/post合并计数
design <- unique(Fibroblast@meta.data[, c("SampleID", "Conditions")])
stopifnot(!anyDuplicated(design$SampleID), all(table(design$Conditions) >= 2))
rownames(design) <- design$SampleID
design <- design[colnames(nhoodCounts(milo)), , drop = FALSE]
#差异丰度分析,正logFC为Donor富集,负logFC为Diseases富集
da <- testNhoods(milo, design = ~ Conditions, design.df = design)
da <- annotateNhoods(milo, da, coldata_col = "Fibroblast_subtype")
da$Fibroblast_subtype <- factor(da$Fibroblast_subtype, levels = rev(subtypes))
da$plotFC <- ifelse(!is.na(da$SpatialFDR) & da$SpatialFDR < 0.05, da$logFC, NA_real_)
#设置接近原图的红白紫配色和统一色标
red <- "#8E2630"
purple <- "#8276B1"
fc_lim <- max(2, ceiling(max(abs(da$logFC), na.rm = TRUE)))
fc_scale <- function(na_color) scale_fill_gradient2(name = "log(FC)", low = red, mid = "#F4F1EF", high = purple, midpoint = 0, limits = c(-fc_lim, fc_lim), na.value = na_color, breaks = c(-fc_lim, 0, fc_lim))
#提取邻域坐标、细胞数和邻域间共享细胞数
nh <- nhoods(milo)
idx <- vapply(nhoodIndex(milo), function(x) as.integer(x[1]), integer(1))
xy <- reducedDim(milo, "UMAP")[idx, , drop = FALSE]
nodes <- data.frame(x = xy[, 1], y = xy[, 2], size = as.numeric(Matrix::colSums(nh)), plotFC = da$plotFC[match(seq_len(ncol(nh)), as.integer(as.character(da$Nhood)))])
overlap <- as.data.frame(summary(Matrix::triu(Matrix::crossprod(nh), k = 1)))
edges <- data.frame(x = nodes$x[overlap$i], y = nodes$y[overlap$i], xend = nodes$x[overlap$j], yend = nodes$y[overlap$j], overlap = overlap$x)
#设置左图UMAP箭头位置
xr <- range(nodes$x)
yr <- range(nodes$y)
dx <- diff(xr)
dy <- diff(yr)
ax <- xr[1] - 0.06 * dx
ay <- yr[1] - 0.10 * dy
#左图:Milo邻域差异丰度网络
p1 <- ggplot() +
geom_segment(data = edges, aes(x = x, y = y, xend = xend, yend = yend, linewidth = overlap), colour = "#C8C8C8", alpha = 0.22) +
geom_point(data = nodes, aes(x = x, y = y, size = size, fill = plotFC), shape = 21, colour = "#333333", stroke = 0.2) +
fc_scale("white") +
scale_size_continuous(name = "Nhood\nsize", range = c(0.5, 3.2)) +
scale_linewidth_continuous(name = "Overlap\nsize", range = c(0.08, 0.7)) +
annotate("segment", x = ax, xend = ax + 0.24 * dx, y = ay, yend = ay, linewidth = 0.6, arrow = grid::arrow(length = grid::unit(0.12, "inches"), type = "closed")) +
annotate("segment", x = ax, xend = ax, y = ay, yend = ay + 0.24 * dy, linewidth = 0.6, arrow = grid::arrow(length = grid::unit(0.12, "inches"), type = "closed")) +
annotate("text", x = ax + 0.12 * dx, y = ay - 0.055 * dy, label = "UMAP 1", size = 3.6) +
annotate("text", x = ax - 0.065 * dx, y = ay + 0.12 * dy, label = "UMAP 2", angle = 90, size = 3.6) +
coord_equal(clip = "off") +
labs(title = "Milo differential abundance") +
guides(fill = guide_colorbar(order = 1), size = guide_legend(order = 2), linewidth = guide_legend(order = 3, override.aes = list(alpha = 0.5))) +
theme_void(base_size = 12) +
theme(plot.title = element_text(hjust = 0.5, size = 14), legend.position = "right", legend.title = element_text(size = 11), legend.text = element_text(size = 10), plot.margin = margin(15, 10, 20, 20))
#右图:实心蜂群散点,显著邻域按logFC着色,不显著邻域显示灰色
p2 <- ggplot(da, aes(x = Fibroblast_subtype, y = logFC)) +
geom_hline(yintercept = 0, colour = "#BDBDBD", linewidth = 0.35, linetype = 2) +
ggbeeswarm::geom_quasirandom(aes(colour = plotFC, group = Fibroblast_subtype), shape = 16, size = 1.2, alpha = 1, width = 0.32, bandwidth = 0.5) +
stat_summary(fun = mean, geom = "point", shape = 124, size = 4, colour = "#222222") +
scale_colour_gradient2(low = red, mid = "#F4F1EF", high = purple, midpoint = 0, limits = c(-fc_lim, fc_lim), na.value = "#858585") +
scale_y_continuous(breaks = pretty(c(-fc_lim, fc_lim), n = 5), expand = expansion(mult = 0)) +
coord_flip(ylim = c(-fc_lim - 0.3, fc_lim + 0.3), clip = "off") +
labs(x = "Fibroblast subcluster", y = "log(FC)") +
guides(colour = "none") +
theme_bw(base_size = 12) +
theme(panel.grid.major = element_line(colour = "#ECECEC", linewidth = 0.35), panel.grid.minor = element_line(colour = "#F3F3F3", linewidth = 0.25), panel.border = element_rect(colour = "#777777", linewidth = 0.5), axis.text = element_text(colour = "#333333"), axis.title = element_text(size = 12), plot.margin = margin(0, 10, 10, 5))
#右图顶部富集方向箭头
direction <- ggplot() +
annotate("segment", x = 0.48, xend = 0.03, y = 0.2, yend = 0.2, linewidth = 0.65, arrow = grid::arrow(length = grid::unit(0.10, "inches"), type = "closed")) +
annotate("segment", x = 0.52, xend = 0.97, y = 0.2, yend = 0.2, linewidth = 0.65, arrow = grid::arrow(length = grid::unit(0.10, "inches"), type = "closed")) +
annotate("text", x = 0.25, y = 0.7, label = "Higher in Diseases", size = 3.8) +
annotate("text", x = 0.75, y = 0.7, label = "Higher in Donor", size = 3.8) +
coord_cartesian(xlim = c(0, 1), ylim = c(0, 1), expand = FALSE, clip = "off") +
theme_void() +
theme(plot.margin = margin(5, 10, 0, 5))
#按接近原图2.44:1的画布比例组合
right <- (direction / p2) + plot_layout(heights = c(0.09, 0.91))
fig <- (p1 | right) + plot_layout(widths = c(1.12, 1)) + plot_annotation(theme = theme(plot.background = element_rect(fill = "white", colour = NA)))
#保存当前组合图
ggsave(file.path(outdir, "Fibroblast_Milo_Beeswarm.png"), plot = fig, width = 12.66, height = 5.19, units = "in", dpi = 600, bg = "white")
ggsave(file.path(outdir, "Fibroblast_Milo_Beeswarm.pdf"), plot = fig, width = 12.66, height = 5.19, units = "in", device = "pdf", bg = "white", useDingbats = FALSE)
06 参数速查与输出文件
- 降维特征与维度:nfeatures = 3000,dims / d = 30,分别决定高变基因集合和构图使用的低维空间。
- 亚群划分:resolution = 0.3,影响重新聚类结果,以及蜂群图按哪些亚群分行。
- 邻域构建:k = 30,影响近邻图和局部结构;prop = 0.1 是初始索引采样比例,不等于最终邻域占比。
- 显著性着色:SpatialFDR < 0.05。阈值决定哪些邻域着色,不应为获得更多彩色点而随意放宽。
- 散点显示:width = 0.32、size = 1.2,分别控制蜂群图的展开宽度和点大小。
- 文件输出:12.66 × 5.19 英寸,横向比例约 2.44∶1;PNG 为 600 dpi,同时保存 PDF。
分析维度、聚类和邻域参数会影响分析或分组,显著性阈值影响着色;散点大小与输出尺寸主要影响显示。本文没有参数对照实验,这些数值只是示例起点。不要为了让红紫色更醒目,只挑有利的分析参数。
07 迁移到自己的数据前先核对
先改输入路径和输出目录,再核对细胞类型列、条件列、样本列和患者列。Seurat v5 多层 RNA assay 在读取计数前先 JoinLayers;如果原始计数保存在外部磁盘,还需确保对应文件可访问。本例先提取成纤维细胞,因此结论限定在成纤维细胞内部的相对组成。组织中成纤维细胞总数是否增加,需要另外的数据或分析支持。
科研图鉴第一期,先从读懂一个点开始。下一期你想拆解哪张论文图?欢迎留下图名或论文链接。
参考资料
[1] Papachristoforou E, Kong K, Colella F, et al. A human single-cell atlas identifies OLR1+ scar-associated macrophages as a potential therapeutic target for chronic liver disease. Nature Genetics 2026;58(10):2550–2565. DOI: 10.1038/s41588-026-02774-w