0

我正在尝试使用 ggtree 绘制一棵大树,但是由于它的大小,我想折叠多个节点。我正在关注一个教程,但它一次折叠一个节点,这不是我的选择。

这是我的代码:

library(ggtree)
library(ape)
library(ggplot2)
library(colorspace)
library(Biostrings)
library(phytools)
library(treeio)
library(dplyr)
library(readr)
library(tidyr)
library(reshape2)


tempnwk<- "((('clade01_1':1.35E-4,('clade01_2':1.0E-6,'clade01_3':1.0E-6):3.3E-5):3.3E-5,('clade02_1':2.7E-4,'clade02_2':3.3E-5):3.3E-5):1.0E-6,'clade03_1':1.0E-6);"  
testTree0 <- read.tree(text = tempnwk)
#
testcollapse0<- ggtree(testTree0)

#Now, this works:
#
testcollapse0b<- testcollapse0 %>% collapse(node = 10) +
  geom_point2(aes(subset=(node==10)),
              shape=21, size=5, fill='green')
testcollapse0b<- collapse(testcollapse0b, node = 11) +
  geom_point2(aes(subset=(node==11)),
              shape=21, size=5, fill='red')

testcollapse0b ####This works
#
#
##############THis does not:
nodes2go<- c(10, 11)
myTestCols<- c('green', 'red')
testcollapse1<- testcollapse0
for(i in 1:2) {
  testcollapse1<- collapse(
    testcollapse1, node = nodes2go[i])  +
    geom_point2(
      aes(subset=(node==i)), shape=23,
      size=7, fill=myTestCols[i])
}
rm(i)
#
testcollapse1 + geom_text(aes(label=label))
#
#Error in FUN(X[[i]], ...) : object 'i' not found

我需要一些帮助,我不知道如何解决它。我看了看drop.tip,但我不确定这是我想要的,因为我仍然想要折叠节点所在的彩色点。

我期待您的反馈,感谢您的关注。

4

1 回答 1

0

出色地,

在等待一种理智的方式来做这件事时,快速和肮脏的方式将完成这项工作:

myTestCols2<- c("'green'", "'red'")
testcollapse2<- testcollapse0
teststring0<- "testcollapse2<- collapse(testcollapse2, node=NODE)  + geom_point2(aes(subset=(node==NODE)), shape=23, size=7, fill=COLOR);"
testString2<- character()
for(i in 1:2) {
  indString<- gsub(
    pattern = "NODE",replacement =  nodes2go[i],
    x = teststring0)
  indString<- gsub(
    pattern = "COLOR", replacement = myTestCols2[i],
    x = indString)
  testString2<- c(testString2, indString)
} 
rm(i, indString)
#
#Run the command
eval(parse(text = testString2))
##And now plot:
testcollapse2

是的,我知道必须有更好的方法来做到这一点

于 2021-05-27T17:31:52.263 回答