ggtree
ggtree copied to clipboard
How to get the coordinate of the nodes of a circular tree by ggtree
I want to know how to get the node coordinates of the deformed ggtree. The coordinates obtained by ggtree::fortify() are valid in a square tree, but I don't know how to get coordinates for other tree points with shapes such as circular or slanted. I guess it's going to take some polar conversion. Here is my code to make tree:
library(ggtree)
tr <- rtree(10)
ggtree::fortify(tr)
ggtree(tr,layout = "fan")->a
The fortified data is not consistent with the plane cartesian coordinates of the points on the fan-shaped graph.
# A tibble: 19 × 9
parent node branch.length label isTip x y branch angle
<int> <int> <dbl> <chr> <lgl> <dbl> <dbl> <dbl> <dbl>
1 11 1 0.614 t6 TRUE 0.614 1 0.307 36
2 13 2 0.659 t9 TRUE 1.83 2 1.50 72
3 14 3 0.647 t5 TRUE 2.21 3 1.89 108
4 15 4 0.0817 t2 TRUE 2.62 4 2.58 144
I tried to parse the ggplot object directly, using ggplot2::ggplot_build()
Here is my code:
ggplot2::ggplot_build(a)[["data"]][[2]]
The output is, take node t6 (node 1 , parent11) as example:
$data[[2]]
node parent x y PANEL group xend yend colour linewidth linetype alpha
1 1 11 0.0000000 2.953125 1 -1 0.0000000 1.000000 black 0.5 1 NA

But t6 is definitely not anywhere near (0,2.95) or (0,1). The expected coorinate is more like the middle of the plot. I really need help, please.