BioF3 FigCode · SCI 绘图代码集
树状图(Treemap)
分类:功能富集 | 依赖包:treemap
解决的生物学问题
富集到的几十个 GO term 按"父类"汇总后,哪些大类占主导?
应用场景
- GO BP/MF/CC 三大类占比
- KEGG 通路类别汇总
- 突变频谱分类展示
- 基金分类资金占比
输入数据格式
层级数据框:parent, child, value
关键参数
- index = c(parent, child) 嵌套层级
- vSize = 数值列
- palette / type = "categorical"
- fontsize.labels 字号
快速开始
1. 直接在线运行
打开 FigCode 在线绘图,点击本工具卡片的"在线绘图"按钮即可用内置示例数据出图,零环境配置。
2. 本地复现
下载脚本和示例数据,本地 RStudio 运行:
# 下载
curl -O https://<your-site>/figcode/scripts/treemap.R
curl -O https://<your-site>/figcode/data/treemap.csv
3. 安装依赖
# CRAN 包
install.packages(c("treemap"))
# Bioconductor 包(如需)
# BiocManager::install(c())
完整代码
library(treemap)
# --- Demo: GO BP top categories ---
df <- data.frame(
category = c(rep("Immune Response", 4),
rep("Cell Cycle", 4),
rep("Metabolism", 5),
rep("Signal Transduction", 4),
rep("Apoptosis", 3)),
term = c(
"T cell activation","B cell proliferation","Cytokine response","Interferon signaling",
"Mitotic G1/S","Spindle checkpoint","DNA replication","Cytokinesis",
"Lipid metabolism","Glycolysis","Amino acid","Cholesterol","Fatty acid oxidation",
"MAPK","Wnt","TGFb","Hedgehog",
"Caspase activation","BCL2 family","Mitochondrial apoptosis"
),
count = c(45, 32, 28, 25,
56, 42, 38, 30,
48, 36, 33, 30, 28,
52, 41, 35, 31,
38, 30, 25)
)
treemap(
df,
index = c("category", "term"),
vSize = "count",
vColor = "category",
type = "categorical",
palette = c("#dc2626","#2563eb","#059669","#d97706","#7c3aed"),
title = "GO BP — Enrichment by Category",
fontsize.labels = c(15, 9),
fontcolor.labels = c("white","white"),
border.col = "white",
border.lwds = c(2, 1),
align.labels = list(c("left","top"), c("center","center"))
)
替换为自己的数据
脚本中以 # --- Demo data --- 标注的段落是示例数据生成代码。替换为自己的数据时,保持列名一致即可:
- 输入格式:层级数据框:parent, child, value
- 使用
read.csv()/readRDS()读取本地文件
延伸阅读
- 后续将补充配套教程
- 出现 bug?欢迎在 FigCode 页面 点击对应卡片,在评论区留言。