6.4 From data to networks
suppressPackageStartupMessages(library(igraph))
#edge list
<- c("Mark", "Mark", "Peter", "Peter", "Bob", "Jill")
personA <- c("Peter", "Jill", "Bob", "Aaron", "Jill", "Aaron")
personB
<- cbind(PersonA = personA, PersonB = personB)
edgelist
print(edgelist)
## PersonA PersonB
## [1,] "Mark" "Peter"
## [2,] "Mark" "Jill"
## [3,] "Peter" "Bob"
## [4,] "Peter" "Aaron"
## [5,] "Bob" "Jill"
## [6,] "Jill" "Aaron"
#adjacency matrix
<- matrix(c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0),
adjacency nrow = 5,
ncol = 5,
dimnames = list(c("Mark", "Peter", "Bob", "Jill", "Aaron"),
c("Mark", "Peter", "Bob", "Jill", "Aaron")))
print(adjacency)
## Mark Peter Bob Jill Aaron
## Mark 0 1 0 1 0
## Peter 1 0 1 0 1
## Bob 0 1 0 1 0
## Jill 1 0 1 0 1
## Aaron 0 1 0 1 0
Edge list into a network object.
<- graph.edgelist(edgelist, directed=FALSE)
network network
## IGRAPH 3e8ebd5 UN-- 5 6 --
## + attr: name (v/c)
## + edges from 3e8ebd5 (vertex names):
## [1] Mark --Peter Mark --Jill Peter--Bob Peter--Aaron Jill --Bob
## [6] Jill --Aaron
Adjacency matrix to network object.
<-graph.adjacency(adjacency,mode = "undirected" ) network_adj
UN
: undirectedDN
: directed- #vertices #edges
Vertices of network
V(network)
## + 5/5 vertices, named, from 3e8ebd5:
## [1] Mark Peter Jill Bob Aaron
Edges of network
E(network)
## + 6/6 edges from 3e8ebd5 (vertex names):
## [1] Mark --Peter Mark --Jill Peter--Bob Peter--Aaron Jill --Bob
## [6] Jill --Aaron
We can visualize the network using the plot() function.
plot(network)