Edgebundle with R – link colors based on network parameters

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on StumbleUponShare on Tumblr

This post is based on a request my supervisor did, to make a network graph to use in a presentation. Idea was straightforward. Use Pearson correlation to calculate the relationship between every two variables and plot a network to visualize the relationship.
However, given that the number of variables in question is relatively low, I wanted to make sure that the visualization is of circos nature to make it more understandable.
Luckily, I got hold of an amazing library edgebundleR to draw an edgebundle network graph. However, to the best of my knowledge, changing the color of the link between two nodes are not straight forward if you need it to be based on an external variable other than the color of the originating node. However, I wanted the link color to represent the correlation between the nodes. So I followed a StackOverflow answer to achieve this. Here is the code below and I added comments on the way. Hope this helps.

# Load Libraries

# Load data from file. 
Networks <- read.csv("~/Documents/Networks2.csv")
# Calculate Correlation. 
# The data has missing values, this rcorr function omits those
# missing values to calculate the correlation. 
# Following section is done to extract the correlation values and normalize them to 
# 0-1 range to use the full color scale. I have used a threshold of r > 0.35. 
# This is completly up to the user to change as needed. 
kr = X$r
kr[kr < 0.35] <- 0
kr[kr == 1] <- 0
kr1000 <- nr*1000
kr1000 <- round(kr1000)
# Create the graph
g1<-graph.adjacency(kr, mode = 'undirected', weighted=TRUE)
## Optional: If you need some netwoek parameters. 
V(g1)$comm <- membership(optimal.community(g1))
V(g1)$degree <- degree(g1)
V(g1)$closeness <- centralization.closeness(g1)$res
V(g1)$betweenness <- centralization.betweenness(g1)$res
V(g1)$eigen <- centralization.evcent(g1)$vector
# Create the edgebundle graph
edgebundle( g1, tension = 0.4 )->eb1
# Create the color pallet
pallet<-colorRampPalette(rev(brewer.pal(8, "Spectral")))
# Create a new color vector based on te lower traingle of the correlation matrix
cols<-pallet(1000)[kr1000[lower.tri(kr, diag = FALSE)]]
# Set colors in the edgebudle graph
E(g1)$color <- cols
eb1$x$edges <- jsonlite::toJSON(get.data.frame(g1,what="edges"))
# This is the workaround to recolor the links on render. You can change the stroke styles
# to chnage their parameters here. 
eb1 <- htmlwidgets::onRender(
  // loop through each of our edges supplied
  //  and change the color
  var source = edge.from;
  var target = edge.to;
  d3.select(el).select(".link.source-" + source + ".target-" + target)
  d3.select(el).select(".link.source-" + source + ".target-" + target)
  d3.select(el).select(".link.source-" + source + ".target-" + target)
## Thats it. 

And the network will look like this.

Screen Shot 2016-06-02 at 10.24.16 AM

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on StumbleUponShare on Tumblr

Leave a Reply

Your email address will not be published. Required fields are marked *