Skip to content

Error in graph_to_tree(graph, mode = direction) : Graph must be directed #247

Closed

Description

@rbenel

Hi,
I have an un-directed graph which produces an image with a single central node and many vertices. The code I used two weeks ago no longer produces that image and instead I receive the error "Error in graph_to_tree(graph, mode = direction) : Graph must be directed".

When using plot from igraph I receive the same image that I previously received using ggraph

I updated ggraph and I still nothing, any ideas?

`> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] viridis_0.5.1 viridisLite_0.3.0 ggrepel_0.8.2 ggraph_2.0.3 igraph_1.2.5 corrr_0.4.2
[7] forcats_0.5.0 stringr_1.4.0 purrr_0.3.4 readr_1.3.1 tibble_3.0.1 tidyverse_1.3.0
[13] ggplot2_3.3.1 data.table_1.12.8 reshape_0.8.8 pheatmap_1.0.12 tidyr_1.1.0 dplyr_1.0.0

loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 lubridate_1.7.8 lattice_0.20-41 assertthat_0.2.1 digest_0.6.25
[6] ggforce_0.3.1 R6_2.4.1 cellranger_1.1.0 plyr_1.8.6 backports_1.1.7
[11] reprex_0.3.0 httr_1.4.1 pillar_1.4.4 rlang_0.4.6 readxl_1.3.1
[16] rstudioapi_0.11 blob_1.2.1 Matrix_1.2-18 BiocParallel_1.18.1 polyclip_1.10-0
[21] munsell_0.5.0 broom_0.5.6 compiler_3.6.3 modelr_0.1.8 pkgconfig_2.0.3
[26] tidyselect_1.1.0 gridExtra_2.3 graphlayouts_0.7.0 fansi_0.4.1 crayon_1.3.4
[31] dbplyr_1.4.4 withr_2.2.0 MASS_7.3-51.6 grid_3.6.3 nlme_3.1-147
[36] jsonlite_1.6.1 gtable_0.3.0 lifecycle_0.2.0 DBI_1.1.0 magrittr_1.5
[41] scales_1.1.1 cli_2.0.2 stringi_1.4.6 farver_2.0.3 fs_1.4.1
[46] xml2_1.3.2 ellipsis_0.3.1 generics_0.0.2 vctrs_0.3.0 RColorBrewer_1.1-2
[51] tools_3.6.3 glue_1.4.1 tweenr_1.0.1 hms_0.5.3 parallel_3.6.3
[56] yaml_2.2.1 colorspace_1.4-1 tidygraph_1.2.0 rvest_0.3.5 haven_2.3.1`

Activity

  1. deziahuja commented on Jul 10, 2020

    @deziahuja

    I'm encountering the same error message when I work with my undirected, egocentric networks. Has there been any work towards resolving this issue?

  2. rbenel commented on Jul 13, 2020

    @rbenel
    Author

    I used igraph instead, as the error didn't seem to be fixed.

  3. beanumber commented on Sep 23, 2020

    @beanumber

    FYI, I was able to get past this by specifying the layout. So instead of

    ggraph(g)
    

    use

    ggraph(g, 'tree')
    
  4. thomasp85 commented on Nov 12, 2020

    @thomasp85
    Owner

    if anyone can provide a reprex I'd be happy to look into it

  5. demiliosaldana commented on Jan 4, 2021

    @demiliosaldana

    FYI, I was able to get past this by specifying the layout. So instead of

    ggraph(g)
    

    use

    ggraph(g, 'tree')
    

    Hi @beanumber! I´m wondering if you could tell me where did you find information about "mode" in the "ggraph" function, in order to avoid the Error. I´ve been searching about this "modes", but I couldn´t find anything related it.
    Thank you Ben! Have a good one!

    Emilio

  6. beanumber commented on Jan 5, 2021

    @beanumber

    @demiliosaldana: mode is an argument to the internal function graph_to_tree().

    ggraph:::graph_to_tree
    #> function (graph, mode) 
    #> {
    #>     if (!is.directed(graph)) {
    #>         stop("Graph must be directed")
    #>     }
    #>     graph <- simplify(graph, edge.attr.comb = "first")
    #>     parent_dir <- if (mode == "out") 
    #>         "in"
    #>     else "out"
    #>     comp <- components(graph, "weak")
    #>     graphs <- lapply(seq_len(comp$no), function(i) {
    #>         graph <- induced_subgraph(graph, which(comp$membership == 
    #>             i))
    #>         n_parents <- degree(graph, mode = parent_dir)
    #>         if (!any(n_parents == 0)) {
    #>             stop("No root in graph. Provide graph with one parentless node")
    #>         }
    #>         if (any(n_parents > 1)) {
    #>             message("Multiple parents. Unfolding graph")
    #>             root <- which(degree(graph, mode = parent_dir) == 
    #>                 0)
    #>             if (length(root) > 1) {
    #>                 message("Multiple roots in graph. Choosing the first")
    #>                 root <- root[1]
    #>             }
    #>             tree <- unfold_tree(graph, mode = mode, roots = root)
    #>             vattr <- lapply(vertex_attr(graph), `[`, i = tree$vertex_index)
    #>             vertex_attr(tree$tree) <- vattr
    #>             graph <- tree$tree
    #>         }
    #>         as_tbl_graph(graph)
    #>     })
    #>     do.call(bind_graphs, graphs)
    #> }
    #> <bytecode: 0x563400c8e110>
    #> <environment: namespace:ggraph>

    Created on 2021-01-05 by the reprex package (v0.3.0)

  7. mschubert commented on Feb 2, 2021

    @mschubert

    if anyone can provide a reprex I'd be happy to look into it

    I can reproduce this on a "tree" graph with edge fields from and to but no edges (i.e, the edge data.frame has no rows):

    net = structure(list(1, FALSE, numeric(0), numeric(0), numeric(0),
        numeric(0), c(0, 0), c(0, 0), list(c(1, 0, 1), structure(list(), .Names = character(0)),
            list(name = "node"), list(weight = numeric(0)))),
            class = c("tbl_graph", "igraph"), active = "nodes")
        
    
    ggraph(igraph::upgrade_graph(net))
    # Error in graph_to_tree(graph, mode = direction) : Graph must be directed
  8. thomasp85 commented on Jan 11, 2024

    @thomasp85
    Owner

    I think this is fixed now but without a reprex for the original issue it is hard to say

  9. added a commit that references this issue on Jun 23, 2025
    e010f77
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions