1

I am trying to plot a large tree using ggtree, but, due to its size, I would like to collapse multiple nodes. I am following a tutorial , but it collapses the nodes one at the time, and this is not an option in my case.

Here is my code:

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

I need some help, I am not sure how to fix it. I had a look at drop.tip, but I am not sure that is what I want, since I still want a colored dot where the collapsed node is.

I am looking forward to your feedback, thank you for your kind attention.

Max_IT
  • 602
  • 5
  • 15

1 Answers1

1

Well,

While waiting for a sane way to do it, quick and dirty will do the job:

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

And yes, I am aware that there must be a better way to do it

Max_IT
  • 602
  • 5
  • 15