# Edgebundle with R – link colors based on network parameters

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
library(RColorBrewer)
library(igraph)
library(edgebundleR)
library(Hmisc)

# Load data from file.
# Calculate Correlation.
# The data has missing values, this rcorr function omits those
# missing values to calculate the correlation.
X<-rcorr(as.matrix(Networks))
# 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
nr<-kr
nr<-(nr-0.35)/0.65
kr1000 <- nr*1000
kr1000 <- round(kr1000)
kr1000[kr1000<0]<-0
# 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
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(
eb1,
'
function(el,x){
// loop through each of our edges supplied
//  and change the color
x.edges.map(function(edge){
var source = edge.from;
var target = edge.to;
d3.select(el).select(".link.source-" + source + ".target-" + target)
.style("stroke",d3.rgb(edge.color));
d3.select(el).select(".link.source-" + source + ".target-" + target)
.style("stroke-opacity",0.7);
d3.select(el).select(".link.source-" + source + ".target-" + target)
.style("stroke-width",1.5);
})
}
'
)
## Thats it.
eb1
```

And the network will look like this.