BioF3 FigCode · SCI 绘图代码集
免疫细胞堆叠图(Immune Composition Stack)
分类:分布可视化 | 依赖包:ggplot2、reshape2
解决的生物学问题
每个样本的免疫微环境组成是什么样?组间免疫格局有差异吗?
应用场景
- TCGA 队列免疫浸润全景图
- Tumor vs Normal 免疫格局对比
- 治疗前后免疫细胞变化
- 亚型分类下的免疫差异
输入数据格式
矩阵:行=免疫细胞,列=样本,值=ssGSEA score(已归一化到 0-1)
关键参数
- 归一化(每细胞 min-max)
- ggplot fill 色板(28 色)
- 样本顺序(聚类 / 风险分组)
快速开始
1. 直接在线运行
打开 FigCode 在线绘图,点击本工具卡片的"在线绘图"按钮即可用内置示例数据出图,零环境配置。
2. 本地复现
下载脚本和示例数据,本地 RStudio 运行:
# 下载
curl -O https://<your-site>/figcode/scripts/immune-composition.R
curl -O https://<your-site>/figcode/data/immune-composition.csv
3. 安装依赖
# CRAN 包
install.packages(c("ggplot2", "reshape2"))
# Bioconductor 包(如需)
# BiocManager::install(c())
完整代码
library(ggplot2)
library(reshape2)
# --- Demo data ---
set.seed(42)
n_cells <- 12; n_samples <- 30
mat <- matrix(runif(n_cells * n_samples), n_cells, n_samples)
mat <- t(apply(mat, 1, function(x) (x - min(x)) / (max(x) - min(x) + 1e-10)))
rownames(mat) <- c('CD8 T','CD4 T','Treg','B cells','NK','Macrophages','DC','Neutrophils','Mast','Tfh','Th1','Th17')
colnames(mat) <- paste0('S', 1:n_samples)
df <- melt(mat); colnames(df) <- c('CellType','Sample','Score')
df$Sample <- factor(df$Sample, levels = colnames(mat))
ggplot(df, aes(x = Sample, y = Score, fill = CellType)) +
geom_bar(stat = 'identity', position = 'fill', width = 0.9) +
scale_y_continuous(labels = scales::percent) +
labs(x = NULL, y = 'Relative Proportion', title = 'Immune Cell Composition (ssGSEA)', fill = 'Cell Type') +
theme_classic() +
theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 7), legend.text = element_text(size = 8))
替换为自己的数据
脚本中以 # --- Demo data --- 标注的段落是示例数据生成代码。替换为自己的数据时,保持列名一致即可:
- 输入格式:矩阵:行=免疫细胞,列=样本,值=ssGSEA score(已归一化到 0-1)
- 使用
read.csv()/readRDS()读取本地文件
延伸阅读
- 相关教程:免疫细胞堆叠图 完整流程
- 出现 bug?欢迎在 FigCode 页面 点击对应卡片,在评论区留言。