BioF3 FigCode · SCI 绘图代码集

空间细胞解卷积(Spatial Deconvolution)

导出日期:2026年6月27日

分类:降维可视化 | 依赖包:ggplot2、scatterpie

解决的生物学问题

每个 spot 实际上是几种细胞的混合?组织上哪些区域被哪些细胞类型占据?

应用场景

输入数据格式

spot 空间坐标 + 解卷积结果矩阵(spot × celltype proportions,来自 cell2location / Tangram / SPOTlight)

关键参数

快速开始

1. 直接在线运行

打开 FigCode 在线绘图,点击本工具卡片的"在线绘图"按钮即可用内置示例数据出图,零环境配置。

2. 本地复现

下载脚本和示例数据,本地 RStudio 运行:

# 下载
curl -O https://<your-site>/figcode/scripts/spatial-deconv.R
curl -O https://<your-site>/figcode/data/spatial-deconv.csv

3. 安装依赖

# CRAN 包
install.packages(c("ggplot2", "scatterpie"))

# Bioconductor 包(如需)
# BiocManager::install(c())

完整代码

library(ggplot2)
library(scatterpie)

# --- Demo: 模拟 Visium 网格 + 解卷积结果(spot × celltype proportions)---
# 实际比例矩阵由 cell2location / Tangram / SPOTlight 生成
set.seed(42)
grid <- expand.grid(x = seq(1, 20), y = seq(1, 20))
n <- nrow(grid)
celltypes <- c("Astro", "Excit", "Inhib", "Oligo", "Microglia")

# 让细胞类型有空间结构(不同区域不同主导细胞)
region <- with(grid, ifelse(y > 13, 1, ifelse(y < 7, 3, 2)))
props <- t(sapply(region, function(r) {
  base <- runif(5, 0.05, 0.2)
  base[r] <- base[r] + runif(1, 0.5, 1.0)  # 该区域主导细胞
  base / sum(base)
}))
colnames(props) <- celltypes
df <- cbind(grid, props)

# --- scatterpie:每个 spot 一个饼图,叠在组织坐标上 ---
ggplot() +
  geom_scatterpie(aes(x = x, y = y, r = 0.45), data = df,
                  cols = celltypes, color = NA) +
  scale_fill_brewer(palette = "Set1") +
  coord_equal() +
  theme_void() +
  labs(title = "Spatial Cell Type Composition (scatterpie)", fill = "Cell type")

替换为自己的数据

脚本中以 # --- Demo data --- 标注的段落是示例数据生成代码。替换为自己的数据时,保持列名一致即可:

延伸阅读