我有以下数据和图表:
数据:
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轴上的加工值,然后将它们组合?
这是我希望它的外观(大约):
答案 0 :(得分:2)
我们需要按照与树图相同的顺序来排列图块图,然后我们需要将这两个图图布置成它们对应的位置。第一项任务相对简单,但是我不确定在没有手动调整布局的情况下如何执行第二项任务。
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
和p2$data %>% filter(isTip) %>% arrange(parent) %>% 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)