From igraph to network and back again
In an effort to achieve this (last paragraph), I created a couple of functions to coerce networks as ‘igraph’ objects to networks as ‘network’ objects and vice versa. I wrapped them into a package called ‘intergraph’ which I just uploaded to my personal miniCRAN.
Please mind, this is still an experimental version! Might be bug-infested.
The package depends on ‘igraph’ and ‘network’ packages (big thanks to Gabor Csardi and Tamas Nepusz for ‘igraph’ and to Carter Butts and the Statnet team for ‘network’!). At the time of writing this the ‘network’ package available on CRAN (version 1.4-1) does not have a namespace. To be able to work with both ‘igraph’ and ‘network’ together reliably I had to create a version of the ‘network’ package with the namespace. To try ‘intergraph’ you will need to install the namespaced version. Installation instructions are at the bottom.
The package contain two major functions ‘as.igraph’, and a ‘as.network’ method for ‘igraph’ objects. Consequently, you can go back and forth between the two representations as in:
ig <- graph.full(10) netg <- as.network(ig) ig2 <- as.igraph(netg)
Because ‘network’ objects define some additional attributes (like ‘na’ etc.) which are not used by ‘igraph’ objects ‘ig’ and ‘ig2′ will not be exectly identical (in the sense of ‘identical()’) . The only potential differences though is due to these attributes.
Fooling around further:
net1 <- as.network(exIgraph) ig1 <- as.igraph(net1) net2 <- as.network(ig1) k <- layout.fruchterman.reingold(ig1) layout(matrix(1:4, 2, 2, byrow=TRUE)) plot(exIgraph, layout=k, main="Original 'igraph'") plot(net1, coord=k, main="'igraph' > 'network'", displaylabels=TRUE, label=net1 %v% "label") plot(ig1, layout=k, main="'igraph' > 'network' > 'igraph'") plot(net2, coord=k, main="'igraph' > 'network' > 'igraph' > 'network'", label=net2 %v% "label", displaylabels=TRUE)
which should produce a figure as shown.
I’m going to test this a bit still, and you are welcome to do that too.Bug reports, comments etc. are more than welcome. If everything is OK the package will be available through CRAN.
Namespaced ‘network’ package
To install namespaced version of the ‘network’ package (tagged with version 1.4-1-1) call:
To install a Windows binary version use one of
install.packages("network", contriburl="http://bojan.3e.pl/R/hacks/2.9") install.packages("network", contriburl="http://bojan.3e.pl/R/hacks/2.10") install.packages("network", contriburl="http://bojan.3e.pl/R/hacks/2.11")
depending on your R version. Sorry, no other R Windows versions supported at this time. I don’t have the facilities to build Mac binaries either.
If you don’t want to overwrite the version of the ‘network’ package you already have installed you can install it to a different directory (essentially separate R library tree) following these steps:
- Create a new directory somewhere, like
- Run one of the above commands with an additional argument ‘lib’ set to the path to that new directory. The package will be installed there.
- Now when you launch R to load the new package you have to either:
- Or let R know about the new library tree by calling
.libPaths("pathToWherePackageIsInstalled")then load the package with
I do hope the next version of ‘network’ will have a namespace.
The ‘intergraph’ package
To install the ‘intergraph’ package itself use