| Title: | Tree Layout Algorithms for Phylogenetic Trees and Other Tree-Like Structures |
|---|---|
| Description: | Computes node coordinates for phylogenetic trees and other tree-like structures. Given a tree, returns one row per node with x and y coordinates, ready for plotting by any graphics system. This is the layout engine used by the 'ggtree' package, extracted so that the coordinate computation can be used without a plotting dependency. |
| Authors: | Guangchuang Yu [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-6485-8781>) |
| Maintainer: | Guangchuang Yu <[email protected]> |
| License: | Artistic-2.0 |
| Version: | 0.1.0 |
| Built: | 2026-11-12 00:00:54 UTC |
| Source: | https://github.com/cran/treelayout |
Adds an angle column holding the label angle for every node, derived from
the node's position and its parent.
calculate_angle(data)calculate_angle(data)
data |
a node table with |
the node table with an angle column added.
d <- tree_layout(ape::rtree(6), layout = "circular") calculate_angle(d)d <- tree_layout(ape::rtree(6), layout = "circular") calculate_angle(d)
Adds branch (the mid-point x), and for the unrooted layouts also
branch.x and branch.y, which place labels on the middle of a branch
even when the branch is not axis-aligned.
calculate_branch_mid(res, layout)calculate_branch_mid(res, layout)
res |
a node table with |
layout |
the layout name the coordinates came from; |
the node table with the branch columns added.
tr <- ape::rtree(6) d <- tree_layout(tr, layout = "equal_angle") calculate_branch_mid(d, layout = "equal_angle")tr <- ape::rtree(6) d <- tree_layout(tr, layout = "equal_angle") calculate_branch_mid(d, layout = "equal_angle")
node to any node inside
the clade, and the largest distance from the root of the whole tree to
any node – the two radii the clade label / hilight code needs.One call instead of three. Returns the left and right boundary angles of
the clade subtree, the largest distance from node to any node inside
the clade, and the largest distance from the root of the whole tree to
any node – the two radii the clade label / hilight code needs.
clade_geometry(data, node)clade_geometry(data, node)
data |
a node table with columns |
node |
the root node id of the clade. |
a list with left, right, radius and root_radius.
tr <- ape::rtree(10) d <- tree_layout(tr, layout = "circular") clade_geometry(d, node = 12)tr <- ape::rtree(10) d <- tree_layout(tr, layout = "circular") clade_geometry(d, node = 12)
convert Date to decimal format, eg "2014-05-05" to "2014.34"
Date2decimal(x)Date2decimal(x)
x |
a Date, or a numeric that is passed through unchanged. |
numeric year with fraction, e.g. 2014.34.
Guangchuang Yu
Date2decimal(as.Date("2014-05-05"))Date2decimal(as.Date("2014-05-05"))
convert decimal format to Date, eg "2014.34" to "2014-05-05"
decimal2Date(x)decimal2Date(x)
x |
numeric year with fraction, e.g. 2014.34. |
a Date.
Guangchuang Yu
decimal2Date(2014.34)decimal2Date(2014.34)
Read the layout recorded on a layout result
get_layout(x)get_layout(x)
x |
a table returned by |
the layout name, or NULL.
tr <- ape::rtree(10) get_layout(tree_layout(tr, layout = "circular"))tr <- ape::rtree(10) get_layout(tree_layout(tr, layout = "circular"))
Computes the x coordinate of every node by walking the edge list from a starting root position. Used by code that recomputes horizontal placement after changing the tree order.
getXcoord2(x, root, parent, child, len, start = 0, rev = FALSE)getXcoord2(x, root, parent, child, len, start = 0, rev = FALSE)
x |
numeric vector indexed by node id; the first entry is overwritten
with |
root |
node id to start from. |
parent |
parent node ids of the edge list. |
child |
child node ids of the edge list. |
len |
branch length of every edge. |
start |
x coordinate assigned to the root, default 0. |
rev |
logical; if TRUE the tree is walked in reverse, so coordinates decrease from the root. |
the x vector with one coordinate per node.
tr <- ape::rtree(6) x <- numeric(max(tr$edge)) getXcoord2(x, root = ape::Ntip(tr) + 1L, parent = tr$edge[, 1], child = tr$edge[, 2], len = tr$edge.length)tr <- ape::rtree(6) x <- numeric(max(tr$edge)) getXcoord2(x, root = ape::Ntip(tr) + 1L, parent = tr$edge[, 1], child = tr$edge[, 2], len = tr$edge.length)
Places tips on evenly spaced y positions (or in the order given by
tip.order) and fills the internal nodes bottom-up with the mean of their
children.
getYcoord(tr, step = 1, tip.order = NULL)getYcoord(tr, step = 1, tip.order = NULL)
tr |
a |
step |
spacing between adjacent tips, default 1. |
tip.order |
tip labels in the order they should appear bottom to top;
|
numeric vector of y coordinates, indexed like the nodes of tr.
tr <- ape::rtree(6) getYcoord(tr) getYcoord(tr, tip.order = rev(tr$tip.label))tr <- ape::rtree(6) getYcoord(tr) getYcoord(tr, tip.order = rev(tr$tip.label))
tree_layout()
Layout names accepted by tree_layout()
layout_names()layout_names()
a character vector of layout names.
layout_names()layout_names()
Fills in missing y values bottom-up: every node whose y is NA gets the
mean of its children's y values. Rows with a y value already present are
left untouched.
re_assign_ycoord_df(df, currentNode)re_assign_ycoord_df(df, currentNode)
df |
a node table with |
currentNode |
node ids to start the fill from, usually the tips. |
the node table with the missing y values replaced.
d <- tree_layout(ape::rtree(6)) d$y[c(2, 3)] <- NA re_assign_ycoord_df(d, currentNode = d$node[d$isTip])d <- tree_layout(ape::rtree(6)) d$y[c(2, 3)] <- NA re_assign_ycoord_df(d, currentNode = d$node[d$isTip])
Turn a tree into a table of node coordinates, one row per node. This is the layout engine behind 'ggtree', extracted so the coordinates can be computed without any plotting dependency.
tree_layout( model, data = NULL, layout = "rectangular", ladderize = TRUE, right = FALSE, branch.length = "branch.length", mrsd = NULL, as.Date = FALSE, yscale = "none", root.position = 0, hang = 0.1, ... )tree_layout( model, data = NULL, layout = "rectangular", ladderize = TRUE, right = FALSE, branch.length = "branch.length", mrsd = NULL, as.Date = FALSE, yscale = "none", root.position = 0, hang = 0.1, ... )
model |
a tree object: |
data |
unused; kept so the signature matches |
layout |
layout name or a function
taking an |
ladderize |
logical, ladderize the tree before layout. |
right |
logical, ladderize to the right. |
branch.length |
variable used to scale branch lengths; |
mrsd |
most recent sampling date, for time-scaled trees. |
as.Date |
logical, whether |
yscale |
variable used to scale tips along y; |
root.position |
x position of the root. |
hang |
fraction of the tree height by which labels hang below the rest
of the tree, for |
... |
passed on to the layout function. |
A tbl_df with one row per node and columns node, parent,
x, y, branch, angle, isTip and label, plus any columns carried
over from treedata. For a multiPhylo input the results are stacked and
an .id column identifies the tree. The attribute "layout" records the
layout used ("external" when layout was a function).
tr <- ape::rtree(10) head(tree_layout(tr)) head(tree_layout(tr, layout = "circular"))tr <- ape::rtree(10) head(tree_layout(tr)) head(tree_layout(tr, layout = "circular"))