将 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轴排在树中的顺序之后,让它们匹配。我尝试使用一些解决方案 here,但我似乎无法使用 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[[5]]
并根据 y
列对它们进行排序以获得节点标签向量 (p2b$data[[5]] %>% arrange(y) %>% pull(label))
)。然后我们将转换 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
下方的空白 space)并调整 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)
我有以下数据和情节:
数据:
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 轴上的加工值,然后将它们组合起来?
这是我想要的样子(大约):
我们需要按照与树图相同的顺序排列图块图,然后我们需要将这两个图进行布局以使其对应。第一个任务相对简单,但我不确定如何在不手动调整布局的情况下完成第二个任务。
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]] %>% arrange(y) %>% pull(label))
)。然后我们将转换 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
下方的空白 space)并调整 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)