简体   繁体   中英

Label tree branches in tidygraph

I have a rooted forest with 116 trees as a tidygraph object. I now want to add a new property to the nodes, ie 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") , ie 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?

This will label nodes in groups of 3 if there are less than 78 nodes. Trees having more nodes require multi character labels eg 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM