YuLab-SMU / ggtree

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

`geom_tiplab2(geom="image",...)`: errors #566

Open bschilder opened 1 year ago

bschilder commented 1 year ago

Prerequisites

Describe you issue

geom_tiplab2 seems to work fine until I introduce some coloring parameters.

phylo_data.csv

library(ggtree)
set.seed(123)

d <- data.table::fread("https://github.com/YuLab-SMU/ggtree/files/11001046/phylo_data.csv")
tr <- rtree(nrow(d), tip.label = d$species)

## Works fine
ggtree(tr, layout = "circular" )%<+% d  +
    geom_tiplab2(geom="image",
                 ggplot2::aes(image=svg))

1942640b-8868-405e-b1c9-b6cc435b725e

## Works fine
ggtree(tr, layout = "circular" )%<+% d  +
    geom_tiplab2(geom="image",
                 ggplot2::aes(image=png))

bbea2f2f-e13a-4b8f-832a-53d1c6c724ae

## Technically works, but fills the background as well. Also produces some strange image distortion
ggtree(tr, layout = "circular",
       ggplot2::aes(color=one2one_orthologs), 
       continuous = 'colour')%<+% d  +
    geom_tiplab2(geom="image",
                 ggplot2::aes(image=svg))

plot_zoom_png?width=1149 height=813

## Error
ggtree(tr, layout = "circular",
       ggplot2::aes(color=one2one_orthologs), 
       continuous = 'colour')%<+% d  +
    geom_tiplab2(geom="image",
                 ggplot2::aes(image=png))

Not sure why this is happening.

Error in `label_geom()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 3rd layer.
Caused by error in `[<-`:
! subscript out of bounds
Run `rlang::last_trace()` to see where the error occurred.

Rerunning with rlang::last_trace()

Error in if (drop) { : the condition has length > 1
13.
trace_as_tree(trace, dir = dir, srcrefs = srcrefs, drop = drop)
12.
trace_format(x, max_frames, dir, srcrefs, drop = drop, ...)
11.
format.rlang_trace(trace, ..., simplify = simplify)
10.
format(trace, ..., simplify = simplify)
9.
paste_trace(out, pending_trace[["trace"]], simplify = simplify,
..., drop = drop)
8.
flush_trace()
7.
cnd_format(x, ..., backtrace = backtrace, simplify = simplify,
drop = drop)
6.
with_error_arg_highlight(out <- cnd_format(x, ..., backtrace = backtrace,
simplify = simplify, drop = drop))
5.
format.rlang_error(x, ...)
4.
format(x, ...)
3.
writeLines(format(x, ...))
2.
print.rlang_error(x)
1.
(function (x, ...)
UseMethod("print"))(x)

Dropping the tree tips not present in the data beforehand doesn't make a difference:

 valid_species <- intersect(d$species, tr$tip.label)
    d <- d[species %in% valid_species]
    dropped <- !tr$tip.label %in% valid_species
    tr <- ape::drop.tip(
        phy = tr,
        tip = tr$tip.label[dropped])

Ask in right place

Session info

``` R version 4.2.1 (2022-06-23) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Ventura 13.2.1 Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] ape_5.7-1 ggtree_3.6.2 loaded via a namespace (and not attached): [1] backports_1.4.1 fastmatch_1.1-3 igraph_1.4.1 lazyeval_0.2.2 [5] orthogene_1.4.1 usethis_2.1.6 ggplot2_3.4.1 digest_0.6.31 [9] foreach_1.5.2 yulab.utils_0.0.6 htmltools_0.5.4 magick_2.7.4 [13] fansi_1.0.4 magrittr_2.0.3 phytools_1.5-1 memoise_2.0.1 [17] optimParallel_1.0-2 grImport2_0.2-0 doParallel_1.0.17 remotes_2.4.2 [21] TreeTools_1.9.0 matrixStats_0.63.0 R.utils_2.12.2 prettyunits_1.1.1 [25] jpeg_0.1-10 colorspace_2.1-0 rbibutils_2.2.13 xfun_0.37 [29] dplyr_1.1.0 callr_3.7.3 crayon_1.5.2 RCurl_1.98-1.10 [33] jsonlite_1.8.4 graph_1.76.0 phangorn_2.11.1 iterators_1.0.14 [37] glue_1.6.2 gtable_0.3.1 DelayedArray_0.24.0 car_3.1-1 [41] R.cache_0.16.0 pkgbuild_1.4.0 BiocGenerics_0.44.0 maps_3.4.1 [45] abind_1.4-5 scales_1.2.1 DBI_1.1.3 rstatix_0.7.2 [49] miniUI_0.1.1.1 Rcpp_1.0.10 plotrix_3.8-2 viridisLite_0.4.1 [53] xtable_1.8-4 gridGraphics_0.5-1 tidytree_0.4.2 bit_4.0.5 [57] stats4_4.2.1 rsvg_2.4.0 profvis_0.3.7 htmlwidgets_1.6.1 [61] httr_1.4.5 RColorBrewer_1.1-3 ellipsis_0.3.2 farver_2.1.1 [65] urlchecker_1.0.1 pkgconfig_2.0.3 XML_3.99-0.13 rphylopic_1.0.0 [69] R.methodsS3_1.8.2 utf8_1.2.3 here_1.0.1 labeling_0.4.2 [73] ggplotify_0.1.0 tidyselect_1.2.0 rlang_1.1.0 later_1.3.0 [77] munsell_0.5.0 biocViews_1.66.3 tools_4.2.1 cachem_1.0.7 [81] cli_3.6.0 generics_0.1.3 broom_1.0.4 devtools_2.4.5 [85] evaluate_0.20 stringr_1.5.0 fastmap_1.1.1 yaml_2.3.7 [89] babelgene_22.9 processx_3.8.0 knitr_1.42 bit64_4.0.5 [93] fs_1.6.1 purrr_1.0.1 gprofiler2_0.2.1 RBGL_1.74.0 [97] nlme_3.1-162 mime_0.12 grr_0.9.5 R.oo_1.25.0 [101] aplot_0.1.10 compiler_4.2.1 rstudioapi_0.14 plotly_4.10.1 [105] curl_5.0.0 png_0.1-8 ggsignif_0.6.4 treeio_1.23.1 [109] clusterGeneration_1.3.7 tibble_3.2.0 ggimage_0.3.1 homologene_1.4.68.19.3.27 [113] stringi_1.7.12 ps_1.7.2 lattice_0.20-45 Matrix_1.5-3 [117] vctrs_0.6.0 stringdist_0.9.10 pillar_1.8.1 BiocCheck_1.34.3 [121] lifecycle_1.0.3 RUnit_0.4.32 BiocManager_1.30.20 combinat_0.0-8 [125] Rdpack_2.4 data.table_1.14.8 bitops_1.0-7 httpuv_1.6.9 [129] patchwork_1.1.2 R6_2.5.1 promises_1.2.0.1 IRanges_2.32.0 [133] sessioninfo_1.2.2 codetools_0.2-19 MASS_7.3-58.3 pkgload_1.3.2 [137] rprojroot_2.0.3 withr_2.5.0 mnormt_2.1.1 S4Vectors_0.36.2 [141] expm_0.999-7 parallel_4.2.1 devoptera_0.99.0 quadprog_1.5-8 [145] grid_4.2.1 ggfun_0.0.9 tidyr_1.3.0 coda_0.19-4 [149] rmarkdown_2.20.1 carData_3.0-5 MatrixGenerics_1.10.0 ggpubr_0.6.0 [153] numDeriv_2016.8-1.1 scatterplot3d_0.3-43 Biobase_2.58.0 shiny_1.7.4 [157] base64enc_0.1-3 ```
bschilder commented 1 year ago

Note, converting the input data to a data.frame with row names as the species seems to help slightly for the SVG example (though image filling and distortion still occurs):

d <- as.data.frame(d) |> `rownames<-`(d$species)

ggtree(tr, layout = "circular",
       ggplot2::aes(color=one2one_orthologs), 
       continuous = 'colour')%<+% d  +
    geom_tiplab2(geom="image",
                 ggplot2::aes(image=svg))

plot_zoom_png?width=1149 height=8132

But the same error still persists for PNGs:

## Error
ggtree(tr, layout = "circular",
       ggplot2::aes(color=one2one_orthologs))%<+% d  +
    geom_tiplab2(geom="image",
                 ggplot2::aes(image=png))
Error in `label_geom()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 3rd layer.
Caused by error in `[<-`:
! subscript out of bounds
Run `rlang::last_trace()` to see where the error occurred.
bschilder commented 1 year ago

@GuangchuangYu what is the status of this?