library(igraph) edgelist <- as.matrix(read.table("fbnet-el.txt")) vnames <- read.table("fbnet-name.txt", sep=",", stringsAsFactor=FALSE, na.strings="") ## with 'graph.edgelist' fbnet0 <- graph.edgelist(edgelist, directed=FALSE) # network summary fbnet0 # vertices V(fbnet0) vcount(fbnet0) V(fbnet0)$initials <- vnames[,1] V(fbnet0)$list <- vnames[,2] fbnet0 head(E(fbnet0)) ecount(fbnet0) ## connectedness is.connected(fbnet0) # [1] FALSE fb.components <- clusters(fbnet0) fb.components # $membership # [1] 1 1 2 2 1 1 1 1 3 1 1 1 1 2 4 2 1 1 5 6 7 1 1 8 1 # [26] 1 1 1 1 9 1 10 1 1 1 1 1 1 2 1 1 11 1 1 1 1 12 13 1 1 # [51] 1 1 1 1 1 14 1 15 15 1 1 1 1 1 16 1 17 1 1 1 1 1 1 1 1 # [76] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 18 1 1 1 1 # [101] 1 1 1 1 1 1 1 1 1 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 # [126] 1 1 1 19 1 1 1 1 1 1 1 1 9 20 1 1 12 1 1 16 1 1 21 1 1 # [151] 1 16 # # $csize # [1] 122 5 1 1 2 1 1 1 2 1 1 2 1 1 2 3 1 1 1 # [20] 1 1 # # $no # [1] 21 names(fb.components) # [1] "membership" "csize" "no" head(fb.components$membership, 10) # [1] 1 1 2 2 1 1 1 1 3 1 fb.components$csize # [1] 122 5 1 1 2 1 1 1 2 1 1 2 1 1 2 3 1 1 # [19] 1 1 1 fb.components$no # [1] 21 # largest connected component fbnet.lcc <- induced.subgraph(fbnet0,fb.components$membership== which.max(fb.components$csize)) fbnet.lcc # IGRAPH U--- 122 535 -- # + attr: initials (v/x) is.connected(fbnet.lcc) # [1] TRUE ## display graph par(mfrow=c(2,2)) plot(fbnet.lcc, layout=layout.random, main="random layout", vertex.size=3, vertex.color="pink", vertex.frame.color="pink", vertex.label.color="darkred", edge.color="grey", vertex.label=V(fbnet.lcc)$initials) # default layout plot(fbnet.lcc, layout=layout.circle, main="circular layout", vertex.size=3, vertex.color="pink", vertex.frame.color="pink", vertex.label.color="darkred", edge.color="grey", vertex.label=V(fbnet.lcc)$initials) plot(fbnet.lcc, layout=layout.kamada.kawai, main="Kamada Kawai layout", vertex.size=3, vertex.color="pink", vertex.frame.color="pink", vertex.label.color="darkred", edge.color="grey", vertex.label=V(fbnet.lcc)$initials) # set the attribute 'layout' for the graph set.seed(23081540) fbnet.lcc$layout <- layout.fruchterman.reingold(fbnet.lcc) # and the attributes 'label' and 'colors' for the nodes V(fbnet.lcc)$label <- V(fbnet.lcc)$initials V(fbnet.lcc)$color <- rainbow(length(unique(V(fbnet.lcc)$list)))[ as.numeric(factor(V(fbnet.lcc)$list))] plot(fbnet.lcc, vertex.size=3, vertex.label.color="black", edge.color="grey") graph.density(fbnet.lcc) # [1] 0.0724834 transitivity(fbnet.lcc) # [1] 0.5604524 ## node characteristics fbnet.degrees <- degree(fbnet.lcc) summary(fbnet.degrees) # Min. 1st Qu. Median Mean 3rd Qu. Max. # 1.00 2.00 6.00 8.77 15.00 31.00 fbnet.between <- betweenness(fbnet.lcc) summary(fbnet.between) # Min. 1st Qu. Median Mean 3rd Qu. Max. # 0.00 0.00 14.03 301.70 123.10 3439.00 # degree and betweenness distributions par(mfrow=c(1,2)) plot(density(fbnet.degrees), lwd=2, main="Degree distribution", xlab="Degree", ylab="Density", col="darkred") plot(density(fbnet.between), lwd=2, main="Betweenness distribution", xlab="Betweenness", ylab="Density", col="darkred") par(mfrow=c(1,1)) par(mar=rep(1,4)) V(fbnet.lcc)$size <- 2*sqrt(fbnet.degrees) bet.col <- cut(log(fbnet.between+1),10,labels=FALSE) V(fbnet.lcc)$color <- heat.colors(10)[11-bet.col] plot(fbnet.lcc, main="Degree and betweenness", vertex.frame.color=heat.colors(10)[bet.col], vertex.label=NA, edge.color="grey") ## Clustering fbnet.clusters <- multilevel.community(fb.lcc) modularity(fbnet.clusters) # Graph community structure calculated with the multi level algorithm # Number of communities (best split): 7 # Modularity (best split): 0.566977 # Membership vector: # [1] 3 2 2 2 2 3 5 3 5 5 4 3 3 2 7 7 7 7 7 7 2 2 6 1 5 4 7 3 4 4 7 2 7 4 2 3 # [37] 6 2 7 2 7 3 4 2 7 6 4 3 3 1 3 2 3 3 5 3 3 3 3 3 4 3 7 3 3 3 4 7 3 2 4 3 # [73] 5 4 6 4 3 4 4 4 5 7 4 4 7 4 4 4 3 6 4 4 7 7 4 2 5 4 4 5 4 5 6 4 4 4 7 7 # [109] 6 2 2 4 5 4 7 7 5 6 7 4 2 7 table(fbnet.clusters$membership) # 1 2 3 4 5 6 7 # 2 18 26 32 12 8 24 V(fbnet.lcc)$community <- fbnet.clusters$membership fbnet.lcc # IGRAPH U--- 122 535 -- # + attr: layout (g/n), initials (v/c), list (v/c), label (v/c), color # (v/c), community (v/n), size (v/n) par(mfrow=c(1,1)) par(mar=rep(1,4)) plot(fbnet.lcc, main="Communities", vertex.frame.color=rainbow(7)[fbnet.clusters$membership], vertex.color=rainbow(7)[fbnet.clusters$membership], vertex.label=NA, edge.color="grey") ## Exportation write.graph(fbnet.lcc, file="fblcc.graphml", format="graphml")