Extracting and working with subtrees using ape can be executed using the following steps:
- Load the ape library and then load the tree:
library(ape)
newick <-read.tree(file.path(getwd(), "datasets", "ch4", "mammal_tree.nwk"))
- Get a list of all of the subtrees:
l <- subtrees(newick)
plot(newick)
plot(l[[4]], sub = "Node 4")
- Extract a specific subtree:
small_tree <- extract.clade(newick, 9)
- Combine two trees:
new_tree <- bind.tree(newick, small_tree, 3)
plot(new_tree)