05. Plotting networks - Basics

**This page was updated on Feb 3, 2013, using igraph 0.6 on R 2.15

Note of caution:

One of the greatest benefits of organizing your data as a network is the ability to visualize the connections between the individuals (or whatever your nodes may be). In some cases, the power of pattern detection by the human eye may be extremely helpful in motivating further questions. However, a great caution is needed—such visualization exercises can potentially be misleading. This is at least in part due to our natural tendency to ascribe meaning to the spatial location of the nodes. However, in actual practice, the fact that one node is to the left or right of another node, or even that one node appears at the center of a cluster of other nodes, may mean nothing. It all depends on what algorithm you use, and the parameters you set in producing these figures. The problem is analogous to phylogenetic trees, in which any monophyletic groups can be flipped around at their common ancestor.

With that said, given a clear hypothesis for what you are looking for, you will likely be able to produce a beautiful figure that illustrates those patterns by using the appropriate algorithms. Both statnet and igraph give you this flexibility.

Let’s start with the simple default figure, using a sample adjacency matrix (provided at bottom of page):


------- In igraph -------

dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=FALSE)
m=as.matrix(dat)
g=graph.adjacency(m,mode="undirected",weighted=NULL,diag=FALSE)
plot.igraph(g)

For graphs with <100 nodes, the default layout in igraph 0.6 is now the Kamada-Kawai method, which uses a force-based algorithm. You can change the layout by specifying different algorithms. For example: 
plot.igraph(g,layout=layout.circle)

You can also use custom layouts. Here, I'll set up the vertices on a line and then used curved edges.


l=matrix(c(1,2,3,4,5,6,7, 1,2,3,4,5,6,7),ncol=2)
plot.igraph(g,layout=l,edge.curved=TRUE)


By default, vertices are labeled by their node indexing number (which used to start with 0 in igraph 0.5 but now starts with 1 in igraph 0.6). You can alter the vertex label, vertex color and edge color, etc.:

plot.igraph(g,vertex.label=V(g)$name,vertex.size=30,,vertex.label.color="yellow", vertex.label.font=2,vertex.color="darkblue",edge.color="black")

For a ton more arguments you can use to make your network look pretty, see ?igraph.plotting

-------In statnet:--------

library(statnet)
# import matrix data and convert to a network object. You can find the data I use here in the "importing data" section - download the sample_adjmatrix.csv file:
dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=FALSE)
m=as.matrix(dat)
net=network(m,matrix.type="adjacency",directed=FALSE)
#open a graphic window and plot the network as an 'undirected' graph
quartz()
gplot(net)


---------------------------------

... you can see that the default of the gplot() command in statnet is to produce a “digraph”—that is, a graph with directionality. If you want simple lines rather than arrows, use:

quartz()
gplot(net,gmode="graph")

-------------------------------------------------

... Notice also that these two plots looks a bit different. However, they are identical as far as their network structure goes.

The default algorithm for producing plots in statnet is the Fruchterman and Reingold's (1991) force-directed placement algorithm. However, there are 17 different algorithms you can use. You can also plot in 3D using the gplot3d() command, but I have not used it and will not go into it here. You can lookup all of the different layout options by using the ?gplot.layout help command.

Just as an example, here are two other plots of the exact same network, arranged in a circle and via multidimensional scaling:

-------------------------------------------------

gplot(net,gmode="graph",mode="circle")


gplot(net,gmode="graph",mode="mds")




ċ
sample_adjmatrix.csv
(0k)
Dai Shizuka,
Apr 27, 2012, 8:40 AM
Comments