R:绘制著名的“6 度分离”(Kevin Bacon)

R: Plot the famous "6 degrees of separation" (Kevin Bacon)

我正在尝试编写一些 R 代码来显示朋友之间的分离程度。也就是说,假设我选择箭头指向它的“节点”,我说“度数 = 3”,我希望能够在这个网络中识别以下路径:

我想我在 Whosebug 上找到了一个以前的 post 来回答类似的问题:

假设我创建了一些虚假的网络数据:

library(igraph)
file <-data.frame(
    
    "source" = c(
        "John",
        "John",
        "Tim",
        "Tim",
        "Alex",
        "Andrew",
        "Andrew",
        "Andrew",
        "Oliver",
        "Oliver",
        "Oliver",
        "Matt",
        "Steven",
        "Steven",
        "Steven",
        "Matt",
        "Charles",
        "Charles",
        "Charles",
        "Sean",
        "Ted",
        "Ryan",
        "Ryan",
        "Ryan",
        "Ted",
        "Phil",
        "Phil",
        "Phil",
        "Sam",
        "Toby",
        "Toby",
        "Donald",
        "Donald",
        "Donald",
        "Mitch",
        "Mitch",
        "Mitch"),
    
    "target" = c("Sam",
                 "Tim",
                 "Alex",
                 "Matt",
                 "Andrew",
                 "Sean",
                 "Peter",
                 "Ben",
                 "Kevin",
                 "Thomas",
                 "Dave",
                 "Steven",
                 "Kenny",
                 "Derek",
                 "CJ",
                 "Charles",
                 "Ivan",
                 "Kyle",
                 "Andrew",
                 "Ted",
                 "Ryan",
                 "Daniel",
                 "Chris",
                 "Scott",
                 "Phil",
                 "Henry",
                 "George",
                 "Paul",
                 "Toby",
                 "Donald",
                 "Mitch",
                 "Jack",
                 "Luke",
                 "Myles",
                 "Elliot",
                 "Harvey",
                 "Owen")
    
)

graph <- graph.data.frame(file, directed=F)
graph <- simplify(graph)
plot(graph)

这是为“John”绘制 3 度分隔的正确代码吗?

distan <- 3

subnetwork <- induced.subgraph(graph, vids = as.vector(unlist(neighborhood(graph, distan, nodes = "John", mode = 'all'))))

plot(subnetwork)

有没有办法找出这个网络中“谁的朋友最多”? (连接最多的节点)

谢谢

您的代码有效,但我认为不推荐使用 neighborhood。相反,使用它可能会更好。

distan <- 3
subnetwork <- induced.subgraph(graph, unlist(ego(graph, order=3, nodes="John"))) 

要找出谁的朋友最多,请使用度数函数。

degree(graph)
   John     Tim    Alex  Andrew  Oliver    Matt  Steven Charles    Sean     Ted 
      2       3       2       5       3       3       4       4       2       3 
   Ryan    Phil     Sam    Toby  Donald   Mitch   Peter     Ben   Kevin  Thomas 
      4       4       2       3       4       4       1       1       1       1 
   Dave   Kenny   Derek      CJ    Ivan    Kyle  Daniel   Chris   Scott   Henry 
      1       1       1       1       1       1       1       1       1       1 
 George    Paul    Jack    Luke   Myles  Elliot  Harvey    Owen 
      1       1       1       1       1       1       1       1 

安德鲁的朋友最多。