0

I have a rooted forest with 116 trees as a tidygraph object. I now want to add a new property to the nodes, i.e. label the nodes within the branches.

For instance, for a graph

a <- tibble(from = c(1, 2, 3, 3, 4, 5, 7, 8, 8, 9, 10), 
            to = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))
a_graph = as_tbl_graph(a)

I'd like to have a column in the node data with c("A", "A", "A", "B", "C", "B", "C", "C", "D", "E", "D", "E"), i.e. nodes 1-3 are labeled with A, nodes 4 and 6 with B, nodes 5, 7, 8 with C, nodes 9 and 11 with D, and nodes 10 and 12 with E.

So, is there a way to automatically label the nodes depending on the branch they're on even if the trees in my forest can have very different structures?

Pascal
  • 563
  • 1
  • 3
  • 15

1 Answers1

0

This will label nodes in groups of 3 if there are less than 78 nodes. Trees having more nodes require multi character labels e.g. AA.

library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(tidyverse)

a <- tibble(
  from = c(1, 2, 3, 3, 4, 5, 7, 8, 8, 9, 10),
  to = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
)
a_graph <- as_tbl_graph(a)

a_graph |>
  activate(nodes) |>
  mutate(label = name |> map_chr(function(name) {
    res <- as.integer(name)
    res <- res / 4
    res <- LETTERS[res + 1]
    res
  }))
#> # A tbl_graph: 12 nodes and 11 edges
#> #
#> # A rooted tree
#> #
#> # Node Data: 12 × 2 (active)
#>   name  label
#>   <chr> <chr>
#> 1 1     A    
#> 2 2     A    
#> 3 3     A    
#> 4 4     B    
#> 5 5     B    
#> 6 7     B    
#> # … with 6 more rows
#> #
#> # Edge Data: 11 × 2
#>    from    to
#>   <int> <int>
#> 1     1     2
#> 2     2     3
#> 3     3     4
#> # … with 8 more rows

Created on 2022-06-21 by the reprex package (v2.0.0)

danlooo
  • 10,067
  • 2
  • 8
  • 22
  • Thanks. This however only works when the names of the nodes are numbers and not letters as well. For instance, replace `a` with the corresponding letters of the alphabet, then we have a problem at `res <- as.integer(name)`. – Pascal Jun 21 '22 at 15:38
  • Also, it doesn't return the results, I was expecting. For instance, nodes 5 and 7 are labeled as B but should be C, nodes 9 and 11 should be D but are C, and nodes 10 and 12 should be E. I've edited the question to be more precise. I was expecting that the labelling process requires identifying leafs and the "division" nodes. – Pascal Jun 21 '22 at 16:11