您可以使用假人扩展顶点表:
library(tidyverse)
library(igraph)
#>
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:dplyr':
#>
#> as_data_frame, groups, union
#> The following objects are masked from 'package:purrr':
#>
#> compose, simplify
#> The following object is masked from 'package:tidyr':
#>
#> crossing
#> The following object is masked from 'package:tibble':
#>
#> as_data_frame
#> The following objects are masked from 'package:stats':
#>
#> decompose, spectrum
#> The following object is masked from 'package:base':
#>
#> union
vertices <- tribble(
~name, ~ alias,
1, "A",
2, "B",
3, "C"
)
vertices
#> # A tibble: 3 × 2
#> name alias
#> <dbl> <chr>
#> 1 1 A
#> 2 2 B
#> 3 3 C
links <- tribble(
~from, ~to,
1, 2,
1, 3,
1, 4,
2, 5
)
links
#> # A tibble: 4 × 2
#> from to
#> <dbl> <dbl>
#> 1 1 2
#> 2 1 3
#> 3 1 4
#> 4 2 5
other_vertices <-
c(links$from, links$to) %>%
unique() %>%
setdiff(vertices$name) %>%
tibble(name = .)
other_vertices
#> # A tibble: 2 × 1
#> name
#> <dbl>
#> 1 4
#> 2 5
vertices %>%
full_join(other_vertices) %>%
graph_from_data_frame(d=links,vertices = ., directed = TRUE)
#> Joining, by = "name"
#> IGRAPH 9e3ec0f DN-- 5 4 --
#> + attr: name (v/c), alias (v/c)
#> + edges from 9e3ec0f (vertex names):
#> [1] 1->2 1->3 1->4 2->5
由reprex 包于 2021-09-18 创建(v2.0.1)