BioF3 FigCode · SCI 绘图代码集

聚类轨迹图(Cluster Trajectories)

导出日期:2026年6月27日

分类:聚类可视化 | 依赖包:ggplot2

解决的生物学问题

时间序列数据中存在几种主要的表达变化模式?各模式包含哪些功能基因?

应用场景

输入数据格式

时间序列表达矩阵 + 聚类结果

关键参数

快速开始

1. 直接在线运行

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

2. 本地复现

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

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

3. 安装依赖

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

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

完整代码

library(ggplot2)

# --- Demo: simulate clustered time-series ---
set.seed(42)
time_points <- paste0("T", 1:6)
n_genes <- 60
k <- 4

# Define cluster patterns
patterns <- matrix(c(
  0, 1, 2, 2, 1, 0,   # Transient up
  2, 1.5, 1, 0.5, 0, 0, # Early decay
  0, 0, 0.5, 1, 1.5, 2, # Late rise
  1, 1, 1, 1, 1, 1      # Stable
), nrow = k, byrow = TRUE)

cluster_assign <- rep(1:k, each = n_genes/k)
mat <- matrix(0, nrow = n_genes, ncol = 6)
for (i in 1:n_genes) {
  mat[i, ] <- patterns[cluster_assign[i], ] + rnorm(6, 0, 0.3)
}

df <- data.frame(
  gene = rep(paste0("G", 1:n_genes), each = 6),
  time = rep(1:6, times = n_genes),
  expr = as.vector(t(mat)),
  cluster = rep(paste0("Cluster ", cluster_assign), each = 6)
)

# --- Plot ---
ggplot(df, aes(x = time, y = expr, group = gene)) +
  geom_line(alpha = 0.3, color = "grey50") +
  stat_summary(aes(group = cluster, color = cluster), fun = mean,
               geom = "line", linewidth = 2) +
  facet_wrap(~ cluster, nrow = 2) +
  scale_x_continuous(breaks = 1:6, labels = time_points) +
  labs(x = "Time Point", y = "Scaled Expression", title = "Time-Series Clustering") +
  theme_classic() +
  theme(legend.position = "none")

替换为自己的数据

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

延伸阅读