briatte / ggnetwork

Geoms to plot networks with ggplot2
https://briatte.github.io/ggnetwork/
146 stars 27 forks source link

3D layouts #73

Closed bschilder closed 1 year ago

bschilder commented 1 year ago

Hello, thanks for the package!

I'm trying to generate a dataframe containing the coordinates of a 3D network layout. In the example below, when I specify dim=3, a new column "NA" is generated, which I assume the z-axis coordinate of each node. However, there is no corresponding "zend" column to specify where each edge/segment should end.

Do you know how I might return this information?

Reprex

g <- igraph::make_empty_graph(n = 5)
df <- ggnetwork::fortify(g,
                         layout = igraph::nicely(dim=3))
          x         y         NA      xend      yend
1 0.5162917 0.0000000  0.5002299 0.5162917 0.0000000
2 0.5753610 1.0000000  0.1739739 0.5753610 1.0000000
3 1.0000000 0.2408661 -1.1782594 1.0000000 0.2408661
4 0.0000000 0.3426721 -1.0646620 0.0000000 0.3426721
5 0.5387355 0.8545710 -1.7393077 0.5387355 0.8545710

Best, Brian

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] HPOExplorer_0.99.4 plotly_4.10.1 ggimage_0.3.1 ggplot2_3.4.1 [5] dplyr_1.1.0 hexSticker_0.4.9 loaded via a namespace (and not attached): [1] network_1.18.1 tidyselect_1.2.0 xfun_0.37 sna_2.7-1 [5] purrr_1.0.1 lattice_0.20-45 ggfun_0.0.9 colorspace_2.1-0 [9] vctrs_0.5.2 generics_0.1.3 htmltools_0.5.4 viridisLite_0.4.1 [13] yaml_2.3.7 utf8_1.2.3 gridGraphics_0.5-1 rlang_1.0.6 [17] hexbin_1.28.2 pillar_1.8.1 glue_1.6.2 withr_2.5.0 [21] ggnetwork_0.5.10 lifecycle_1.0.3 stringr_1.5.0 munsell_0.5.0 [25] gtable_0.3.1 htmlwidgets_1.6.1 evaluate_0.20 coda_0.19-4 [29] knitr_1.42 fastmap_1.1.0 crosstalk_1.2.0 fansi_1.0.4 [33] ontologyIndex_2.10 Rcpp_1.0.10 scales_1.2.1 showtext_0.9-5 [37] desc_1.4.2 pkgload_1.3.2 magick_2.7.3 jsonlite_1.8.4 [41] sysfonts_0.8.8 farver_2.1.1 digest_0.6.31 stringi_1.7.12 [45] showtextdb_3.0 rprojroot_2.0.3 grid_4.2.1 cli_3.6.0 [49] tools_4.2.1 yulab.utils_0.0.6 magrittr_2.0.3 lazyeval_0.2.2 [53] tibble_3.1.8 tidyr_1.3.0 pkgconfig_2.0.3 ellipsis_0.3.2 [57] data.table_1.14.8 ggplotify_0.1.0 rmarkdown_2.20.1 httr_1.4.4 [61] rstudioapi_0.14 statnet.common_4.8.0 R6_2.5.1 igraph_1.4.0 [65] compiler_4.2.1 ```
briatte commented 1 year ago

Hi Brian -- I must admit that I never planned to support 3D networks with this package. Perhaps have a look at the more advanced tidygraph and ggraph packages, in case they do support a z-dimension?

bschilder commented 1 year ago

Thanks, @briatte .

ggraph

As far as I can tell (I've been testing it quite a bit lately), ggraph doesn't support 3D plots. It also doesn't convert to plotly objects very well via plotly::ggplotly().

tidygraph

tidygraph may very well be useful for preparing 3D graph objects, but doesn't have any plotting functions itself (to my knowledge).

HPOExplorer

Instead, I've implemented some functions within HPOExplorer to facilitate 3D interactive network visualization using plotly as the underlying engine. https://neurogenomics.github.io/HPOExplorer/reference/network_3d.html

This function is designed for the specific use case of Human Phenotype Ontology data, but some of the internal functions like igraph_to_plotly may nevertheless be instructive for users who faced an issue similar to mine: https://github.com/neurogenomics/HPOExplorer/blob/master/R/igraph_to_plotly.R

briatte commented 1 year ago

You're right, Plotly does seem like the way to go!

bschilder commented 1 year ago

You're right, Plotly does seem like the way to go!

Indeed! Though note that atm there's a bit more support for graph objects in the python implementation of plotly (the docs in the link you provided). For the R implementation, some preprocessing to convert igraph objects to a data.frame is required.