【问题标题】:Plotting a tree - collapsing a vector of nodes绘制树 - 折叠节点向量
【发布时间】:2021-08-15 21:00:30
【问题描述】:

我正在尝试使用 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,但我不确定这是我想要的,因为我仍然想要折叠节点所在的彩色点。

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

【问题讨论】:

    标签: r ggtree


    【解决方案1】:

    嗯,

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

    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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 2014-06-01
      • 1970-01-01
      相关资源
      最近更新 更多