簡體   English   中英

將 ggtree 對象添加到具有共享 y 軸的現有 ggplot

[英]Adding a ggtree object to already existing ggplot with shared y-axis

我有以下數據和情節:

數據:

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 軸進行排序,以便它們匹配。 我嘗試在這里使用一些解決方案,但我似乎無法使用 facet_plot 函數生成相同的圖。 有沒有辦法在兩個圖上識別 y 軸上的加工值,然后將它們組合起來?

這就是我想要的樣子(大約):

在此處輸入圖片說明

我們需要按照與樹圖相同的順序排列瓦片圖,然后我們需要將兩個圖放在一起,使它們對應。 第一個任務相對簡單,但我不知道如何在沒有手動調整布局的情況下完成第二個任務。

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 這將輸出一個包含代表繪圖結構的各種數據框的列表。 查看這些,我們可以看到第五個和第六個數據幀具有節點標簽。 我們將使用第五個( p2b$data[[5]]並根據y列對它們進行排序以獲得節點標簽的向量( p2b$data[[5]] %>% arrange(y) %>% pull(label)) )。 然后我們將test_df$id轉換test_df$id具有此節點順序的因子變量。

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

(作為另一種選擇,您可以使用p2$data %>% filter(isTip) %>% arrange(parent) %>% pull(label)直接從p2獲取節點的排序)

現在我們可以生成具有與樹圖對應的節點順序的瓦片圖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)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM