怎样用R语言ggplot2+ggtree+aplot画气泡图组合聚类树图

发布时间:2021-11-22 15:54:21 作者:柒染
来源:亿速云 阅读:1345
# 怎样用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"))

2. 加载包

library(ggplot2)
library(ggtree)
library(aplot)
library(dplyr)
library(tidyr)

3. 示例数据准备

我们将使用两个数据集: - 树状图数据:随机生成的聚类树 - 气泡图数据:模拟的物种丰度数据

# 生成随机树
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))
)

第一部分:绘制基础树状图

使用ggtree绘制聚类树

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轴标签防止重叠


第三部分:使用aplot组合图形

基础拼接方法

p_combined <- p_tree %>% 
    insert_right(p_bubble, width = 1.5) # 控制右侧图宽度

print(p_combined)

高级调整技巧

  1. 对齐调整
p_combined_aligned <- p_tree +
    xlim_tree(5) %>% # 确保树图有足够的空间
    insert_right(
        p_bubble + 
            scale_y_discrete(limits = tree$tip.label), # 确保样本顺序一致
        width = 1.5
    )
  1. 添加图例
p_bubble <- p_bubble +
    guides(size = guide_legend(title = "Expression Level"),
           color = guide_legend(title = "Genes"))
  1. 主题统一
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)

第五部分:常见问题解决

问题1:图形元素不对齐

解决方案: - 确保两个图的y轴使用相同的因子水平顺序

p_bubble <- p_bubble + 
    scale_y_discrete(limits = tree$tip.label)

问题2:气泡大小不合适

调整方法:

p_bubble <- p_bubble +
    scale_size_continuous(range = c(1, 8), # 最小和最大尺寸
                         breaks = c(1, 5, 10)) # 指定图例断点

问题3:树图分支重叠

解决方法:

p_tree <- ggtree(tree, branch.length = "none") + # 取消分支长度
    layout_dendrogram() # 使用dendrogram布局

结语

通过ggplot2+ggtree+aplot的组合,我们可以灵活地创建各种树图与其他图形的组合。这种方法不仅适用于生物信息学领域,也可以应用于任何需要展示层次结构与多维度数据的场景。关键步骤包括:

  1. 分别构建树图和气泡图
  2. 确保两个图的样本顺序一致
  3. 使用aplot进行智能拼接
  4. 通过主题系统统一图形风格

希望本文能帮助您创建出专业的组合可视化图形!


参考文献

  1. Yu G, et al. (2017) ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution.
  2. Wickham H (2016) ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York.
  3. aplot package documentation: https://github.com/YuLab-SMU/aplot

”`

推荐阅读:
  1. R语言笔记 画多个图
  2. vscode为什么画不了图

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ggplot2 r语言 ggtree

上一篇:R语言怎么实现柱形图

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》