Convert igraph R objects to Gexf GePhi format

GePhi has interesting visualization capabilities built around graphs - and igraph is one of the widely used graph processing libraries for R. But there is no straightway to combine these two at present in R. The gexf format used by GePhi is not currently supported by the igraph package.

GePhi has its own package, the rgexf package for R, that provides some support for creating GePhi styled graphs out of custom matrix/dataframe data. It would have been great if it accepted an igraph object straight out-of-the box and generated a gexf object from it. But the current version does not support it. However, rolling out your own exporter is not that difficult. Here is something to get you started.

   # load the igraph and rgexf packages
  library(igraph)
  library(rgexf)

  # create a sample igraph object
  cg1 <- erdos.renyi.game(10, 0.8)

  # construct the nodes and edges data for gexf conversion
  nodes <- data.frame(cbind(V(cg1), as.character(V(cg1))))
  edges <- t(Vectorize(get.edge, vectorize.args='id')(cg1, 1:ecount(cg1)))

  # do the conversion
  write.gexf(nodes, edges)    

This piece of code will generate xml formatted in gexf ready for GePhi. You can further add node attributes and edge weights in similar fashion.

There are couple of points to be taken care though, such as xml & problem. For some reason, write.gexf fails to properly handle & symbols in the attribute vectors, so you would have to convert the & symbol to & before sending it to write.gexf.

Also note that write.gexf writes to console output and does not save to file. You would have to use print method for that. This is somewhat confusing naming convension !!

A reusable method with complete source code for exporting the igraph nodes and edges along with the associated attribute data can be found here.