ggtree
ggtree copied to clipboard
Width uniformization of collapsed triangles
Hello,
I post this as a feature request since I did not see this possibility in available documentation.
Using ggtree::collapse, I collapse many clades as triangles, but I don't want their size to be proportional to the number of tips. Instead, I want all collapsed triangles to have the same size (roughly equivalent to the uniform font size of tip & node labels).
ggtree::scaleClade helps to achieve this, but is impractical when the operation has to be done on many collapsed nodes, each containing a different number of taxa.
> packageVersion("ggtree")
[1] ‘2.4.2’
what you want is actually the default behavior of collapse, see http://yulab-smu.top/treedata-book/chapter6.html#collapsing-and-expanding-clade
Thank you for your reply.
I actually fail to recognize the default behavior of collapse as making clades having uniform width.
Please find attached figures illustrating my query, with an example in FigTree which displays both clades as having the same size, as here:

Here is the code I used to collapse the tree in ggtree:
tree_start <- ggtree(beast.tree, ladderize = TRUE, right = TRUE)+geom_text(aes(label=node))
collapse(tree_start, 364, 'max', fill = "#D83588") %>%
collapse(623, 'max', fill = "#D83588")
It would of course be possible to obtain similar results by using ggtree::scaleClade, but is impractical when the operation has to be done on many collapsed nodes, each containing a different number of taxa.
@xiangpin add a parameter height = NULL to ggtree::collapse().
possible input:
- NULL (default), do nothing.
relobject by e.g.,rel(0.2), zoom out the height of the triangle to 20%- numeric, e.g., 1, force the total height to be 1.
This can be easily achieved with scaleClade by making the scale parameter dependent on 1 / (ntips - 1).
Simplified versions of my personal scaling and collapsing functions look as follows (both accept a vector of internal node numbers to collapse):
scaleMyClade <- function(.p, .node) {
offs <- offspring(.p$data, .node)
scaleClade(.p, .node, 1 / (nrow(offs) - 1))
}
collapseMyClade <- function(.p, .node) {
# optionally, specify label and fill color
collapse(.p, .node, "mixed")
}
p <- ggtree(tree)
p <- Reduce(scaleMyClade, nodes.to.collapse, p)
p <- Reduce(collapseMyClade, nodes.to.collapse, p)