💡
concept
ggplot2可视化
```r library(ggplot2) # 创建示例数据 data <- data.frame( gene = rep(c("GeneA", "GeneB", "GeneC"), each = 10), expression = c(rnorm(10, 10, 2), rnorm(10, 15, 3), rnorm(10, 8, 1)), group = rep(c("...
📖 定义
library(ggplot2)
# 创建示例数据
data <- data.frame(
gene = rep(c("GeneA", "GeneB", "GeneC"), each = 10),
expression = c(rnorm(10, 10, 2), rnorm(10, 15, 3), rnorm(10, 8, 1)),
group = rep(c("Control", "Treatment"), each = 5, times = 3)
)
# 散点图
p1 <- ggplot(data, aes(x = group, y = expression, color = group)) +
geom_point(position = position_jitter(width = 0.2), size = 3) +
geom_boxplot(alpha = 0.3, outlier.shape = NA) +
facet_wrap(~gene) +
labs(title = "Gene Expression Comparison",
x = "Condition",
y = "Expression Level") +
theme_minimal()
print(p1)
# 热图(使用pheatmap包)
library(pheatmap)
expr_matrix <- matrix(rnorm(100), nrow = 10)
rownames(expr_matrix) <- paste0("Gene", 1:10)
colnames(expr_matrix) <- paste0("Sample", 1:10)
pheatmap(expr_matrix,
scale = "row",
clustering_method = "ward.D2",
color = colorRampPalette(c("navy", "white", "firebrick"))(50))
# 火山图(差异表达结果)
de_results <- data.frame(
gene = paste0("Gene", 1:1000),
log2FC = rnorm(1000, 0, 2),
pvalue = runif(1000)
)
de_results$padj <- p.adjust(de_results$pvalue, method = "BH")
ggplot(de_results, aes(x = log2FC, y = -log10(padj))) +
geom_point(aes(color = abs(log2FC) > 1 & padj < 0.05), alpha = 0.5) +
scale_color_manual(values = c("grey", "red")) +
geom_vline(xintercept = c(-1, 1), linetype = "dashed") +
geom_hline(yintercept = -log10(0.05), linetype = "dashed") +
labs(x = "log2 Fold Change", y = "-log10 adjusted p-value") +
theme_bw()