YuLab-SMU / ggtree

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

cant combine ggtree with nodepie inset and geological bars from deeptime #419

Closed oscarIM closed 3 years ago

oscarIM commented 3 years ago

I am trying to combine a ggtree with node pie inset with geologic bars from the deeptime package. For this, I first create the tree with the nodepie and then add the geological bars. This gives the error : Error: annotation_custom only works with Cartesian coordinates (above minimal example):


library(ape)
library(phytools)
library(ggtree)
library(deeptime)
set.seed(12345)
tree <- sim.bdtree(b = 0.1, d = 0, stop = "time", t = 20, seed = 12345)
#sim trait
trait <- rTraitDisc(tree)
trait_anc <- data.frame(rerootingMethod(tree, trait)$marginal.anc)
trait_anc$node <- row.names(trait_anc)
#tree with pie
p <- ggtree(tree)
pies <- nodepie(trait_anc, cols = 1:2, color = c("darkorange1","blue"), alpha = 0.8)
p_pie <- inset(p, pies, width = 0.07, height = 0.07)
#tree with pie
p_pie_time <- p_pie +  coord_geo()#just default`
xiangpin commented 3 years ago

This is because the annotation_custom of ggplot2 is not compatible with the other coord. However, you can use geom_plot of ggpp to display the example. You can refer to the following codes.

> library(ape)
> library(phytools)
Loading required package: maps
> library(ggtree)
ggtree v3.1.2  For help: https://yulab-smu.top/treedata-book/

If you use ggtree in published research, please cite the most appropriate paper(s):

1. Guangchuang Yu. Using ggtree to visualize data on tree-like structures. Current Protocols in Bioinformatics, 2020, 69:e96. doi:10.1002/cpbi.96
2. Guangchuang Yu, Tommy Tsan-Yuk Lam, Huachen Zhu, Yi Guan. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution 2018, 35(12):3041-3043. doi:10.1093/molbev/msy194
3. Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution 2017, 8(1):28-36. doi:10.1111/2041-210X.12628

> library(deeptime)
> library(geiger)
> set.seed(12345)
> tree <- sim.bdtree(b = 0.1, d = 0, stop = "time", t = 20, seed = 12345)
> trait <- rTraitDisc(tree)
> trait_anc <- data.frame(rerootingMethod(tree, trait)$marginal.anc)
> trait_anc$node <- row.names(trait_anc)
> pies <- nodepie(trait_anc, cols = 1:2, color = c("darkorange1","blue"), alpha = 0.8)
> df <- tibble::tibble(node=as.numeric(trait_anc$node), pies=pies)
> p1 <- ggtree(tree)
> p2 <- p1 %<+% df
> library(ggpp)
Loading required package: ggplot2
> p2 + geom_plot(data=td_filter(!isTip), mapping=aes(x=x,y=y, label=pies), vp.width=0.09, vp.height=0.09, hjust=0.5, vjust=0.5) + coord_geo()

ggpies

oscarIM commented 3 years ago

thank you very much for your help! Best

bioyear commented 2 years ago

Thanks for this question and kindly reply, I have got a lot help from that. But it comes a new question that when I figured out two traits in a single tree plot, in other words when I use geom_plot twice, the second pie layer didn't work. So, how can I deal this question? Thank you again!