YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/treedata-book/
821 stars 172 forks source link

Error with geom_inset of nodepies onto circular/radial/fan trees #599

Open ruthvikpsi opened 6 months ago

ruthvikpsi commented 6 months ago

Hi ggtree team,

I have been trying to plot discrete ancestral states at nodes with pie charts created using nodepie(). It works completely fine with the default layout. However, my original tree is large (>3000 tips) and I would like to represent the tree in a circular/fan layout with pie charts on the nodes. But, I get this error with the layouts "circular", "fan" and "radial":

Error in annotation_custom(): ! Problem while converting geom to grob. ℹ Error occurred in the 6th layer. Caused by error in draw_panel(): ! annotation_custom() only works with coord_cartesian()

Here's a reproducible example from the example dataset anoletree data:

library(ggtree)
library(dplyr)
library(phytools)

data(anoletree)
x <- getStates(anoletree,"tips")
tree <- as.phylo(anoletree)

cols <- setNames(palette()[1:length(unique(x))],sort(unique(x)))
fitER <- ape::ace(x,tree,model="ER",type="discrete")
ancstats <- as.data.frame(fitER$lik.anc)
ancstats$node <- 1:tree$Nnode+Ntip(tree)

tree2 <- full_join(tree, data.frame(label = names(x), stat = x ), by = 'label')
p <- ggtree(tree2, layout = "circular") + geom_tiplab() +
  geom_tippoint(aes(color = stat)) + 
  scale_color_manual(values = cols) +
  theme(legend.position = "right") + 
  xlim(NA, 8)

pies <- nodepie(ancstats, cols = 1:6)
pies <- lapply(pies, function(g) g+scale_fill_manual(values = cols))
p2 <- p + geom_inset(pies, width = .1, height = .1) 
p2

Session Info:

R version 4.3.2 (2023-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale: [1] LC_COLLATE=English_United Kingdom.utf8 LC_CTYPE=English_United Kingdom.utf8
[3] LC_MONETARY=English_United Kingdom.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.utf8

time zone: Europe/London tzcode source: internal

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] phytools_2.1-1 maps_3.4.2 ape_5.7-1 dplyr_1.1.4 ggtree_3.10.0