'Is it possible to use geom_insert() in ggtree for circular trees?

I am trying to to include my results of ancestral state estimation as pie charts at the nodes of a phylogenetic tree. It works fine for rectangular trees but I would really prefer a circular one. When I try that though, I get an error message:

Error: annotation_custom only works with Cartesian coordinates

Reproducible example:

library(phytools)
library(tidyverse)
library(ggtree)
data("anoletree")

x <- getStates(anoletree,"tips")
cols_x <- setNames(palette()[1:length(unique(x))],sort(unique(x)))
fitER <- ape::ace(x,anoletree,model="ER",type="discrete")
ancstats_x <- as.data.frame(fitER$lik.anc)
ancstats_x$node <- 1:anoletree$Nnode+Ntip(anoletree)
tree2 <- full_join(anoletree, data.frame(label = names(x), stat = x ), by = 'label')
p_x <- ggtree(tree2, layout = "circular") + geom_tiplab() +
  geom_tippoint(aes(color = stat)) + 
  scale_color_manual(values = cols_x) +
  theme(legend.position = "right") + 
  xlim(NA, 8)
pies_x <- nodepie(ancstats_x, cols = 1:6)
pies_x <- lapply(pies_x, function(g) g+scale_fill_manual(values = cols_x))
p_x2 <- p_x + geom_inset(pies_x, width = .1, height = .1) 

Is there a possibility to circumvent this error? Any help would be greatly appreciated



Solution 1:[1]

This is because annotation_custom of ggplot doesn't work well with circular graphs. You can refer to https://github.com/YuLab-SMU/ggtree/issues/419 for the exact guide and how-to!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Erfan Shekarriz