ggtree icon indicating copy to clipboard operation
ggtree copied to clipboard

Width uniformization of collapsed triangles

Open sihellem opened this issue 4 years ago • 4 comments
trafficstars

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’

sihellem avatar May 31 '21 06:05 sihellem

what you want is actually the default behavior of collapse, see http://yulab-smu.top/treedata-book/chapter6.html#collapsing-and-expanding-clade

GuangchuangYu avatar Oct 01 '21 15:10 GuangchuangYu

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: figtree_collapsed

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")
ggtree_collapsed_node_364_623

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.

sihellem avatar Oct 15 '21 06:10 sihellem

@xiangpin add a parameter height = NULL to ggtree::collapse().

possible input:

  • NULL (default), do nothing.
  • rel object 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.

GuangchuangYu avatar Oct 15 '21 06:10 GuangchuangYu

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)

alephreish avatar Mar 16 '22 14:03 alephreish