如何为 tidygraph 和 ggraph 设置数据?

How do I setup data for tidygraph and ggraph?

我想 运行 进行网络分析,但完全不知道如何正确构建我的数据,因为大多数示例已经在 to 和 [=12= 处构建了数据]级。

我的数据示例如下:

df <- data.frame(Name = c("Alice", "Ben", "Tom", "Jane", "Neil", "Alice", "Tom", "Ben", "Jane", "Neil", "Alice", "Tom", "Ben", "Jane", "Bob"),
         Location = c("Ward", "Desk", "Op", "Call", "Off",
                      "Ward", "Desk", "Op", "Call", "Off",
                      "Ward", "Desk", "Op", "Call", "Off"),
         Rating = c(1, 1, 1, 1, 1, 10, 10, 10, 10, 10, 8, 8, 8, 8, 8))

我现在希望得到 tofrom 人的组合,如 Name 所示,每个 Rating。您还会注意到,人们可以在不同的评级期间处于不同的 Location,尽管我更愿意这样做,结合 Name 成为节点,Rating 成为节点边缘。

我看过 library(iterpc) 但我很难理解整个组合,有五个不同的阵容。

我的问题是否有潜在的 dplyr 解决方案?谢谢!

编辑:看起来我的问题与 非常相似,但标记的答案对我不起作用,而是 Error: Column name Name must not be duplicated.

如果您希望 from 列为 Name 并且 to 列为您的 Rating 列,那么 tidygraph 会为您完成此映射。

library(tidygraph)
#> Warning: package 'tidygraph' was built under R version 3.6.3
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter

df <- data.frame(
  Name = c(
    "Alice", "Ben", "Tom", "Jane", "Neil",
    "Alice", "Tom", "Ben", "Jane", "Neil",
    "Alice", "Tom", "Ben", "Jane", "Bob"
  ),
  Location = c(
    "Ward", "Desk", "Op", "Call", "Off",
    "Ward", "Desk", "Op", "Call", "Off",
    "Ward", "Desk", "Op", "Call", "Off"
  ),
  Rating = c(
    1, 1, 1, 1, 1,
    10, 10, 10, 10, 10,
    8, 8, 8, 8, 8)
)

tg <- as_tbl_graph(df)
tg
#> # A tbl_graph: 11 nodes and 15 edges
#> #
#> # A directed acyclic multigraph with 4 components
#> #
#> # Node Data: 11 x 1 (active)
#>   name 
#>   <chr>
#> 1 Alice
#> 2 Ben  
#> 3 Tom  
#> 4 Jane 
#> 5 Neil 
#> 6 Bob  
#> # ... with 5 more rows
#> #
#> # Edge Data: 15 x 3
#>    from    to Rating
#>   <int> <int>  <dbl>
#> 1     1     7      1
#> 2     2     8      1
#> 3     3     9      1
#> # ... with 12 more rows

您可以 double-check 通过查看边缘 table 的第一行并查看 17 之间的边缘来正确完成此映射,它们是AliceWard,这是原始数据框中的第一行。

data.frame(tg)
#>     name
#> 1  Alice
#> 2    Ben
#> 3    Tom
#> 4   Jane
#> 5   Neil
#> 6    Bob
#> 7   Ward
#> 8   Desk
#> 9     Op
#> 10  Call
#> 11   Off

reprex package (v0.3.0)

于 2020-09-21 创建