rtree: Generate Random Trees

View source: R/rtree.R

rtreeR Documentation

Generate Random Trees


These functions generate trees by splitting randomly the edges (rtree and rtopology) or randomly clustering the tips (rcoal). rtree and rtopology generate general trees, and rcoal generates coalescent trees. The algorithms are described in Paradis (2012) and in a vignette in this package.


rtree(n, rooted = TRUE, tip.label = NULL, br = runif, equiprob = FALSE, ...)
rtopology(n, rooted = FALSE, tip.label = NULL, br = runif, ...)
rcoal(n, tip.label = NULL, br = "coalescent", ...)
rmtree(N, n, rooted = TRUE, tip.label = NULL, br = runif,
       equiprob = FALSE, ...)
rmtopology(N, n, rooted = FALSE, tip.label = NULL, br = runif, ...)



an integer giving the number of tips in the tree.


a logical indicating whether the tree should be rooted (the default).


a character vector giving the tip labels; if not specified, the tips "t1", "t2", ..., are given.


one of the following: (i) an R function used to generate the branch lengths (rtree; use NULL to simulate only a topology), or the coalescence times (rcoal); (ii) a character to simulate a genuine coalescent tree for rcoal (the default); or (iii) a numeric vector for the branch lengths or the coalescence times.


(new since ape 5.4-1) a logical specifying whether topologies are generated in equal frequencies. If, FALSE, the unbalanced topologies are generated in higher proportions than the balanced ones.


further argument(s) to be passed to br.


an integer giving the number of trees to generate.


The trees generated are bifurcating. If rooted = FALSE in (rtree), the tree is trifurcating at its root.

The option equiprob = TRUE generates unlabelled topologies in equal frequencies. This is more complicated for the labelled topologies (see the vignette “RandomTopologies”).

The default function to generate branch lengths in rtree is runif. If further arguments are passed to br, they need to be tagged (e.g., min = 0, max = 10).

rmtree calls successively rtree and set the class of the returned object appropriately.


An object of class "phylo" or of class "multiPhylo" in the case of rmtree or rmtopology.


Emmanuel Paradis


Paradis, E. (2012) Analysis of Phylogenetics and Evolution with R (Second Edition). New York: Springer.

See Also

stree, rlineage, vignette “RandomTopologies”.


layout(matrix(1:9, 3, 3))
### Nine random trees:
for (i in 1:9) plot(rtree(20))
### Nine random cladograms:
for (i in 1:9) plot(rtree(20, FALSE), type = "c")
### generate 4 random trees of bird orders:
layout(matrix(1:4, 2, 2))
for (i in 1:4)
  plot(rcoal(23, tip.label = bird.orders$tip.label), no.margin = TRUE)
par(mar = c(5, 4, 4, 2))

ape documentation built on March 31, 2023, 6:56 p.m.