## Archive for the ‘R’ Category

### Visualizing networks by hierarchical-edge-bundling using R

Thursday, December 27th, 2012

library(igraph)
library(ape)
library(grid)
library(Hmisc)
require(Matrix)
require(lattice)
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=” “))
#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))
#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)

—————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)

—————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”)

————————–

### Analyzing network data using the igraph library in R

Thursday, December 27th, 2012

library(igraph)

#========import data and construct a graph========#

fg = graph.data.frame(f[,1:2])

#========network statistics==================#

#———-statistics of networks———————-#

length(V(fg))
length(E(fg))
average.path.length(fg,directed = “Ture”)
transitivity(fg,”global”)
graph.density(fg)
cgw = clusters(fg, mode=c(“weak”))
cgs = clusters(fg, mode=c(“strong”))
c(cgw\$no,cgs\$no)
nCluster = cbind(V(fg)\$name,cgw\$member,cgs\$member)

#———-statistics of nodes———————-#

indegree = degree(fg, mode = “in”)
outdegree = degree(fg, mode = “out”)
betweeness = betweenness(fg,directed =”T”)
incloseness = closeness(fg, mode =”in”)
outcloseness = closeness(fg, mode = “out”)
clusteringcoefficient = transitivity(fg,”local”)
pagerank = page.rank(fg)\$vector
nStat = cbind(indegree, outdegree, betweeness, incloseness,
outcloseness, clusteringcoefficient, pagerank)
colMeans(nStat)

png(filename=”D:/nstat.png”, height=700, width=700, bg=”white”)
par(mfrow=c(3,2))
plot(indegree,outdegree,log=”xy”,col=”purple3″)
plot(indegree,betweeness,log=”xy”,col=”blue3″)
plot(indegree,incloseness,log=”xy”,col=”green3″)
plot(indegree,outcloseness,log=”xy”,col=”gold3″)
plot(indegree,clusteringcoefficient,log=”xy”,col=”red3″)
plot(indegree,pagerank,log=”xy”,col=”pink3″)
dev.off()

#=======community detection ==============#

fgu = as.undirected(graph.data.frame(f[,1:2]))
positionu = positions[match(V(fgu)\$name,positions[,1]),]
positiond = positions[match(V(fg)\$name,positions[,1]),]

le = leading.eigenvector.community(fgu) #sizes(le)
V(fgu)\$color = rainbow(1)[as.vector(membership(le))]
plot(fgu,vertex.size = 5, vertex.label = NA, edge.arrow.size=0.4,

main = “leading eigenvector community”,

layout=as.matrix(positionu[,2:3]))

#———- fastgreedy.community (undirected) ————#

fc = fastgreedy.community(fgu) #sizes(fc)
V(fgu)\$color = rainbow(10)[as.vector(membership(fc))]
plot(fgu,vertex.size = 4, vertex.label = NA, edge.arrow.size=0.2

, main = “fastgreedy community”,

layout=as.matrix(positionu[,2:3]))

#———label.propagation.community (undirected) ———–#

lp = label.propagation.community(fgu) #sizes(lp)
V(fgu)\$color = rainbow(3)[as.vector(membership(lp))]
plot(fgu, vertex.size = 4, vertex.label = NA, edge.arrow.size=0.2
, main = “label propagation community”, layout=as.matrix(positionu[,2:3]))

#—————- walktrap.community (directed) ————-#

wt = walktrap.community(fg) #sizes(wt)
V(fg)\$color <- rainbow(15)[as.vector(membership(wt))]
plot(fg,vertex.size = 4, vertex.label = NA, edge.arrow.size=0.2
, main = “walktrap community”, layout=as.matrix(positiond[,2:3]))

#——-spinglass & edge.betweenness.community (undirected)———-#

sc = spinglass.community(fgu, spins=20) #sizes(sc)
eb = edge.betweenness.community(fgu)#sizes(eb)

png(filename=”D:/communities.png”, height=700, width=700, bg=”white”)
par(mfrow=c(2,2))
par(mar=c(0, 0, 0, 0))
#plot 4 communities
dev.off()