# Graph theory — analysis and visualization

I am not sure the subject enters into the CrossValidated interest. You'll tell me.

I have to study a graph (from the graph theory) ie. I have a certain number of dots that are connected. I have a table with all the dots and the dots each one is dependant on. (I have also another table with the implications)

My questions are:
Is there a good software (or a R package) to study that easily?
Is there an easy way to display the graph?

## 6 Answers

iGraph is a very interesting cross-language (R, Python, Ruby, C) library. It allows you to work with unidirected and directed graphs and has quite a few analysis algorithms already implemented.

• (+1) It seems my response came after yours. As your response highlights somewhat a different perspective (cross-platform, algorithms), I feel like our responses are not so redundant, but I can remove mine without any prob. – chl Jan 11 '11 at 10:23

There are various packages for representing directed and undirected graphs, incidence/adjacency matrix, etc. in addition to graph; look for example at the gR Task view.

For visualization and basic computation, I think the igraph package is the reliable one, in addition to Rgraphviz (on BioC as pointed out by @Rob). Be aware that for the latter to be working properly, graphviz must be installed too. The igraph package has nice algorithms for creating good layouts, much like graphviz.

Here is an example of use, starting from a fake adjacency matrix:

adj.mat <- matrix(sample(c(0,1), 9, replace=TRUE), nr=3)
g <- graph.adjacency(adj.mat)
plot(g)


• Thanks for your answer. graphviz is not easy to install with R, but it seams to be a great library – RockScience Feb 28 '11 at 6:38
• FYI what I do now is that I generate the graphviz code with R and I read it in a mediawiki using the mediawiki graphviz extension. (The Rgraphviz package is not easy to install and doesnt work with the last version of R) – RockScience Dec 27 '11 at 1:16
• "Package ‘graph’ was removed from the CRAN repository." – bartektartanus Apr 15 '17 at 0:56

Aside from what has been said, for the vusualization task alone (and outside from R), you might be interested in checking Gephi.

Another option is the statnet package. Statnet has functions for all the commonly used measures in SNA, and can also estimate ERG models. If you have your data in an edge list, read in the data as follows (assuming your data frame is labelled "edgelist"):

net <- as.network(edgelist, matrix.type = "edgelist", directed = TRUE) #if the network is directed, otherwise: directed = FALSE


If your data is in an adjacency matrix you replace the matrix.type argument with "adjacency":

net <- as.network(edgelist, matrix.type = "adjacency", directed = TRUE)


The statnet package has some very nice plotting capabilities. To do a simple plot simply type:

gplot(net)


To scale the nodes according to their betweenness centrality, simply do:

bet <- betweenness(net)
gplot(net, vertex.cex = bet)


By default the gplot function uses Fruchterman-Reingold algorithm for placing the nodes, however this can be controlled from the mode option, for instance to use MDS for the placement of nodes type:

gplot(net, vertex.cex, mode = "mds")


or to use a circle layout:

gplot(net, vertex.cex, mode = "circle")


There are many more possibilities, and this guide covers most of the basic options. For a self contained example:

net <- rgraph(20) #generate a random network with 20 nodes
bet <- betweenness(net) #calculate betweenness scores
gplot(net) #a simple plot
gplot(net, vertex.cex = bet/3) #nodes scaled according to their betweenness centrality, the measure is divided by 3 so the nodes don't become to big.
gplot(net, vertex.cex = bet/3, mode = "circle") #with a circle layout
gplot(net, vertex.cex = bet/3, mode = "circle", label = 1:20) #with node labels

• (+1) Never used this package, but your overview suggest I shoud try it. Seems good at first sight. – chl Jan 11 '11 at 10:26

A similar question was asked on cstheory, also if you are specifically interested in planar graphs, or bibliographic visualization.

Gephi was already mentioned here, but it was also recommended by a few on cstheory, so I think that is a good choice.

Other cool options include:

• Flare provides some really cool visualization tools and create very pretty graphics for reports and papers.
• Cyptoscape has some very powerful analysis and visualization tools. It is particularly good for chemistry and molecular biology.
• This website provides links to many other nice visualization tools and libraries (although not for R).

I found NodeXL very helpful and easy to use. It is an MS Excel template that provides easy import / export of a graph, formatting of edges / vertices, calculates some metrics, has some clustering algorithms. You can easily use custom images as vertices.
Another helpful tool for me was Microsoft Automatic Graph layout which provides good layout can be tried online (with a browser that supports SVG).