No pages like "Network"!

Clear message

Network Analysis in R with igraph

A network can be described by a list of nodes, or vertices that interact with each other, thus a tab-delimited list:

Bj1     dah
Bj1     rst
Borr    CG6459
Borr    Rab11
BthD    CG6843
BthD    Mcm3

Read a table into R and generate a network graph:

data <- read.table(file="my_interactions.txt",sep="\t")

# create a graph data object
g <- graph.data.frame(data, directed=F)

# draw the network!
plot(g)

It can look a bit amorphous and un interpretable. Draw it with a "force-directed algorithm:

plot(g,layout=layout.fruchterman.reingold)

If your network contains un-connected pieces, you can find them using the deconstruct function.

dg <- decompose.graph(g)

This returns a list, whereby each element is itself a graph data object. (If your network doesn't have any unconnected pieces, I'm not sure what happens yet.) You can use see the sub pieces in the main network figure by highlighting them in color.

To do this, you use the V() command to access and set various attributes of the Vertices. For instance, the first chunk of my decomposed network is found in dg[[1]]. We can get the node names from this and set a display color for the matching names in the main network object:

# take the sub network to a new variable for clarity
a <- dg[[1]]

# create an index vector to match names
iv <- match(V(a)$name, V(g)$name)

# set the vertex colors using the index vector
V(g)$color[iv] <- "red"

# draw the map
plot(g,layout=layout.fruchterman.reingold,vertex.size=5)

It can also be of interest to identify clusters in the network map.

# find clusters
clust <- clusters(g)

# get a list of colors equal in length to the number of clusters
cols <- rainbow(33)

# set the colors of each cluster based on membership
# member ship is counted from 0, whereas arrays are indexed from 1
# so use +1 in the array index
V(g)$color <- cols[clust$membership+1]