您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# 怎样用R语言ggplot2+ggtree+aplot画气泡图组合聚类树图
## 引言
在生物信息学和数据可视化领域,将聚类树图(如系统发育树或层次聚类树)与其他类型图表(如气泡图、热图等)组合展示是一种常见需求。这种组合图可以同时展示样本间的进化/聚类关系以及多维度特征数据。本文将详细介绍如何使用R语言中的`ggplot2`、`ggtree`和`aplot`包来实现气泡图与聚类树图的组合。
---
## 准备工作
### 1. 安装必要R包
```r
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("ggtree")
install.packages(c("ggplot2", "aplot", "tidyr", "dplyr"))
library(ggplot2)
library(ggtree)
library(aplot)
library(dplyr)
library(tidyr)
我们将使用两个数据集: - 树状图数据:随机生成的聚类树 - 气泡图数据:模拟的物种丰度数据
# 生成随机树
set.seed(123)
tree <- rtree(10) # 10个叶节点的随机树
tree$tip.label <- paste0("Sample_", LETTERS[1:10])
# 生成模拟丰度数据
bubble_data <- data.frame(
Sample = rep(tree$tip.label, each = 5),
Feature = rep(paste0("Gene_", 1:5), 10),
Value = abs(rnorm(50, mean = 5, sd = 3))
)
p_tree <- ggtree(tree) +
geom_tiplab(align = TRUE) +
theme_tree2() +
xlim(0, 5) # 控制树图的宽度
print(p_tree)
关键参数说明:
- geom_tiplab()
: 添加叶节点标签
- theme_tree2()
: 使用经典树图主题
- xlim()
: 控制树的x轴范围,影响后续拼接时的对齐
首先将数据转换为宽格式(适合气泡图):
bubble_wide <- bubble_data %>%
pivot_wider(names_from = Feature, values_from = Value)
# 确保样本顺序与树图一致
bubble_wide <- bubble_wide[match(tree$tip.label, bubble_wide$Sample), ]
p_bubble <- bubble_wide %>%
pivot_longer(-Sample, names_to = "Feature", values_to = "Value") %>%
ggplot(aes(x = Feature, y = Sample, size = Value, color = Feature)) +
geom_point() +
scale_size_continuous(range = c(1, 10)) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
print(p_bubble)
关键参数说明:
- geom_point()
: 绘制气泡
- scale_size_continuous()
: 控制气泡大小范围
- theme(axis.text.x)
: 旋转X轴标签防止重叠
p_combined <- p_tree %>%
insert_right(p_bubble, width = 1.5) # 控制右侧图宽度
print(p_combined)
p_combined_aligned <- p_tree +
xlim_tree(5) %>% # 确保树图有足够的空间
insert_right(
p_bubble +
scale_y_discrete(limits = tree$tip.label), # 确保样本顺序一致
width = 1.5
)
p_bubble <- p_bubble +
guides(size = guide_legend(title = "Expression Level"),
color = guide_legend(title = "Genes"))
common_theme <- theme(
text = element_text(family = "Arial"),
legend.position = "bottom"
)
p_combined <- p_combined & common_theme
# 加载示例数据
data(microbiome)
phy_tree <- microbiome::phy_tree(microbiome)
otu_table <- as.data.frame(microbiome::otu_table(microbiome))
# 绘制树
p_micro_tree <- ggtree(phy_tree) %<+% microbiome::tax_table(microbiome) +
geom_tippoint(aes(color = Phylum))
# 绘制气泡图(前20个OTU)
top_otus <- names(sort(colSums(otu_table), decreasing = TRUE)[1:20])
p_micro_bubble <- otu_table %>%
select(all_of(top_otus)) %>%
mutate(Sample = rownames(.)) %>%
pivot_longer(-Sample) %>%
ggplot(aes(x = name, y = Sample, size = value)) +
geom_point()
# 组合图形
p_micro_tree %>%
insert_right(p_micro_bubble, width = 2)
解决方案:
- 确保两个图的y
轴使用相同的因子水平顺序
p_bubble <- p_bubble +
scale_y_discrete(limits = tree$tip.label)
调整方法:
p_bubble <- p_bubble +
scale_size_continuous(range = c(1, 8), # 最小和最大尺寸
breaks = c(1, 5, 10)) # 指定图例断点
解决方法:
p_tree <- ggtree(tree, branch.length = "none") + # 取消分支长度
layout_dendrogram() # 使用dendrogram布局
通过ggplot2
+ggtree
+aplot
的组合,我们可以灵活地创建各种树图与其他图形的组合。这种方法不仅适用于生物信息学领域,也可以应用于任何需要展示层次结构与多维度数据的场景。关键步骤包括:
aplot
进行智能拼接希望本文能帮助您创建出专业的组合可视化图形!
”`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。