Read viz attributes from GEXF files

November 26, 2017
By

(This article was first published on Posts on GGVY, and kindly contributed to R-bloggers)

So one of the new features that I’ve working on is processing viz attributes. In the CRAN version of rgexf, the function read.gexf only reads in non-visual attributes and the graph structure itself, which is no longer true as of today (at least for the static viz attributes, all the other dynamic features supported by GEXF will come in the future).

We start by loading the R packages and reading the “lesmiserables.gexf” file that is included in rgexf. We use the gexf.to.igraph function to coerce the gexf object to an object of class igraph

# Loading R packages
library(rgexf)
library(igraph)

# Reading and coercing into an igraph object
fn      <- system.file("gexf-graphs", "lesmiserables.gexf", package="rgexf")
gexf1   <- read.gexf(fn)
igraph1 <- gexf.to.igraph(gexf1)

Here comes the nice new feature. read.gexf and gexf.to.igraph take into account the visual attributes of the network, and we can use those with plot.igraph directly without us specifying them! The only changes that I do in the next code chunk are rescaling the vertex and labels sizes (igraph automatically changes scales, which messes a bit with what we read from the GEXF object), and setting the edges to be curved and labels to be black using the sans font family, and this is what we get

# We set the mai = c(0,0,0,0) so we have more space for our plot
oldpar <- par(no.readonly = TRUE)
par(mai = rep(0,4))
plot(igraph1,
     vertex.size        = V(igraph1)$size/2,
     vertex.label.cex   = V(igraph1)$size/50,
     vertex.label.color = "black",
     edge.curved        = TRUE,
     vertex.label.family = "sans"
     )

par(oldpar)

And to make sure we are getting the same output, we can take a look at how the plot.gexf function draws our GEXF graph.

plot(
  gexf1,
  copy.only = TRUE,
  dir       = "../../static/post/read-viz-attributes-from-gexf-files/viz"
  )
## GEXF graph successfully written at:
## /home/george/Documents/website/static/post/read-viz-attributes-from-gexf-files/viz/network.gexf

As expected, the same result.

To leave a comment for the author, please follow the link and comment on their blog: Posts on GGVY.

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



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

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)