有绘图经验的朋友们都知道“一图胜千言”的道理。很多时候杂志编辑或审稿人看到形象生动又美观的可视化图片会对论文产生很多好感。因为绘图的技能不仅代表作者的数据分析能力,也代表一种对待科学严谨的态度。而弦图(Chord Diagram)因其优美的弧线设计和信息表达的直观性,成为学术论文和报告中吸睛的神器。弦图不仅能够直观展示多组数据之间的复杂关系,还可以通过精美的设计提升研究结果的感染力,让审稿人眼前一亮,从而增加论文的接受率。
今天的更新带你逐步学习如何用R语言制作从基础到高级的弦图,最后呈现一幅高度优化的、具有科研杂志配色风格的弦图。相信我们的可视化参数可以直接套用到你的数据之中,助你轻松上手弦图。
1. 什么是弦图?
2. 数据下载和分步绘图
GEOquery
包下载公开的基因表达数据并绘制三个不同难度的弦图。2.1 安装并加载必要的包
# 加载包
library(GEOquery)
library(circlize)
2.2 从GEO下载数据
GEOquery
下载一个基因表达数据集,提取其中的基因表达量作为绘制弦图的数据来源。# 下载GEO数据集 (以GSE1009为例)
geo_data <- getGEO("GSE1009", GSEMatrix = TRUE)
exprs_data <- exprs(geo_data[[1]])
# 查看数据
head(exprs_data)
## GSM15965 GSM15966 GSM15967 GSM15968 GSM15969 GSM15970
## 1000_at 3045.2 3076.5 3288.1 4870.6 2826.3 4982.3
## 1001_at 891.0 791.7 1351.1 2155.4 1882.9 2520.5
## 1002_f_at 11.6 59.8 40.5 60.5 104.3 329.4
## 1003_s_at 137.5 117.0 262.0 275.0 750.0 167.3
## 1004_at 69.5 241.1 287.1 529.1 456.8 956.9
## 1005_at 16839.7 13852.0 7485.8 4637.3 6492.7 2088.2
# 选取前四个基因的表达量作为弦图示例
selected_genes <- exprs_data[1:4, 1:4]
rownames(selected_genes) <- c("GeneA", "GeneB", "GeneC", "GeneD")
colnames(selected_genes) <- c("Sample1", "Sample2", "Sample3", "Sample4")
selected_genes
## Sample1 Sample2 Sample3 Sample4
## GeneA 3045.2 3076.5 3288.1 4870.6
## GeneB 891.0 791.7 1351.1 2155.4
## GeneC 11.6 59.8 40.5 60.5
## GeneD 137.5 117.0 262.0 275.0
2.3 绘制弦图
2.3.1 基础弦图:简单的基因表达量关系
# 基因表达关系矩阵
mat <- as.matrix(selected_genes)
# 保存为png文件
png("basic_chord_diagram.png", width = 8, height = 8, units = "in", res = 300)
# 绘制基础弦图
chordDiagram(mat, transparency = 0.5, annotationTrack = "grid", preAllocateTracks = 1)
title("基础弦图")
dev.off()
2.3.2 进阶弦图:分组和颜色增强
# 设置分组和颜色
group <- c("Group1", "Group2", "Group1", "Group2")
names(group) <- rownames(mat)
# 保存为png文件
png("update_chord_diagram.png", width = 8, height = 8, units = "in", res = 300)
# 绘制分组弦图
chordDiagram(mat,
grid.col = c("Group1" = "#1f77b4", "Group2" = "#ff7f0e"),
transparency = 0.3,
annotationTrack = "grid",
preAllocateTracks = 1)
# 添加图例
legend("bottomright",
legend = c("Group1", "Group2"),
fill = c("#1f77b4", "#ff7f0e"),
border = NA,
bty = "n")
title("进阶弦图")
dev.off()
2.3.3 完整弦图:配色美化
# 优化数据和配色
mat2 <- mat * 1.5 # 放大数据值
rownames(mat2) <- colnames(mat2) <- c("GeneX", "GeneY", "GeneZ", "GeneW")
grid_colors <- c("GeneX" = "#E69F00", "GeneY" = "#56B4E9",
"GeneZ" = "#009E73", "GeneW" = "#F0E442")
# 保存为png文件
png("Style_chord_diagram.png", width = 8, height = 8, units = "in", res = 300)
# 设置字体和布局
par(mar = c(1, 1, 3, 1), family = "serif")
circos.clear()
chordDiagram(mat2,
grid.col = grid_colors,
annotationTrack = c("grid", "name"),
preAllocateTracks = list(track.height = 0.05))
# 标题和文字样式
title("完整弦图", cex.main = 2, font.main = 3, col.main = "#333333")
# 自定义图例
legend("topright",
legend = names(grid_colors),
fill = grid_colors,
title = "Gene Categories",
border = "black",
bty = "n",
cex = 0.8)
dev.off()