Working with R network objects
Here are a few things that I have learnt while working with R network objects, using the igraph
and network
+ sna
packages (the last two packages go well together).
This note is only concerned with some quirky internals of the packages mentioned above. For network visualization, readers should head to Katherine Ognyanova's excellent tutorial.
igraph
The igraph
package implements more placement algorithms than the sna
package does, and its implementations tend to accept more parameters, such as edge weights for the Fruchterman-Reingold and Kamada-Kawai algorithms.
As of version 1.1.0 of the package, edge rewiring does not play well with edge attributes. This is a serious issue if the aim of the rewiring is to randomise an edge value.
The development repository of the igraph
R package has lots of issues (over a hundred right now, half of which are still open), including some nasty namespace conflicts.
network
There are two different methods to set edge values on a network
object: set.edge.attribute
and set.edge.value
. The functions work identically, but perform completely different assignments.
Reading through the package documentation too quickly, I missed the subtle difference between set.edge.attribute
, which assigns values to edges in “sequential order”, and set.edge.value
(or the %e%
method, which is a shortcut for it), which assigns values in “adjacency matrix form”.
This Gist illustrates the difference between both methods (which I then thought to be a bug). Right below the Gist is a nice explanation by the author of the package of when to use each method.
sna
There is extensive namespace overlap between igraph
and network
+ sna
. Consequently, loading the packages together can be error-prone.
This pull request illustrates the problem through the degree
function of the sna
package. The function calls another sna
function, which itself calls the is.bipartite
function, expecting it to be that of the network
package. If the igraph
package has been loaded after the network
package, this function will have been masked and the code will break.
Here's the line of code that causes the error (the comment next to it reads quite ironically in the present context). Fixing the issue would only require editing two lines of code in the sna
package.
- First published on September 17th, 2015