Visualizing networks by hierarchical-edge-bundling using R

library(igraph)
library(ape)
library(adephylo)
library(grid)
library(Hmisc)
require(Matrix)
require(lattice)
w1<-read.csv(“D:/research/The flow structure on the WWW/data/cleaned clickstream network/googleflowweb20101222.csv”,header=F)
g=as.undirected(graph.data.frame(w1[,1:2]))
#E(f1)$weight <-w1[,3]/(1.97*10^9)
#using this source to generate weighted matrix
#http://www.stanford.edu/~messing/Affiliation%20Data.html

#————-prepare hierarchical structure——–#

#g = graph.data.frame(read.table(“D:/karate.txt”,sep=” “))
#A = get.adjacency(g)
#d = degree(g)
#D = as.matrix(diag(d,length(d)))
#L = D – A
#E = eigen(L)
#V = E$vectors[,length(E$values)-1]
#data = cbind(V(g),V)
#H = hclust(dist(data))
#plot(g,layout=layout.kamada.kawai,edge.arrow.size=0.2,vertex.color=NA,vertex.size=0)
#P = as.phylo(H)
wt <- walktrap.community(g, modularity=TRUE)
#dend <- as.dendrogram(wt, use.modularity=TRUE)
P = asPhylo(wt)
——————plot the circle dendropgram and get the positions of nodes———

plot(P,type=”fan”,show.tip.label =F,edge.color=”gray70″)
xy <- get(“last_plot.phylo”,envir=.PlotPhyloEnv)
xx <- xy$xx
yy <- xy$yy
points(xx,yy,col=”black”,pch=15,cex=0.5)
#text(xx,yy,col=”black”,1:length(xx),cex=0.6)

vl = read.csv(“D:/research/The flow structure on the WWW/data/finalList1.csv”,header=F)
vl = vl[match(V(g)$name,vl[,1]),]
strings = rep(“c”,length(xx)-length(V(g)$name))
vn = cbind(c(as.character(vl[,1]),strings),

c(as.character(vl[,2]),strings),c(as.character(vl[,3]),strings))
positionData = cbind(seq(length(xx)),xx,yy,vn)
write.csv(positionData,”D:/positionData.csv”,row.names=F)
positionData=read.csv(“D:/positionData.csv”,header=T)

—————finding shortest paths between vertices (tips)———–

search = function (n) {
nodepair = c(which(V(g)$name==es2[n,1]),which(V(g)$name==es2[n,2]))
s = sp.tips(P,nodepair[1],nodepair[2])
internalNodes = c(nodepair[1],unique(as.vector(unlist(s))),
nodepair[2])
t = subset(positionData,positionData[,1]%in%internalNodes)
t = t[match(internalNodes,t[,1]),]
row.names(t) = seq(nrow(t))
edge = rep(n,length(t[,1]))
t = cbind(edge,t)
return(t)
}
es1 = get.edgelist(g)
es2 = subset(es1,es1[,1]!=es1[,2])
paths = lapply(seq(nrow(es2)),search)
paths = do.call(rbind,paths)
write.csv(paths,”D:/paths.csv”,row.names=F)
paths = read.csv(“D:/paths.csv”,header=T)

—————for processing ————–
ls = as.vector(unique(subset(paths[,7],nchar(as.character(paths[,7]))>3)))

cleanBezierParameter=function(n){
p1 = subset(paths,paths[,1]==n)
if (nrow(p1)>3){return (
cbind( p1[1,3:4], p1[2,3:4], p1[nrow(p1)-1,3:4], p1[nrow(p1),3:4], which(ls==p1[1,7]), which(ls==p1[nrow(p1),7]))
)}
else {}
}
shortPaths = do.call(rbind,lapply(unique(paths[,1]),cleanBezierParameter))
write.csv(shortPaths,”D:/shortPaths.csv”,row.names=F)
————–plot curve——————

plotBezier=function(n){
data = subset(paths[,3:4],paths[,1]==n)
bz <- bezier(data[,1],data[,2])
lines(bz$x,bz$y, col=”lightblue”)
}

l = unique(paths[,1])
lapply(l,plotBezier)

————plot together—————-

plot(P,type=”fan”,show.tip.label =F,edge.color=NA)
lapply(seq(1,length(paths),1),plotBezier)
#text(xx[1:length(V(g))],yy[1:length(V(g))],col=”black”,1:length(V(g)),cex=0.6)

# ————– rotation of circle dendropgram——–

#tr1<-tr2<-rtree(10)
#par(mfrow=c(2,1))
#plot(tr1,type=”fan”)
#for(i in 1:tr2$Nnode) tr2<-rotate(tr2,i+length(tr2$tip))
#plot(tr2,type=”fan”)

————————–

2 Responses to “Visualizing networks by hierarchical-edge-bundling using R”

  1. supersambo says:

    Hi,
    I would like to display networks with hierarchical edge bundling in R and it seems that I’ve finally found what I was looking for. However it would be great to see the plot of this or if you could provide the necessary data, so I could reproduce this and see the plot.

    thank you very much.

    Best Regards,
    stephan

  2. zhao says:

    Hi Lingfei,

    I’m trying to reproduce your article: Visualizing networks by hierarchical-edge-bundling using R.

    Could you send me the data you’ve used (googleflowweb20101222.csv, finalList1.csv)?

    Thank you very much.

    Best,
    Zhao

Leave a Reply