Better plotting of tree object - r

I am trying to find a better way to plot a "tree" object from the tree package in R. I realize that there are good plotting tools for rpart package trees, but I am creating my tree object from scratch, therefore a "tree" object from the tree package is better suited for my purposes.
My question is, does anyone know of a better way to plot "tree" objects than plot?
I am trying to include the distribution of values, square blocks for the leaf nodes, majority class, etc.
I could've sworn there was a package that could take a "tree" object as the argument but I can't seem to find it.
This is for a classification tree.

Related

Way to Make a Better Looking Dendrogram than in Seurat?

I'm trying to make a hierarchical clustering tree from the clustering produced by Seurat's clustering function. Their functions, BuildClusterTree and PlotClusterTree, produce a frankly ugly tree based on SNN (shared nearest neighbor) algorithm that you can't manipulate with ggplot2. I'm trying to figure out how to use other functions to plot the clustering already produced by Seurat, but I can't figure out how or what R package would work best. Does anyone have any advice for me?
I would suggest the clustree package for this purpose

Plot igraph tree objects with ggtree

Generating trees as a subclass of graphs with the igraph package is the de-facto standard in R.
The package ggtree is very versatile in tree visualization. It seems some plotting functionalities go beyond the capabilties of igraph.
This leads to the question:
Is there a way to use a valid tree graph object generated by igraph package (i.e. example below) as input for a visualization with ggtree?
library(igraph)
g <- graph.tree(20, 2)
This is a good idea.
ggtree is designed for phylogenetic analysis. Some of the functionalities may not be applied to other objects like igraph directly. To make the support more smoothly is to convert the igraph object as a phylo object. So that after conversion, ggtree can be used to visualize it and all functionalities are supported.
The issue for conversion is that igraph allow singleton as in the example posted while phylo don't allow since it's meaningless in evolution.
I will consider to develop a conversion function in future version.
reference
G Yu, DK Smith, H Zhu, Y Guan, TTY Lam*. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution. doi:10.1111/2041-210X.12628.

How to traverse the tree structure of rpart object in R? I need to get all the nodes associated with a subtree, how can I do it?

I am using rpart for building a decision tree classifier. I wish to use my own pruning function based on certain parameters of the leaf nodes corresponding to a subtree. For this, I think I need to traverse the tree. I explored the documentation of rpart, but could not come up with a way to do it. Please help.
Take a look at the tree traversal code I had at extracting predictors from ctree object
Potentially, this will be useful.

decision trees with forced structure

I have been using decision trees (CART) in R using the rpart package to look at the relationship between SST (predictor variables) and climate (predictand variable).
I would like to "force" the tree into a particular structure - i.e. split on predictor variable 1, then on variable 2.
I've been using R for a while so I thought I'd be able to look at the code behind the rpart function and modify it to search for 'best splits' in a particular predictor variable first. However the rpart function calls C routines and not having any experience with C I get lost here...
I could write a function from scratch but would like to avoid it if possible! So my questions are:
Is there another decision tree technique (implemented in R
preferably) in which you can force the structure of the tree?
If not - is there some way I could convert the C code to R?
Any other ideas?
Thanks in advance, and help is much appreciated.
When your data indicates a tree with a known structure, present that structure to R using either a newick or nexus file format. Then you can read in the structure using either read.tree or read.nexus from Package Phylo.
Maybe you should look at the method formal parameter of rpart
In the documentation :
... ‘method’ can be a list of functions named ‘init’, ‘split’ and ‘eval’. Examples are given in the file ‘tests/usersplits.R’ in the sources.

Normalizing a Phylogenetic Tree in R

When working with phylogenetic tree data in R (specifically when working with "phylo" or "phylo4" objects) it would be useful to normalize branch lengths so that certain taxa (the ones that evolve faster) do not contribute a disproportionate amount of branch length to the tree. This seems to be common in computing UniFrac values, as can be found in the discussion here: http://bmf2.colorado.edu/unifrac/help.psp. (I need more than just UniFrac values, however).
However, I cannot find a function that performs this normalization step. I have looked in ape, picante, adephylo, and phylobase. Could someone direct me to a package that includes this function, or a package that makes writing this kind of function straightforward?
Are you looking for a function to just scale the branch lengths of a tree? If so, compute.brlen() in ape will do it. There are built in options for Grafen's rho and all = 1. You can also supply your own function.
I don't know if UniFrac does some other kind of branch length scaling. But if so, you could write your function and pass it.

Resources