library(igraph) edgelist <- as.matrix(read.table("fbnet-el.txt")) vnames <- read.table("fbnet-name.txt") vnames <- as.character(vnames$V1) ## with 'graph.edgelist' fbnet0 <- graph.edgelist(edgelist, directed=FALSE) # network summary fbnet0 # vertices V(fbnet0) vcount(fbnet0) V(fbnet0)$initials <- vnames 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 graph.density(fbnet.lcc) # [1] 0.0724834 transitivity(fbnet.lcc) # [1] 0.5604524 ## 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 fbnet.lcc$layout <- layout.fruchterman.reingold(fbnet.lcc) V(fbnet.lcc)$label <- V(fbnet.lcc)$initials plot(fbnet.lcc, main="Fruchterman & Reingold layout", vertex.size=3, vertex.color="pink", vertex.frame.color="pink", vertex.label.color="darkred", edge.color="grey") ## 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 <- spinglass.community(fbnet.lcc) fbnet.clusters # Graph community structure calculated with the spinglass algorithm # Number of communities: 9 # Modularity: 0.5654136 # Membership vector: # [1] 9 5 6 5 5 9 1 9 1 1 4 9 9 6 2 7 2 7 2 7 5 5 3 8 1 4 7 9 4 4 2 7 7 4 5 9 3 # [38] 6 7 5 7 9 4 5 7 3 4 9 9 8 9 6 9 9 5 9 9 9 9 9 4 9 7 9 9 9 4 2 9 5 4 9 5 4 # [75] 3 4 9 4 4 4 1 7 4 4 7 4 4 4 9 3 4 4 7 7 4 6 5 4 4 1 4 5 3 4 4 4 7 7 3 6 5 # [112] 4 1 4 2 2 1 3 7 4 6 7 table(fbnet.clusters$membership) # 1 2 3 4 5 6 7 8 9 # 8 7 8 32 14 7 18 2 26 V(fbnet.lcc)$community <- fbnet.clusters$membership fbnet.lcc # IGRAPH U--- 122 535 -- # + attr: layout (g/n), initials (v/c), label (v/c), size (v/n), color (v/c), # community (v/n) par(mfrow=c(1,1)) par(mar=rep(1,4)) plot(fbnet.lcc, main="Communities", vertex.frame.color=rainbow(9)[fbnet.clusters$membership], vertex.color=rainbow(9)[fbnet.clusters$membership], vertex.label=NA, edge.color="grey") ## Exportation write.graph(fbnet.lcc, file="fblcc.graphml", format="graphml")