Skip to content

reproduce ggphylo example using ggtree

Guangchuang Yu edited this page Aug 31, 2015 · 7 revisions

ggphylo example

ggphylo supports plotting a list of phylo objects.

require(ape)
require(ggphylo)

tree.list = rmtree(3, 20)
ggphylo(tree.list)

![](https://raw.githubusercontent.com/GuangchuangYu/ygc_name/master/Bioconductor/ggtree/related_pkg/ggphylo/ggphylo.png)

AND plotting data along tree
```r
n <- 40;  x <- rtree(n); n.nodes <- length(nodes(x))
bootstraps <- 100 - rexp(n.nodes, rate=5) * 100
pop.sizes <- pmax(0, rnorm(n.nodes, mean=50000, sd=50000))
for (i in nodes(x)) {
x <- tree.set.tag(x, i, 'bootstrap', bootstraps[i])
x <- tree.set.tag(x, i, 'pop.size', pop.sizes[i]) }
plot.args <- list(
   x,
   line.color.by='bootstrap',
   line.color.scale=scale_colour_gradient(limits=c(50, 100), low='red', high='black'), 
                                      node.size.by='pop.size',
   node.size.scale = scale_size_continuous(limits=c(0, 100000), 
                    range=c(1, 5)), label.size=2
)
do.call(ggphylo, plot.args)

reproduce using ggtree

In ggtree, multiPhylo object is supported.

require(ggplot2)
require(ggtree)

ggtree(tree.list, ladderize=F, aes(color=.id)) + facet_wrap(~.id) + 
      geom_point() + geom_text(subset=.(!isTip), aes(label=node), hjust=-.2, size=3) + 
          geom_tiplab(size=4, hjust=-.2) 

ggtree defined fortify method to convert tree object to data.frame, user can operate this data.frame to add more information, and use ggplot with geom_tree that defined in ggtree to add tree layer.

df <- fortify(x, ladderize=F)
df$bootstrap <- bootstraps
df$pop.size <- pop.sizes
ggplot(df, aes(x, y, color=bootstrap)) + geom_tree() + geom_point(aes(size=pop.size)) +  
      scale_color_gradient(limits=c(50, 100), low="red", high="black") + 
          geom_tiplab(size=3, color="black", hjust=-.5)

Clone this wiki locally