library(igraph)
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
library(igraphdata)
graph_from_literal(1-2, 1-3)
## IGRAPH 31ed866 UN-- 3 2 --
## + attr: name (v/c)
## + edges from 31ed866 (vertex names):
## [1] 1--2 1--3
# manual definition of an undirected graph with graph_from_literal
u_graph <- graph_from_literal(1-2, 1-3, 2-3, 2-4, 3-5, 4-5, 3-4, 4-6, 4-7, 5-6, 6-7)
plot(u_graph)
# manual definition of a directed graph with graph_from_literal
d_graph <- graph_from_literal(1+-2, 1-+3, 2-+3, 2+-4, 3+-5, 4+-5, 3+-4, 4++6, 4++7, 5+-6, 6-+7)
plot(d_graph)
# edge count
ecount(u_graph)
## [1] 11
# vertex count
vcount(u_graph)
## [1] 7
# Is there an edge btw vertex i and vertex j?
d_graph[4, 1] # 0 if there is no connection
## [1] 0
d_graph[4, 6] # 1 if there is connection
## [1] 1
neighbors(d_graph, 3, mode= "all")
## + 4/7 vertices, named, from 31f64c4:
## [1] 1 2 4 5
neighbors(d_graph, 3, mode= "in")
## + 4/7 vertices, named, from 31f64c4:
## [1] 1 2 4 5
neighbors(d_graph, 3, mode= "out")
## + 0/7 vertices, named, from 31f64c4:
ego(d_graph, # Network
2, # Number of steps
5, # Vertex of interest
mode= "out") # Set direction: in, out, all
## [[1]]
## + 6/7 vertices, named, from 31f64c4:
## [1] 5 3 4 2 6 7
people_graph <-graph_from_literal(John-+Jim, Jim--Sue, Sue+-John, John++Pam, Pam-+Sue)
plot(people_graph)
V(people_graph)
## + 4/4 vertices, named, from 3261b00:
## [1] John Jim Sue Pam
# Let us set attributes to vertices
people_graph<- set_vertex_attr(people_graph, "gender", value= c("M", "M", "F", "F"))
vertex.attributes(people_graph)
## $name
## [1] "John" "Jim" "Sue" "Pam"
##
## $gender
## [1] "M" "M" "F" "F"
# Alternatively
V(people_graph)$gender <- c("Male", "Male", "Female", "Female")
vertex_attr(people_graph)
## $name
## [1] "John" "Jim" "Sue" "Pam"
##
## $gender
## [1] "Male" "Male" "Female" "Female"
Let’s visualize
plot(people_graph, vertex.color = "purple")
plot(people_graph, vertex.color = "red")
plot(people_graph, vertex.color = ifelse(V(people_graph)$gender =="Male", "blue", "pink"))
# Add more settings
plot(people_graph, vertex.color = ifelse(V(people_graph)$gender =="Male", "green", "orange"), vertex.size = 68, vertex.shape="square", vertex.label.cex= 0.8, vertex.label = V(people_graph)$gender, vertex.label.font =3 )
# Set edge attribute
E(people_graph)
## + 5/5 edges from 3261b00 (vertex names):
## [1] John->Jim John->Sue John->Pam Pam ->John Pam ->Sue
people_graph<- set_edge_attr(people_graph, "relationship", value=c("friend", "foe", "foe", "friend", "friend"))
edge_attr(people_graph)
## $relationship
## [1] "friend" "foe" "foe" "friend" "friend"
people_graph<- set_edge_attr(people_graph, "debt", value=c(10, -12, 21, 22, -20))
edge_attr(people_graph)
## $relationship
## [1] "friend" "foe" "foe" "friend" "friend"
##
## $debt
## [1] 10 -12 21 22 -20
# Define a graph using an edge list
data <- read.csv("edgelist.csv", stringsAsFactors = FALSE, header = TRUE)
# Save as a matrix
data<- as.matrix(data)
data
## name1 name2 frequency
## [1,] "k" "po" " 1"
## [2,] "a" "d" " 2"
## [3,] "ada" "ooi" " 3"
## [4,] "ada" "d" " 4"
## [5,] "d" "a" " 5"
## [6,] "b" "f" " 6"
## [7,] "b" "d" " 7"
## [8,] "ada" "e" " 8"
## [9,] "a" "e" " 9"
## [10,] "f" "ooi" "10"
## [11,] "po" "i" "11"
## [12,] "po" "ooi" "12"
## [13,] "ada" "po" "13"
## [14,] "d" "po" "14"
## [15,] "f" "po" "15"
graph1 <- graph.edgelist(data[, 1:2], directed= F)
graph2 <- graph.edgelist(data[, 1:2], directed= T)
plot(graph1)
plot(graph2)
V(graph1)[1]
## + 1/10 vertex, named, from 32929da:
## [1] k
V(graph1)["k"]
## + 1/10 vertex, named, from 32929da:
## [1] k
V(graph1)[1:5]
## + 5/10 vertices, named, from 32929da:
## [1] k po a d ada
graph1_matrix=get.adjacency(graph1, sparse = FALSE)
write.csv(graph1_matrix, "adjacency_matrix.csv")
friendship <- read.csv("fr.csv", header=T, row.names=1)
friendship
## John Jim Sue Pam Kim Tom
## John 0 1 1 0 0 0
## Jim 0 0 1 1 1 1
## Sue 0 0 0 0 1 0
## Pam 1 1 1 0 1 0
## Kim 1 1 0 0 0 1
## Tom 0 0 1 1 1 0
friendship<- as.matrix(friendship)
f_graph <- graph_from_adjacency_matrix(friendship, mode="directed")
V(f_graph)
## + 6/6 vertices, named, from 32a7b40:
## [1] John Jim Sue Pam Kim Tom
gsize(f_graph)
## [1] 17
V(f_graph)$gender <- c("Male", "Male", "Female", "Female", "Female", "Male")
V(f_graph)$fav.animal <- c("cat", "dog", "lion", "ant", "ant", "dog")
plot(f_graph)
plot(f_graph, vertex.color = "purple")
plot(f_graph, vertex.color = "red")
plot(f_graph, vertex.color = "blue")
plot(f_graph, vertex.color = ifelse(V(f_graph)$fav.animal =='dog', "pink", "green"))
plot(f_graph, vertex.color = ifelse(V(f_graph)$gender =="Male", "blue", "red"))
plot(f_graph, vertex.color = ifelse(V(f_graph)$gender =="Male", "grey", "red"), vertex.size = 68, vertex.shape="circle",vertex.label.cex= 0.8,
vertex.label = V(f_graph)$gender, vertex.label.font =2, edge.arrow.size = 0.2 )
##layout
plot(graph2, vertex.size=20, layout=layout.fruchterman.reingold(graph2) )
plot(graph2, vertex.size=20, layout=layout_as_star(graph2))
plot(graph2, vertex.size=20, layout=layout_as_tree(graph2))
plot(graph2, vertex.size=20, layout=layout.circle(graph2))
plot(graph2, vertex.size=20, layout=layout.grid(graph2))
plot(graph2, vertex.size=20, layout= layout.kamada.kawai(graph2))
# Get degree of the vertices in graph1
degree(graph1)
## k po a d ada ooi b f e i
## 1 6 3 5 4 3 2 3 2 1
deg = degree(graph1, mode="all")
#deg = degree(graph1, mode="in")
#deg = degree(graph1, mode="out")
##min, max, mean degree
max(degree(graph1))
## [1] 6
min(degree(graph1))
## [1] 1
mean(degree(graph1))
## [1] 3
plot(graph1, vertex.size = degree(graph1)*4)
degree(graph1)
## k po a d ada ooi b f e i
## 1 6 3 5 4 3 2 3 2 1
deg = degree(graph1, mode="all")
# Create a histogram
hist(deg)
hist(deg, breaks=1:vcount(graph1)-1, main="Histogram of node degree")
dd<- degree_distribution(graph1, cumulative = T) # cumulative degree distribution
plot(x=0:max(deg), y=1-dd)
# Get the degree sequence in descending order
sort(degree(graph1)) # Sort degrees of each vertex
## k i b e a ooi f ada d po
## 1 1 2 2 3 3 3 4 5 6
sort(degree(graph1), decreasing = TRUE) # Sort in an ascending order
## po d ada a ooi f b e k i
## 6 5 4 3 3 3 2 2 1 1
which.max(deg) # Find the one with the max degree
## po
## 2
Degree distribution
For any integer k greater or equal to 0, the quantity Pk is the fraction of nodes having degree k. This also represents the probability that a randomly chosen node in the network has degree k.
# Degree distribution:
degree_distribution(graph1)
## [1] 0.0 0.2 0.2 0.3 0.1 0.1 0.1
deg <-degree(graph1)
hist(degree(graph1))
plot(degree(graph1))
plot(degree_distribution(graph1, cumulative=T, mode="all" ), xlab= "Degree", ylab= "Cumulative frequency") ##### label axes
Diameter is the longest path length: path is the nb edges to be traversed to go from i to j
get_diameter(graph1)
## + 4/10 vertices, named, from 32929da:
## [1] k po d a
diam= get_diameter(graph1)
E(graph1, path=diam)
## + 3/15 edges from 32929da (vertex names):
## [1] k --po po--d a --d
farthest_vertices(graph1)
## $vertices
## + 2/10 vertices, named, from 32929da:
## [1] k a
##
## $distance
## [1] 3
mean_distance(graph1)
## [1] 1.911111
shortest.paths(graph1)
## k po a d ada ooi b f e i
## k 0 1 3 2 2 2 3 2 3 2
## po 1 0 2 1 1 1 2 1 2 1
## a 3 2 0 1 2 3 2 3 1 3
## d 2 1 1 0 1 2 1 2 2 2
## ada 2 1 2 1 0 1 2 2 1 2
## ooi 2 1 3 2 1 0 2 1 2 2
## b 3 2 2 1 2 2 0 1 3 3
## f 2 1 3 2 2 1 1 0 3 2
## e 3 2 1 2 1 2 3 3 0 3
## i 2 1 3 2 2 2 3 2 3 0
# Color edges (ecol = edge color)
ecol <-rep("gray80",ecount(graph1)) ###color everything gray
ecol[E(graph1, path=diam)] <- "orange" ##except the diameter
# E(net, path=diam) finds edges along a path, here'diam'
plot(graph1, edge.color=ecol, vertex.color= "blue")
#density
edge_density(graph1)
## [1] 0.3333333
library(igraphdata)
data("karate")
plot(karate)
## This graph was created by an old(er) igraph version.
## Call upgrade_graph() on it to use with the current igraph version
## For now we convert it on the fly...
centr_degree(karate)
## $res
## [1] 16 9 10 6 3 4 4 4 5 2 3 1 2 5 2 2 2 2 2 3 2 2 2 5 3
## [26] 3 2 4 3 4 4 6 12 17
##
## $centralization
## [1] 0.3761141
##
## $theoretical_max
## [1] 1122
c= centr_degree(karate)
c$res
## [1] 16 9 10 6 3 4 4 4 5 2 3 1 2 5 2 2 2 2 2 3 2 2 2 5 3
## [26] 3 2 4 3 4 4 6 12 17
c$centralization
## [1] 0.3761141
c$theoretical_max
## [1] 1122
betweenness(karate)
## Mr Hi Actor 2 Actor 3 Actor 4 Actor 5 Actor 6 Actor 7
## 250.150000 33.800000 36.650000 1.333333 0.500000 15.500000 15.500000
## Actor 8 Actor 9 Actor 10 Actor 11 Actor 12 Actor 13 Actor 14
## 0.000000 13.100000 7.283333 0.500000 0.000000 0.000000 1.200000
## Actor 15 Actor 16 Actor 17 Actor 18 Actor 19 Actor 20 Actor 21
## 0.000000 0.000000 0.000000 16.100000 3.000000 127.066667 0.000000
## Actor 22 Actor 23 Actor 24 Actor 25 Actor 26 Actor 27 Actor 28
## 0.000000 0.000000 1.000000 33.833333 0.500000 0.000000 6.500000
## Actor 29 Actor 30 Actor 31 Actor 32 Actor 33 John A
## 10.100000 0.000000 3.000000 66.333333 38.133333 209.500000
max(betweenness(karate))
## [1] 250.15
centr_betw(karate)
## $res
## [1] 231.0714286 28.4785714 75.8507937 6.2880952 0.3333333 15.8333333
## [7] 15.8333333 0.0000000 29.5293651 0.4476190 0.3333333 0.0000000
## [13] 0.0000000 24.2158730 0.0000000 0.0000000 0.0000000 0.0000000
## [19] 0.0000000 17.1468254 0.0000000 0.0000000 0.0000000 9.3000000
## [25] 1.1666667 2.0277778 0.0000000 11.7920635 0.9476190 1.5428571
## [31] 7.6095238 73.0095238 76.6904762 160.5515873
##
## $centralization
## [1] 0.4055572
##
## $theoretical_max
## [1] 17424
V(karate)$betweenness <- betweenness(karate)
which(V(karate)$betweenness ==max(betweenness(karate)) )
## [1] 1
max(closeness(karate))
## [1] 0.007692308
min(closeness(karate))
## [1] 0.003289474
centr_clo(karate)
## $res
## [1] 0.5689655 0.4852941 0.5593220 0.4647887 0.3793103 0.3837209 0.3837209
## [8] 0.4400000 0.5156250 0.4342105 0.3793103 0.3666667 0.3707865 0.5156250
## [15] 0.3707865 0.3707865 0.2844828 0.3750000 0.3707865 0.5000000 0.3707865
## [22] 0.3750000 0.3707865 0.3928571 0.3750000 0.3750000 0.3626374 0.4583333
## [29] 0.4520548 0.3837209 0.4583333 0.5409836 0.5156250 0.5500000
##
## $centralization
## [1] 0.2981949
##
## $theoretical_max
## [1] 16.24615
closeness(karate)
## Mr Hi Actor 2 Actor 3 Actor 4 Actor 5 Actor 6
## 0.007692308 0.006060606 0.005952381 0.005347594 0.004629630 0.004608295
## Actor 7 Actor 8 Actor 9 Actor 10 Actor 11 Actor 12
## 0.004651163 0.005524862 0.006024096 0.005780347 0.005319149 0.004424779
## Actor 13 Actor 14 Actor 15 Actor 16 Actor 17 Actor 18
## 0.006211180 0.005780347 0.005181347 0.004166667 0.003289474 0.005847953
## Actor 19 Actor 20 Actor 21 Actor 22 Actor 23 Actor 24
## 0.005681818 0.007518797 0.006172840 0.005347594 0.004807692 0.004201681
## Actor 25 Actor 26 Actor 27 Actor 28 Actor 29 Actor 30
## 0.004784689 0.003745318 0.005128205 0.004739336 0.006134969 0.005291005
## Actor 31 Actor 32 Actor 33 John A
## 0.005263158 0.006329114 0.006060606 0.007633588
eigen_centrality(karate)
## $vector
## Mr Hi Actor 2 Actor 3 Actor 4 Actor 5 Actor 6 Actor 7
## 0.85787944 0.82876616 0.99036448 0.54536909 0.15291191 0.18519270 0.18250148
## Actor 8 Actor 9 Actor 10 Actor 11 Actor 12 Actor 13 Actor 14
## 0.49006831 0.67825515 0.13788382 0.12588193 0.11866884 0.11499616 0.66050150
## Actor 15 Actor 16 Actor 17 Actor 18 Actor 19 Actor 20 Actor 21
## 0.21845188 0.31067062 0.05086244 0.11732645 0.13429645 0.20164970 0.17234251
## Actor 22 Actor 23 Actor 24 Actor 25 Actor 26 Actor 27 Actor 28
## 0.15554033 0.22248354 0.59886179 0.14020695 0.33667492 0.16102652 0.40561477
## Actor 29 Actor 30 Actor 31 Actor 32 Actor 33 John A
## 0.23660019 0.37306834 0.43481077 0.57527665 0.91256318 1.00000000
##
## $value
## [1] 21.68757
##
## $options
## $options$bmat
## [1] "I"
##
## $options$n
## [1] 34
##
## $options$which
## [1] "LA"
##
## $options$nev
## [1] 1
##
## $options$tol
## [1] 0
##
## $options$ncv
## [1] 0
##
## $options$ldv
## [1] 0
##
## $options$ishift
## [1] 1
##
## $options$maxiter
## [1] 3000
##
## $options$nb
## [1] 1
##
## $options$mode
## [1] 1
##
## $options$start
## [1] 1
##
## $options$sigma
## [1] 0
##
## $options$sigmai
## [1] 0
##
## $options$info
## [1] 0
##
## $options$iter
## [1] 2
##
## $options$nconv
## [1] 1
##
## $options$numop
## [1] 26
##
## $options$numopb
## [1] 0
##
## $options$numreo
## [1] 21
eigen_centrality(karate)$vector
## Mr Hi Actor 2 Actor 3 Actor 4 Actor 5 Actor 6 Actor 7
## 0.85787944 0.82876616 0.99036448 0.54536909 0.15291191 0.18519270 0.18250148
## Actor 8 Actor 9 Actor 10 Actor 11 Actor 12 Actor 13 Actor 14
## 0.49006831 0.67825515 0.13788382 0.12588193 0.11866884 0.11499616 0.66050150
## Actor 15 Actor 16 Actor 17 Actor 18 Actor 19 Actor 20 Actor 21
## 0.21845188 0.31067062 0.05086244 0.11732645 0.13429645 0.20164970 0.17234251
## Actor 22 Actor 23 Actor 24 Actor 25 Actor 26 Actor 27 Actor 28
## 0.15554033 0.22248354 0.59886179 0.14020695 0.33667492 0.16102652 0.40561477
## Actor 29 Actor 30 Actor 31 Actor 32 Actor 33 John A
## 0.23660019 0.37306834 0.43481077 0.57527665 0.91256318 1.00000000
evcent(karate)$vector
## Mr Hi Actor 2 Actor 3 Actor 4 Actor 5 Actor 6 Actor 7
## 0.85787944 0.82876616 0.99036448 0.54536909 0.15291191 0.18519270 0.18250148
## Actor 8 Actor 9 Actor 10 Actor 11 Actor 12 Actor 13 Actor 14
## 0.49006831 0.67825515 0.13788382 0.12588193 0.11866884 0.11499616 0.66050150
## Actor 15 Actor 16 Actor 17 Actor 18 Actor 19 Actor 20 Actor 21
## 0.21845188 0.31067062 0.05086244 0.11732645 0.13429645 0.20164970 0.17234251
## Actor 22 Actor 23 Actor 24 Actor 25 Actor 26 Actor 27 Actor 28
## 0.15554033 0.22248354 0.59886179 0.14020695 0.33667492 0.16102652 0.40561477
## Actor 29 Actor 30 Actor 31 Actor 32 Actor 33 John A
## 0.23660019 0.37306834 0.43481077 0.57527665 0.91256318 1.00000000
library(centiserve)
## Loading required package: Matrix
katzcent(karate)
## Mr Hi Actor 2 Actor 3 Actor 4 Actor 5 Actor 6 Actor 7 Actor 8
## 4.982994 3.651810 4.121408 3.022648 1.890452 2.031073 2.031073 2.577886
## Actor 9 Actor 10 Actor 11 Actor 12 Actor 13 Actor 14 Actor 15 Actor 16
## 3.112664 1.926075 1.890452 1.498299 1.800564 3.091820 1.940527 1.940527
## Actor 17 Actor 18 Actor 19 Actor 20 Actor 21 Actor 22 Actor 23 Actor 24
## 1.406215 1.863480 1.940527 2.377414 1.940527 1.863480 1.940527 2.586533
## Actor 25 Actor 26 Actor 27 Actor 28 Actor 29 Actor 30 Actor 31 Actor 32
## 1.709116 1.730106 1.751366 2.355640 2.226616 2.374316 2.616974 3.005410
## Actor 33 John A
## 4.265928 5.139339
plot(karate, main="Hubs", vertex.label="",
vertex.size=6 * sqrt(hub_score(karate)$vector))
plot(karate, main="Authorities",
vertex.label="", vertex.size=10 *sqrt(authority_score(karate)$vector))