BioF3 FigCode · SCI 绘图代码集
免疫细胞组间对比(Immune Group Boxplot)
分类:分布可视化 | 依赖包:ggplot2、reshape2
解决的生物学问题
哪些免疫细胞在分组间显著差异浸润?这些差异是否和临床表型一致?
应用场景
- Tumor vs Normal 免疫差异
- 高低风险组免疫格局对比
- Responder vs Non-responder 免疫预测
- 亚型间免疫差异
输入数据格式
数据框:CellType, Sample, Score, Group
关键参数
- 筛选 top variable cells(cap 12)
- stat_compare_means 加 p 值
- facet_wrap 单独看每细胞
快速开始
1. 直接在线运行
打开 FigCode 在线绘图,点击本工具卡片的"在线绘图"按钮即可用内置示例数据出图,零环境配置。
2. 本地复现
下载脚本和示例数据,本地 RStudio 运行:
# 下载
curl -O https://<your-site>/figcode/scripts/immune-boxplot.R
curl -O https://<your-site>/figcode/data/immune-boxplot.csv
3. 安装依赖
# CRAN 包
install.packages(c("ggplot2", "reshape2"))
# Bioconductor 包(如需)
# BiocManager::install(c())
完整代码
library(ggplot2)
library(reshape2)
# --- Demo data ---
set.seed(42)
n_samples <- 60
mat <- matrix(rnorm(8 * n_samples), 8, n_samples)
rownames(mat) <- c('CD8 T','Treg','Macrophages','NK','DC','Tfh','Th1','MDSC')
grp <- rep(c('Tumor','Normal'), each = n_samples/2)
mat[1,] <- mat[1,] + ifelse(grp == 'Tumor', 0.8, 0)
mat[3,] <- mat[3,] + ifelse(grp == 'Tumor', 1.2, 0)
df <- melt(mat); colnames(df) <- c('CellType','Sample','Score')
df$Group <- grp[df$Sample]
ggplot(df, aes(CellType, Score, fill = Group)) +
geom_boxplot(outlier.size = 0.5, width = 0.7) +
scale_fill_manual(values = c(Tumor = '#dc2626', Normal = '#2563eb')) +
labs(x = NULL, y = 'ssGSEA Score', title = 'Immune Infiltration: Group Comparison') +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 9), legend.position = 'top')
替换为自己的数据
脚本中以 # --- Demo data --- 标注的段落是示例数据生成代码。替换为自己的数据时,保持列名一致即可:
- 输入格式:数据框:CellType, Sample, Score, Group
- 使用
read.csv()/readRDS()读取本地文件
延伸阅读
- 相关教程:免疫细胞组间对比 完整流程
- 出现 bug?欢迎在 FigCode 页面 点击对应卡片,在评论区留言。