I am trying to write some R code that shows degrees of separation between friends. That is, suppose I pick the "node" with the arrow pointing towards it, and I say "degree = 3", I would like to be able to identify the following path in this network:
I think I found a previous post on stackoverflow that answers a similar question: R Igraph subgraph given node index and number of nodes to include in the graph
Suppose I create some fake network data :
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)
Is this the correct code for plotting 3 degrees of separation for "John"?
distan <- 3
subnetwork <- induced.subgraph(graph, vids = as.vector(unlist(neighborhood(graph, distan, nodes = "John", mode = 'all'))))
plot(subnetwork)
Is there a way to find out "who has the most friends" in this network? (the node with the most connections)
Thanks
Your code works but I believe that use of neighborhood
is deprecated. Instead, it might be better to use.
distan <- 3
subnetwork <- induced.subgraph(graph, unlist(ego(graph, order=3, nodes="John")))
To find out who has the most friends, use the degree function.
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
Andrew has the most friends.