【问题标题】:Adding a ggtree object to already existing ggplot with shared y-axis将 ggtree 对象添加到具有共享 y 轴的现有 ggplot
【发布时间】:2018-09-11 17:26:46
【问题描述】:

我有以下数据和绘图:

数据:

structure(list(type = c("mut", "mut", "mut", "mut", "mut", "mut", 
"mut", "mut", "gene", "gene", "gene", "gene"), gene = c("gyrA", 
"gyrA", "gyrB", "gyrB", "parC", "parC", "parE", "parE", "qnrA1", 
"qnrA1", "sul3", "sul3"), type2 = c(1, 1, 1, 1, 1, 1, 1, 1, 2, 
2, 2, 2), id = c("2014-01-7234-1-S", "2015-01-3004-1-S", "2014-01-2992-1-S", 
"2016-17-299-1-S", "2015-01-2166-1-S", "2014-01-4651-1-S", "2016-02-514-2-S", 
"2016-02-402-2-S", "2016-02-425-2-S", "2015-01-5140-1-S", "2016-02-522-2-S", 
"2016-02-739-2-S"), result = c("1", "0", "0", "0", "0", "0", 
"1", "1", "0", "0", "0", "1"), species = c("Broiler", "Pig", 
"Broiler", "Red fox", "Pig", "Broiler", "Wild bird", "Wild bird", 
"Wild bird", "Pig", "Wild bird", "Wild bird"), fillcol = c("Broiler_1", 
"Pig_0", "Broiler_0", "Red fox_0", "Pig_0", "Broiler_0", "Wild bird_1", 
"Wild bird_1", "Wild bird_0", "Pig_0", "Wild bird_0", "Wild bird_1"
)), row.names = c(NA, -12L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = "gene", drop = TRUE, indices = list(
    0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L, 
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
    gene = c("gyrA", "gyrB", "parC", "parE", "qnrA1", "sul3")), row.names = c(NA, 
-6L), class = "data.frame", vars = "gene", drop = TRUE, indices = list(
    0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L, 
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
    gene = c("gyrA", "gyrB", "parC", "parE", "qnrA1", "sul3")), row.names = c(NA, 
-6L), class = "data.frame", vars = "gene", drop = TRUE)))

剧情:

library(ggplot2)

p1 <- ggplot(test_df, aes(fct_reorder(gene, type2),
             factor(id),
             fill = fillcol,
             alpha = result)) +
  geom_tile(color = "white")+
  theme_minimal()+
  labs(fill = NULL)+
  theme(axis.text.x = element_text(angle = 90,
                                   hjust = 1,
                                   vjust = 0.3,
                                   size = 7),
        axis.title = element_blank(),
        panel.grid = element_blank(),
        legend.position = "right")+
  guides(alpha = FALSE)+
  coord_fixed()

此外,我还有以下树对象:

structure(list(edge = structure(c(23L, 23L, 22L, 22L, 21L, 21L, 
20L, 20L, 19L, 19L, 18L, 18L, 17L, 17L, 16L, 16L, 15L, 15L, 14L, 
14L, 13L, 13L, 1L, 3L, 2L, 9L, 22L, 23L, 4L, 5L, 20L, 21L, 11L, 
12L, 18L, 19L, 10L, 17L, 8L, 16L, 6L, 7L, 14L, 15L), .Dim = c(22L, 
2L)), edge.length = c(2, 2, 0, 0, 2.5, 0.5, 2, 2, 0.75, 0.25, 
0.5, 0.5, 2.41666666666667, 0.166666666666667, 3.0625, 0.145833333333333, 
3.38888888888889, 0.326388888888889, 3, 3, 0.5, 0.111111111111111
), tip.label = c("2016-02-425-2-S", "2016-02-522-2-S", "2015-01-2166-1-S", 
"2016-02-402-2-S", "2016-02-514-2-S", "2016-17-299-1-S", "2016-02-739-2-S", 
"2015-01-5140-1-S", "2014-01-2992-1-S", "2014-01-7234-1-S", "2014-01-4651-1-S", 
"2015-01-3004-1-S"), Nnode = 11L), class = "phylo", order = "postorder")

如下图:

library(ggtree)

p2 <- ggtree(tree)+
  geom_treescale()+
  geom_tiplab(align = TRUE, linesize = 0, size = 1)+
  xlim(0, 4.2)

我要做的是将树和第一个图结合起来,并在树中的顺序之后对第一个图的y轴进行排序,以便它们匹配。我尝试使用一些解决方案here,但我似乎无法使用 facet_plot 函数生成相同的图。有没有办法在两个图上识别 y 轴上的匹配值,然后将它们组合起来?

这就是我希望它看起来的样子(大约):

【问题讨论】:

    标签: r ggplot2 ggtree


    【解决方案1】:

    我们需要按照与树状图相同的顺序排列瓦片图,然后我们需要将这两个图布置出来,以便它们对应。第一项任务相对简单,但如果不对布局进行一些手动调整,我不确定如何执行第二项。

    library(tidyverse)
    library(ggtree)
    library(grid)
    library(gridExtra)
    
    p2 <- ggtree(tree)+
      geom_treescale()+
      geom_tiplab(align = TRUE, linesize = 0, size = 3)+
      xlim(0, 4.2)
    

    现在我们已经创建了树形图,让我们以编程方式获取 y 轴的顺序。我们可以使用ggplot_build 来获取情节结构。

    p2b = ggplot_build(p2)
    

    我们可以通过在控制台中运行p2b$data 来查看绘图布局的数据。这将输出一个列表,其中包含表示绘图结构的各种数据框。查看这些,我们可以看到第五个和第六个数据帧具有节点标签。我们将使用第五个(p2b$data[[5]] 并根据y 列对它们进行排序,以获得节点标签向量(p2b$data[[5]] %&gt;% arrange(y) %&gt;% pull(label)))。然后我们将test_df$id 转换为具有此节点排序的因子变量.

    test_df = test_df %>% 
      mutate(id = factor(id, levels=p2b$data[[5]] %>% arrange(y) %>% pull(label)))
    

    (作为另一种选择,您可以直接从p2p2$data %&gt;% filter(isTip) %&gt;% arrange(parent) %&gt;% pull(label) 获取节点的顺序)

    现在我们可以生成平铺图p1,其节点顺序与树图的顺序相对应。

    p1 <- ggplot(test_df, aes(fct_reorder(gene, type2),
                              factor(id),
                              fill = fillcol,
                              alpha = result)) +
      geom_tile(color = "white")+
      theme_minimal()+
      labs(fill = NULL)+
      theme(axis.text.x = element_text(angle = 90,
                                       hjust = 1,
                                       vjust = 0.3,
                                       size = 7),
            axis.title = element_blank(),
            panel.grid = element_blank(),
            legend.position = "right")+
      guides(alpha = FALSE)+
      coord_fixed()
    

    我们可以在下图中看到标签对应。

    grid.arrange(p2, p1, ncol=2)
    

    现在我们需要用一组标签来布置两个图,并且节点线与图块垂直匹配。我通过创建nullGrob()(基本上是p1 下面的一个空白区域)并调整heights 参数来获得对齐,在下面进行了一些手动调整。布局可能可以通过编程方式完成,但这需要一些额外的 grob(图形对象)操作。

    grid.arrange(p2 + theme(plot.margin=margin(0,-20,0,0)),
                 arrangeGrob(p1 + theme(axis.text.y=element_blank()), 
                             nullGrob(), 
                             heights=c(0.98,0.02)), 
                 ncol=2)
    

    【讨论】:

    • 感谢您提供的信息丰富的回答!但是,我似乎找不到函数“nullGrob()”。那是哪个包的?此外,当我使用我的大数据集时,树似乎与图不对齐。我尝试调整 0.98 值以将其推高一点(它与图中 x 轴文本的位置对齐),但它没有改变任何东西。
    • 我让它与边距设置的一些调整一起工作。谢谢!
    • 抱歉,这是grid 包。我忘了将它添加到我的代码中。现已修复。
    猜你喜欢
    • 2013-01-22
    • 1970-01-01
    • 2021-03-14
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2018-09-05
    • 2020-06-11
    相关资源
    最近更新 更多