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.
Do 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 asIgraph()
and a asNetwork()
method for ‘igraph’ objects. Consequently, you can go back and forth between the two representations as in:
library(igraph)
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
library(intergraph)
ig <- graph.full(10)
netg <- asNetwork(ig)
ig2 <- asIgraph(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:
library(network)
## network: Classes for Relational Data
## Version 1.16.0 created on 2019-11-30.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
## Mark S. Handcock, University of California -- Los Angeles
## David R. Hunter, Penn State University
## Martina Morris, University of Washington
## Skye Bender-deMoll, University of Washington
## For citation information, type citation("network").
## Type help("network-package") to get started.
##
## Attaching package: 'network'
## The following objects are masked from 'package:igraph':
##
## %c%, %s%, add.edges, add.vertices, delete.edges, delete.vertices,
## get.edge.attribute, get.edges, get.vertex.attribute, is.bipartite,
## is.directed, list.edge.attributes, list.vertex.attributes,
## set.edge.attribute, set.vertex.attribute
net1 <- asNetwork(exIgraph)
ig1 <- asIgraph(net1)
net2 <- asNetwork(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
)
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:
- Create a new directory somewhere, like
~/lib/R/dev
orc:\R\library2
etc. - 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:
- Use
library(network, lib="pathToWherePackageIsInstalled")
- Or let R know about the new library tree by calling
.libPaths("pathToWherePackageIsInstalled")
then load the package withlibrary(network)
.
- Use
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")