Skip to content

Instantly share code, notes, and snippets.

@cmt2
Created May 31, 2024 23:26
Show Gist options
  • Save cmt2/c4af02165cc55a4588a34642f8da4030 to your computer and use it in GitHub Desktop.
Save cmt2/c4af02165cc55a4588a34642f8da4030 to your computer and use it in GitHub Desktop.
format a standard phylo object for use in RevGadgets plotting functions
# randomly sample a tree for this example
t <- rtree(10)
# check that the example tree is a phylo object
class(t) == "phylo"
# convert to RevGadgets plotting format
t_td <- convertTreeFormat(t) # [note, run the function code below before executing this line of code]
# The function:
# `tree` is the argument for your phylo object.
# `format` should be either 1 or 2, where 1
# products a list of treedata objects and 2
# produces a list of lists of treedata objects.
# The appropriate format will depend on which
# downstream plotting functions you wish to use.
convertTreeFormat <- function(tree, format = 2){
td <- list(tidytree::as.treedata(tree))
if (format == 1){
return(td)
} else if (format == 2){
return(list(td))
} else {
simpleError("Format arg should be either 1 or 2")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment