BioF3 FigCode · SCI 绘图代码集

免疫细胞相关性热图(Immune Correlation Heatmap)

导出日期:2026年6月27日

分类:分布可视化 | 依赖包:ggplot2、reshape2

解决的生物学问题

哪些免疫细胞协同浸润?哪些互斥?这种共浸润模式在不同肿瘤是否一致?

应用场景

输入数据格式

矩阵:行=免疫细胞,列=样本,值=ssGSEA raw score

关键参数

快速开始

1. 直接在线运行

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

2. 本地复现

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

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

3. 安装依赖

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

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

完整代码

library(ggplot2)
library(reshape2)

# --- Demo data ---
set.seed(42)
n_cells <- 14; n_samples <- 60
mat <- matrix(rnorm(n_cells * n_samples), n_cells, n_samples)
# Inject correlation structure
mat[2,] <- mat[1,] + rnorm(n_samples, 0, 0.5)  # CD4 ~ CD8
mat[5,] <- -mat[1,] + rnorm(n_samples, 0, 0.5) # NK anti-CD8
rownames(mat) <- c('CD8 T','CD4 T','Treg','B','NK','Macrophages','DC','Neutrophils','Mast','Tfh','Th1','Th17','Th2','MDSC')

cor_mat <- cor(t(mat), method = 'spearman')
df <- melt(cor_mat); colnames(df) <- c('A','B','r')

ggplot(df, aes(A, B, fill = r)) +
  geom_tile(color = 'white') +
  scale_fill_gradient2(low = '#2563eb', mid = 'white', high = '#dc2626', midpoint = 0, limits = c(-1, 1)) +
  labs(x = NULL, y = NULL, title = 'Immune Cell Co-infiltration (Spearman)', fill = 'r') +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 9))

替换为自己的数据

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

延伸阅读