BioF3 FigCode · SCI 绘图代码集
时序轨迹图(Time Trajectory)
分类:分布可视化 | 依赖包:ggplot2
解决的生物学问题
基因表达随时间如何变化?哪些基因呈现相似的时序表达模式?
应用场景
- 时间序列 RNA-seq 表达趋势
- 药物处理后基因响应动态
- 发育过程基因表达变化
- 多时间点差异基因轨迹
输入数据格式
数据框:gene, time_point, expression(长格式)
关键参数
- time 时间变量
- group 基因分组/聚类
- geom 展示方式(line + point)
- facet 是否按模块分面
- smooth 是否添加趋势线(loess)
快速开始
1. 直接在线运行
打开 FigCode 在线绘图,点击本工具卡片的"在线绘图"按钮即可用内置示例数据出图,零环境配置。
2. 本地复现
下载脚本和示例数据,本地 RStudio 运行:
# 下载
curl -O https://<your-site>/figcode/scripts/time-trajectory.R
curl -O https://<your-site>/figcode/data/time-trajectory.csv
3. 安装依赖
# CRAN 包
install.packages(c("ggplot2"))
# Bioconductor 包(如需)
# BiocManager::install(c())
完整代码
library(ggplot2)
# --- Demo data ---
set.seed(42)
time_points <- c(0, 2, 6, 12, 24, 48)
genes <- paste0("Gene", 1:20)
clusters <- rep(c("Early response", "Late response", "Transient", "Stable"), each = 5)
traj_df <- expand.grid(gene = genes, time = time_points)
traj_df$cluster <- rep(clusters, each = length(time_points))
# Different patterns per cluster
traj_df$expression <- with(traj_df, ifelse(
cluster == "Early response", 2 * exp(-time/10) + rnorm(nrow(traj_df), 0, 0.3),
ifelse(cluster == "Late response", 2 * (1 - exp(-time/20)) + rnorm(nrow(traj_df), 0, 0.3),
ifelse(cluster == "Transient", 2 * dnorm(time, 12, 8) * 20 + rnorm(nrow(traj_df), 0, 0.3),
rnorm(nrow(traj_df), 1, 0.3)))))
# --- Plot ---
ggplot(traj_df, aes(x = time, y = expression, group = gene, color = cluster)) +
geom_line(alpha = 0.4) +
geom_point(size = 1) +
stat_summary(aes(group = cluster), fun = mean, geom = "line", linewidth = 1.5) +
facet_wrap(~ cluster) +
labs(x = "Time (hours)", y = "Scaled Expression", title = "Gene Expression Trajectories") +
theme_classic() +
theme(legend.position = "none")
替换为自己的数据
脚本中以 # --- Demo data --- 标注的段落是示例数据生成代码。替换为自己的数据时,保持列名一致即可:
- 输入格式:数据框:gene, time_point, expression(长格式)
- 使用
read.csv()/readRDS()读取本地文件
延伸阅读
- 相关教程:时序轨迹图 完整流程
- 出现 bug?欢迎在 FigCode 页面 点击对应卡片,在评论区留言。