# From igraph to network and back again

August 25, 2010
By

(This article was first published on Brokering the Closure » R, and kindly contributed to R-bloggers)

The Hobbit, or There and Back Again (by Alan Lee)

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.

## Installation instructions

### Namespaced ‘network’ package

To install namespaced version of the ‘network’ package (tagged with version 1.4-1-1) call:

install.packages("network", contriburl="http://bojan.3e.pl/R/hacks")


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:

1. Create a new directory somewhere, like ~/lib/R/dev or c:Rlibrary2 etc.
2. 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.
3. Now when you launch R to load the new package you have to either:
1. Use library(network, lib="pathToWherePackageIsInstalled")
2. Or let R know about the new library tree by calling .libPaths("pathToWherePackageIsInstalled") then load the package with library(network).

I do hope the next version of ‘network’ will have a namespace.

### The ‘intergraph’ package

To install the ‘intergraph’ package itself use

install.packages("intergraph", repos="http://bojan.3e.pl/R")


Filed under: networks, R

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...