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 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
5 15 5 0.564 t1 TRUE 3.11 5 2.82 180
6 18 6 0.663 t4 TRUE 2.51 8 2.18 288
7 19 7 0.607 t10 TRUE 2.62 9 2.31 324
8 19 8 0.696 t8 TRUE 2.71 10 2.36 360
9 17 9 0.975 t3 TRUE 2.68 7 2.19 252
10 16 10 0.683 t7 TRUE 1.66 6 1.32 216
11 11 11 0 NA FALSE 0 2.95 0 106.
12 11 12 0.807 NA FALSE 0.807 4.91 0.403 177.
13 12 13 0.364 NA FALSE 1.17 2.88 0.988 104.
14 13 14 0.397 NA FALSE 1.57 3.75 1.37 135
15 14 15 0.975 NA FALSE 2.54 4.5 2.06 162
16 12 16 0.175 NA FALSE 0.981 6.94 0.894 250.
17 16 17 0.723 NA FALSE 1.70 7.88 1.34 284.
18 17 18 0.144 NA FALSE 1.85 8.75 1.78 315
19 18 19 0.162 NA FALSE 2.01 9.5 1.93 342
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) plot a with tip label The expected coorinate is more like the middle of the plot. I really need help, please.