igraph demonstration

References

Load packages

## network analysis
library(igraph)
## d3.js output
library(d3Network)
## Error: there is no package called 'd3Network'

Data Preparation and Checking (Facebook example from Stanford)

## Read in edges information
igraphDat <- read.graph(file = "./igraph.d/facebook/0.edges", directed = FALSE)
## Simplify to remove duplications and from-self-to-self loops
igraphDat <- simplify(igraphDat, remove.multiple = TRUE, remove.loops = TRUE)
## Give numbers
V(igraphDat)$label <- seq_along(V(igraphDat))

Network Characteristics

## Check the transitivity of a graph (probability that the adjacent vertices of a vertex are connected)
(transitivityDat <- transitivity(igraphDat, type = "localaverage",isolates = "zero"))
## [1] 0.4863
## Average path length between any two given nodes
(averagePathLength <- average.path.length(igraphDat))
## [1] 3.752
## Community structure detection based on edge betweenness
communityEdgeBetwn <- edge.betweenness.community(igraphDat)

Graph with characteristics

## Set the seed to get the same result
set.seed("20140513")
## Add community indicating background colors
plot(igraphDat,
     vertex.color = communityEdgeBetwn$membership, vertex.size = log(degree(igraphDat) + 1),
     mark.groups = by(seq_along(communityEdgeBetwn$membership), communityEdgeBetwn$membership, invisible))

## Annotate
title("Stanford Facebook data", sub = "http://snap.stanford.edu/data/egonets-Facebook.html")
text(x = -1, y = -1, labels = sprintf("Average path length: %.2f\nTransitivity: %.2f",
                         averagePathLength, transitivityDat))

plot of chunk unnamed-chunk-5

Individual Characteristics

## Set individuals of interest
individualsOfInterest <- c(20,24,48,153,216)

## Calculate for everybody
indDat <-
    data.frame(## Degree: number of connections
               degree      = degree(igraphDat),
               ## Betweenness: number of geodesics (shortest paths) going through a vertex.
               betweenness = betweenness(igraphDat),
               ## Closeness: centrality measures how many steps is required to access
               ## every other vertex from a given vertex.
               closeness   = closeness(igraphDat))

## Show for individuals of interest
indDat[individualsOfInterest,]
##     degree betweenness   closeness
## 20      15        9827 0.000105652
## 24      16        7155 0.000106315
## 48       1           0 0.000100735
## 153      4        2826 0.000104351
## 216      0           0 0.000008281

Graph with individuals of interest

## Remove label from others
V(igraphDat)$label[-1 * individualsOfInterest] <- ""

## Set the seed to get the same result
set.seed("20140513")
## Add community indicating background colors
plot(igraphDat,
     vertex.color = communityEdgeBetwn$membership, vertex.size = log(degree(igraphDat) + 1),
     mark.groups = by(seq_along(communityEdgeBetwn$membership), communityEdgeBetwn$membership, invisible))

## Annotate
title("Stanford Facebook data", sub = "http://snap.stanford.edu/data/egonets-Facebook.html")
text(x = -1, y = -1, labels = sprintf("Average path length: %.2f\nTransitivity: %.2f",
                         averagePathLength, transitivityDat))

plot of chunk unnamed-chunk-7

Characteristic individuals