BioF3 FigCode · SCI 绘图代码集
桑基图(Sankey Plot)
分类:网络与互作 | 依赖包:networkD3
解决的生物学问题
细胞类型在不同条件下如何重新分布?通路成员如何映射到下游模块?
应用场景
- 细胞类型在治疗前后流转
- 原始 cluster 与人工注释对应
- Pathway → 基因 → 表型映射
- GO term 在多通路间的重叠
输入数据格式
数据框:source, target, value(每条流的起点 / 终点 / 权重)
关键参数
- nodes data.frame(label / color)
- links data.frame(source / target / value)
- fontSize(节点字号)
- sinksRight = TRUE 终点对齐右
快速开始
1. 直接在线运行
打开 FigCode 在线绘图,点击本工具卡片的"在线绘图"按钮即可用内置示例数据出图,零环境配置。
2. 本地复现
下载脚本和示例数据,本地 RStudio 运行:
# 下载
curl -O https://<your-site>/figcode/scripts/sankey.R
curl -O https://<your-site>/figcode/data/sankey.csv
3. 安装依赖
# CRAN 包
install.packages(c("networkD3"))
# Bioconductor 包(如需)
# BiocManager::install(c())
完整代码
library(networkD3)
# --- Demo data: cell type re-distribution after treatment ---
nodes <- data.frame(
name = c("Pre_T","Pre_B","Pre_NK","Pre_Mono",
"Post_T","Post_B","Post_NK","Post_Mono","Post_Apoptotic")
)
links <- data.frame(
source = c(0, 0, 1, 1, 2, 3, 3),
target = c(4, 8, 5, 8, 6, 7, 8),
value = c(60, 15, 40, 20, 30, 50, 25)
)
sankeyNetwork(
Links = links, Nodes = nodes,
Source = "source", Target = "target", Value = "value",
NodeID = "name",
fontSize = 12, nodeWidth = 18,
sinksRight = TRUE,
height = 400, width = 700
)
替换为自己的数据
脚本中以 # --- Demo data --- 标注的段落是示例数据生成代码。替换为自己的数据时,保持列名一致即可:
- 输入格式:数据框:source, target, value(每条流的起点 / 终点 / 权重)
- 使用
read.csv()/readRDS()读取本地文件
延伸阅读
- 后续将补充配套教程
- 出现 bug?欢迎在 FigCode 页面 点击对应卡片,在评论区留言。