rggtree

ggtree: highlighting clades by tip labels


Is it possible to highlight clade by tip labels in ggtree (A_ and B_ in the example below)? Finding node numbers is really annoying especially when you have more than 1000 tips.

set.seed(2015-12-21)
tree <- rtree(30)
tree$tip.label <- c(paste0(rep("A_", 15), rep(1:15)), paste0(rep("B_", 14), rep(1:14)), "C_1")
ggtree(tree) + geom_highlight(node = 33) + geom_tiplab() + xlim(NA, 8)

enter image description here


Solution

  • specify id's first

    id <- c("A_", "B_", "C_")
    

    then use the ape package to find MRCA nodes

    parent_nodes <- sapply(id, function(x) ape::getMRCA(tree, tree$tip.label[str_detect(tree$tip.label, x)]))
    parent_nodes
    $A_
    [1] 33
    
    $B_
    [1] 47
    
    $C_
    NULL
    

    and use them to highligth clades:

    ggtree(tree) + geom_highlight(node = parent_nodes[1:2]) + geom_tiplab() + xlim(NA, 8)
    

    enter image description here