如何在 R 中创建一个可读且可保存的大型缠结图

How can I create a large tanglegram in R that is readable and can be saved

我正在使用 cophyloplot 创建两个系统发育树的缠结图。该方法适用于小树,但随着树变大,输出图像保持相同大小,我找不到扩展它的方法。

下面是一棵工作正常的小树的代码(与示例基本相同:https://www.rdocumentation.org/packages/ape/versions/5.4-1/topics/cophyloplot):

library(ape)

#two random trees
TreeA <- rtree(10)
TreeB <- rtree(10)

#creation of the association matrix:
association <- cbind(TreeB$tip.label, TreeB$tip.label)

cophyloplot(TreeA, TreeB, assoc = association, length.line = 4, space = 28, gap = 3)

这是小树代码产生的:

但是当我使用更大的树时,它们变得不可读。例如,使用带有 100 个提示的树会导致:

无法阅读提示标签。如何扩展渲染以使其可读?

tanglegram 函数带有许多选项来改进您获得的图像的输出(尤其是参数 lab.cex 和 margin_inner)。最大的因素可能是 tanglegram 的外部因素,并且是图形设备的大小(通过 dev.new),因此调整宽度和高度可能会解决大部分问题

这是一个简单的自包含代码,展示了如何使用这些选项来获得不错的输出。

########
## Nice example of some colored trees
# see the coloring of common sub trees:
set.seed(23235)
ss <- sample(1:150, 100)
dend1 <- iris[ss, -5] %>%
  dist() %>%
  hclust("com") %>%
  as.dendrogram()
dend2 <- iris[ss, -5] %>%
  dist() %>%
  hclust("sin") %>%
  as.dendrogram()
dend12 <- dendlist(dend1, dend2)
# dend12 %>% untangle %>% tanglegram
dev.new(width=5, height=4)

dend12 %>% tanglegram(common_subtrees_color_branches = TRUE,
                      lab.cex = .5, margin_inner = 1.3)

##我希望这段代码对大树有帮助

library(ape)
library(phytools)
library(dendextend)
library(viridis)
library(dplyr)
library(phylogram)

tree1 <- read.tree(file = "c1.raxml.bestTree")
tree1 <- midpoint.root(tree1)
tree2 <- read.tree(file = "c1_gubbins.raxml.bestTree")
tree2 <- midpoint.root(tree2)
tree1 <- compute.brlen(tree1)
tree2 <- compute.brlen(tree2)
tree1<- as.dendrogram(tree1)
tree1
tree2<- as.dendrogram(tree2)
dndlist <- dendextend::dendlist(tree1, tree2)
dendextend::tanglegram(dndlist, fast = TRUE, margin_inner = 1.8, lab.cex = 0.3, lwd = 
0.5, edge.lwd = 0.5, type = "r")
dev.copy(pdf, 'Discrete001.pdf', width = 10, height = 11)
dev.off()