Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R session aborts with "unrooted" layout option #383

Open
grunwald-lab-umass opened this issue Dec 30, 2024 · 1 comment
Open

R session aborts with "unrooted" layout option #383

grunwald-lab-umass opened this issue Dec 30, 2024 · 1 comment

Comments

@grunwald-lab-umass
Copy link

I started working on auto-generating a tree for an article figure, and got about this far before I had my R session abort every time I tried to plot this graph.
I had been using the "tree" layout, which worked fine except it added an extra root node when I already had included.
So I went to use the "unrooted" option and... well, every time I run that part of the code, my R session aborts and restarts.

R version 4.4.2 (2024-10-31)
Platform: aarch64-apple-darwin20
Running under: macOS Sequoia 15.1.1
tidyverse 2.0.0
ggraph 2.2.1
tidygraph 1.3.1

library(tidyverse)
library(ggraph)
library(tidygraph)

MNI = 4 # MNI - 1 = number of intersections
MAX_LEVEL = 2*(MNI - 1) + 3 # 2 levels per intersection (location inside/outside threshold and if intersection occurs) + initial node + starting location
node_tib <- tibble_row(
    node_id = "0",
    node_level = 1,
    label = "root",
    parent = NA,
    has_children = TRUE,
    value = NA) # init_node

level = 2

while(level < MAX_LEVEL){

    for(parent_id in filter(node_tib, node_level == level - 1)$node_id){
        if(filter(node_tib, node_id == parent_id)$has_children == TRUE){
    
        if(level %% 2 == 1){ # odd number levels
            # child 1
            node_tib <- add_row(node_tib,
                                node_id = paste0(parent_id,"1"),
                                node_level = level,
                                label = "yes",
                                parent = parent_id,
                                has_children = TRUE,
                                value = NA)
            # child 2
            node_tib <- add_row(node_tib,
                                node_id = paste0(parent_id,"0"),
                                node_level = level,
                                label = "no",
                                parent = parent_id,
                                has_children = TRUE,
                                value = NA)
        }
        else if(level == 2){ # second level
            # child 1
            node_tib <- add_row(node_tib,
                                node_id = paste0(parent_id,"1"),
                                node_level = level,
                                label = "nucleus",
                                parent = parent_id,
                                has_children = TRUE,
                                value = NA)
            # child 2
            node_tib <- add_row(node_tib,
                                node_id = paste0(parent_id,"0"),
                                node_level = level,
                                label = "cytoplasm",
                                parent = parent_id,
                                has_children = TRUE,
                                value = NA)
        }
        else { # even number levels
            # child 1
            node_tib <- add_row(node_tib,
                                node_id = paste0(parent_id,"1"),
                                node_level = level,
                                label = "yes",
                                parent = parent_id,
                                has_children = TRUE,
                                value = NA)
            # child 2
            node_tib <- add_row(node_tib,
                                node_id = paste0(parent_id,"0"),
                                node_level = level,
                                label = "no",
                                parent = parent_id,
                                has_children = FALSE,
                                value = NA)
        }
    
        }
    }
    
    level <- level + 1
}

for(parent_id in filter(node_tib, node_level == level - 1)$node_id){
    if(filter(node_tib, node_id == parent_id)$has_children == TRUE){
        node_tib <- add_row(node_tib,
                            node_id = paste0(parent_id,"1"),
                            node_level = level,
                            label = "yes",
                            parent = parent_id,
                            has_children = FALSE,
                            value = NA)
        node_tib <- add_row(node_tib,
                            node_id = paste0(parent_id,"0"),
                            node_level = level,
                            label = "no",
                            parent = parent_id,
                            has_children = FALSE,
                            value = NA)
    }
}

graph <- as_tbl_graph(
    data.frame(
        from = node_tib$parent,
        to = node_tib$node_id
    )
)
graph

tree_plot <- ggraph(graph, layout="unrooted") + 
    geom_edge_link() + 
    geom_node_point(size=3, shape = 21, colour = "black", stroke = 1, fill = "white")
tree_plot
@jctourtellotte
Copy link

I wanted to clarify that I am posting this, as I did not intend to post under my lab's general account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants