BioF3 FigCode · SCI 绘图代码集
动态火山图(Animated Volcano)
分类:差异分析 | 依赖包:gganimate、ggplot2、gifski
解决的生物学问题
处理后基因表达变化的动态过程是什么?哪些基因早期就响应、哪些晚期才出现?
应用场景
- 药物处理时间序列
- 感染早期 / 中期 / 晚期
- 发育阶段动态分化
- 汇报演示 GIF / mp4
输入数据格式
长表数据框:gene, log2FC, padj, time_point
关键参数
- transition_states = "time_point"
- transition_length = 2 / state_length = 2
- fps = 10
- renderer = gifski_renderer()
快速开始
1. 直接在线运行
打开 FigCode 在线绘图,点击本工具卡片的"在线绘图"按钮即可用内置示例数据出图,零环境配置。
2. 本地复现
下载脚本和示例数据,本地 RStudio 运行:
# 下载
curl -O https://<your-site>/figcode/scripts/volcano-animated.R
curl -O https://<your-site>/figcode/data/volcano-animated.csv
3. 安装依赖
# CRAN 包
install.packages(c("gganimate", "ggplot2", "gifski"))
# Bioconductor 包(如需)
# BiocManager::install(c())
完整代码
library(gganimate)
library(ggplot2)
# --- Demo: 4 time points ---
set.seed(42)
times <- c("0h", "6h", "12h", "24h")
make_de <- function(t) {
n <- 800
data.frame(
gene = paste0("Gene", 1:n),
log2FC = rnorm(n, 0, 1.4 * (which(times == t) / length(times))),
padj = runif(n, 0, 1),
time_point = factor(t, levels = times)
)
}
df <- do.call(rbind, lapply(times, make_de))
df$padj[sample(nrow(df), 60)] <- runif(60, 1e-12, 0.001)
df$sig <- ifelse(df$padj < 0.05 & df$log2FC > 1, "Up",
ifelse(df$padj < 0.05 & df$log2FC < -1, "Down", "NS"))
# --- Animated volcano ---
p <- ggplot(df, aes(log2FC, -log10(padj), color = sig)) +
geom_point(size = 0.7, alpha = 0.6) +
scale_color_manual(values = c(Up="#dc2626", Down="#2563eb", NS="#9ca3af")) +
geom_vline(xintercept = c(-1, 1), linetype = "dashed") +
geom_hline(yintercept = -log10(0.05), linetype = "dashed") +
labs(title = "Volcano at {closest_state}",
x = "log2 Fold Change", y = "-log10(padj)") +
theme_classic()
p_anim <- p + transition_states(time_point, transition_length = 2,
state_length = 1) +
ease_aes("cubic-in-out") +
enter_fade() + exit_fade()
# Save as gif
anim_save("plot_001.gif", animation = p_anim,
width = 700, height = 500, fps = 10, duration = 6)
替换为自己的数据
脚本中以 # --- Demo data --- 标注的段落是示例数据生成代码。替换为自己的数据时,保持列名一致即可:
- 输入格式:长表数据框:gene, log2FC, padj, time_point
- 使用
read.csv()/readRDS()读取本地文件
延伸阅读
- 相关教程:动态火山图 完整流程
- 出现 bug?欢迎在 FigCode 页面 点击对应卡片,在评论区留言。