From igraph to network and back again

[This article was first published on Brokering the Closure » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 ‘’ method for ‘igraph’ objects. Consequently, you can go back and forth between the two representations as in:

ig <- graph.full(10)
netg <-
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 <-
ig1 <- as.igraph(net1)
net2 <-
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="")

To install a Windows binary version use one of

install.packages("network", contriburl="")
install.packages("network", contriburl="")
install.packages("network", contriburl="")

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="")

Filed under: networks, R

To leave a comment for the author, please follow the link and comment on their blog: Brokering the Closure » R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)