数据可视化

ggplot2 绘图模板

火山图 (Volcano Plot)

library(ggplot2)
library(dplyr)

# 示例数据
data <- data.frame(
  gene = paste0("Gene", 1:1000),
  log2FC = rnorm(1000, 0, 2),
  pvalue = 10^(-abs(rnorm(1000, 0, 3)))
)

data <- data %>%
  mutate(
    neg_log10_p = -log10(pvalue),
    significance = case_when(
      log2FC > 1 & pvalue < 0.05 ~ "Up",
      log2FC < -1 & pvalue < 0.05 ~ "Down",
      TRUE ~ "NS"
    )
  )

# 绑定
ggplot(data, aes(x = log2FC, y = neg_log10_p, color = significance)) +
  geom_point(alpha = 0.6, size = 1.5) +
  scale_color_manual(values = c("Up" = "#E64B35", "Down" = "#4DBBD5", "NS" = "grey")) +
  geom_hline(yintercept = -log10(0.05), linetype = "dashed") +
  geom_vline(xintercept = c(-1, 1), linetype = "dashed") +
  theme_minimal() +
  labs(x = "log2 Fold Change", y = "-log10(p-value)")

热图 (Heatmap)

library(pheatmap)

# 生成示例数据
mat <- matrix(rnorm(200), nrow = 20, ncol = 10)
rownames(mat) <- paste0("Gene", 1:20)
colnames(mat) <- paste0("Sample", 1:10)

# 注释信息
annotation_col <- data.frame(
  Group = rep(c("Control", "Treatment"), each = 5)
)
rownames(annotation_col) <- colnames(mat)

# 绑定
pheatmap(mat,
  scale = "row",
  clustering_distance_rows = "correlation",
  annotation_col = annotation_col,
  color = colorRampPalette(c("#4DBBD5", "white", "#E64B35"))(100)
)

配色方案推荐

杂志风格配色代码
Nature#E64B35, #4DBBD5, #00A087
Science#3C5488, #F39B7F, #8491B4
Lancet#00468B, #ED0000, #42B540