YuLab-SMU / ggtree

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

geom_taxalink error "as_string() Can't convert `NULL` to a string." #615

Open ammaraziz opened 1 month ago

ammaraziz commented 1 month ago

Prerequisites

Describe you issue

From the ggtree book example here:

library(ggtree)
library(treeio)

treetext = "(((ADH2:0.1[&&NHX:S=human], ADH1:0.11[&&NHX:S=human]):
0.05 [&&NHX:S=primates:D=Y:B=100],ADHY:
0.1[&&NHX:S=nematode],ADHX:0.12 [&&NHX:S=insect]):
0.1[&&NHX:S=metazoa:D=N],(ADH4:0.09[&&NHX:S=yeast],
ADH3:0.13[&&NHX:S=yeast], ADH2:0.12[&&NHX:S=yeast],
ADH1:0.11[&&NHX:S=yeast]):0.1[&&NHX:S=Fungi])[&&NHX:D=N];"

tree <- read.nhx(textConnection(treetext))
ggtree(tree) + geom_tiplab() + geom_taxalink(taxa1='A', taxa2='E')

Produces this error:

Error in `as_string()`:
! Can't convert `NULL` to a string.
Run `rlang::last_trace()` to see where the error occurred.
> rlang::last_trace()
<error/rlang_error>
Error in `as_string()`:
! Can't convert `NULL` to a string.
---
Backtrace:
    ▆
 1. └─ggplot2:::`+.gg`(...)
 2.   └─ggplot2:::add_ggplot(e1, e2, e2name)
 3.     ├─ggplot2::ggplot_add(object, p, objectname)
 4.     └─ggtree:::ggplot_add.taxalink(object, p, objectname)
 5.       ├─base::as.vector(object$data[[as_name(object$mapping$taxa1)]])
 6.       └─rlang::as_name(object$mapping$taxa1)
 7.         └─rlang::as_string(x)
Run rlang::last_trace(drop = FALSE) to see 2 hidden frames.

I have also tried using mapping:

dat <- data.frame(from=c("A", "F", "L"), 
                  to=c("E", "K", "M"), 
                  h=c(1, 1, 0.1), 
                  type=c("t1", "t2", "t3"), 
                  s=c(2, 1, 2))
p4 <- ggtree(tree, layout="inward_circular", xlim=c(150, 0)) +
    geom_taxalink(data=dat, 
                  mapping=aes(taxa1=from, 
                              taxa2=to, 
                              color=type, 
                              size=s), 
                  ncp=10,
                  offset=0.15) + 
    geom_tiplab(hjust=1) +
    scale_size_continuous(range=c(1,3))

Error:

Error in `ggplot_add()`:
! The taxa: A, F, L, E, K, M can not be found.

Newly installed R4.4 and latest ggtree.

Software Versions

> sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Ventura 13.5

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: X
tzcode source: internal

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

other attached packages:
[1] treeio_1.28.0 ggtree_3.12.0

loaded via a namespace (and not attached):
 [1] jsonlite_1.8.8     gtable_0.3.5       crayon_1.5.2       dplyr_1.1.4       
 [5] compiler_4.4.0     tidyselect_1.2.1   Rcpp_1.0.12        aplot_0.2.2       
 [9] gridGraphics_0.5-1 parallel_4.4.0     ggplotify_0.1.2    tidyr_1.3.1       
[13] ggfun_0.1.4        scales_1.3.0       fastmap_1.2.0      lattice_0.22-6    
[17] ggplot2_3.5.1      R6_2.5.1           patchwork_1.2.0    generics_0.1.3    
[21] tidytree_0.4.6     yulab.utils_0.1.4  tibble_3.2.1       munsell_0.5.1     
[25] pillar_1.9.0       rlang_1.1.3        utf8_1.2.4         cachem_1.1.0      
[29] fs_1.6.4           lazyeval_0.2.2     memoise_2.0.1      cli_3.6.2         
[33] magrittr_2.0.3     digest_0.6.35      grid_4.4.0         lifecycle_1.0.4   
[37] nlme_3.1-164       vctrs_0.6.5        glue_1.7.0         ape_5.8           
[41] fansi_1.0.6        colorspace_2.1-0   purrr_1.0.2        tools_4.4.0       
[45] pkgconfig_2.0.3   
ammaraziz commented 1 month ago

I am happy to roll back to an R/ggtree version that works, if you could provide guidance which to test?

xiangpin commented 1 month ago

The taxa1 and taxa2 should be the tip.label or node number of tree.

> tree
'treedata' S4 object'.

...@ phylo:

Phylogenetic tree with 8 tips and 4 internal nodes.

Tip labels:
  ADH2, ADH1, ADHY, ADHX, ADH4, ADH3, ...

Rooted; includes branch lengths.

with the following features available:
  'V1', 'B', 'D', 'S'.

# The associated data tibble abstraction: 12 × 7
# The 'node', 'label' and 'isTip' are from the phylo tree.
    node label isTip    V1     B D     S
   <int> <chr> <lgl> <dbl> <dbl> <chr> <chr>
 1     1 ADH2  TRUE     NA    NA NA    human
 2     2 ADH1  TRUE     NA    NA NA    human
 3     3 ADHY  TRUE     NA    NA NA    nematode
 4     4 ADHX  TRUE     NA    NA NA    insect
 5     5 ADH4  TRUE     NA    NA NA    yeast
 6     6 ADH3  TRUE     NA    NA NA    yeast
 7     7 ADH2  TRUE     NA    NA NA    yeast
 8     8 ADH1  TRUE     NA    NA NA    yeast
 9     9 NA    FALSE    NA    NA N     NA
10    10 NA    FALSE    NA    NA N     metazoa
# ℹ 2 more rows
# ℹ Use `print(n = ...)` to see more rows
ggtree(tree, layout='circluar') + geom_tiplab() + geom_taxalink(taxa1=2, taxa2=4, hratio=3)

ggtree(tree) + geom_tiplab() + geom_taxalink(taxa1='ADHY', taxa2='ADH3')

Please refer to the help information of geom_taxalink using ?geom_taxalink