YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/contribution-tree-data/
839 stars 173 forks source link

`geom_tiplab2()` is broken with custom `coord_polar()` settings #638

Open willgearty opened 2 days ago

willgearty commented 2 days ago

My understanding is that specifying ggtree(..., layout="circular") is essentially the same as this:

library(ggplot2)
library(ggtree)
library(ape)
data("chiroptera")
ggtree(chiroptera) +
    coord_polar(theta = "y", start = -pi/2, direction = -1) +
    scale_y_continuous(limits = c(0, NA), expand = expansion(0, 0.6))

Created on 2024-10-24 with reprex v2.1.1

Based on the code here: https://github.com/YuLab-SMU/ggtree/blob/879854353e3dc8eb02ddda13c536b94c05250901/R/method-ggplot-add.R#L106-L114

However, this hardcodes the starting and ending points of the phylogeny, which I would prefer to have on the left. So I've been playing around with changing the start and direction arguments in coord_polar(), which seems to work fine, until you try to add tip labels. For any values other than the defaults, geom_tiplab2() breaks. Here are some interesting examples:

library(ggplot2)
library(ggtree)
library(ape)
data("chiroptera")
ggtree(chiroptera) +
    coord_polar(theta = "y", start = pi/2, direction = -1) +
    scale_y_continuous(limits = c(0, NA), expand = expansion(0, 0.6)) +
    geom_tiplab2(size = 0.8)

Created on 2024-10-24 with reprex v2.1.1

library(ggplot2)
library(ggtree)
library(ape)
data("chiroptera")
ggtree(chiroptera) +
    coord_polar(theta = "y", start = -pi/2, direction = 1) +
    scale_y_continuous(limits = c(0, NA), expand = expansion(0, 0.6)) +
    geom_tiplab2(size = 0.8)

Created on 2024-10-24 with reprex v2.1.1

willgearty commented 2 days ago

Note that I discovered this after an issue was raised for my deeptime package: https://github.com/willgearty/deeptime/issues/72