Code Rnet 1

Example network in Pajek format

% HSE example 1; December 4, 2018
*vertices 6
1 a
2 f
3 c
4 b
5 d
6 e
*arcs
1 4
2 6
3 2
4 5
     5       1
6 3
*edges
1 5
3 4
3 3

Our network HSEexample1.net has no coordinates data. Pajek puts nodes on the circle:

ex1circ.svg

We changed some weights and computed the alldegree vector and indegree partition. We used them in the visualization. After some changes of graphical options we get:

ex1.eps ex1.pdf ex1.svg

Using Inkscape we enhanced the obtained SVG picture: changing a label, improving the shape and position of a loop, changing the position of parallel links, adding text, adding region, …

ex1ink.svg ex1ink.pdf

We saved the network as a Pajek project file HSEexample1.paj and the graphical settings on the file HSEexample1.ini.

Examples from the network collection

Collection 2015

C:\Users\batagelj\Documents\papers\2017\Moscow\Rnet\test\nets

  • tinaSet - visualization, criteria
  • class
  • SaKtrade - matrix permutation
  • USAair - ZOOM
  • mreza
  • GR30_60 - 3D

igraph

> setwd("C:/Users/batagelj/Documents/papers/2018/moskva/december/nets")
> library(igraph)
> links <- c("A","C", "A","G",
+   "B","C", "B","A", "B","E",
+   "C","A", "C","E", "D","B", 
+   "D","G", "D","E", "E","A",
+   "F","E", "G","C", "G","F")
> L <- graph(links)
> L
IGRAPH bb7e45b DN-- 7 14 -- 
+ attr: name (v/c)
+ edges from bb7e45b (vertex names):
 [1] A->C A->G B->C B->A B->E C->A C->E D->B D->G D->E E->A F->E G->C G->F
> plot(L)
> vcount(L)
[1] 7
> ecount(L)
[1] 14
> L <- L + vertex("H")
> plot(L)
> V(L)
+ 8/8 vertices, named, from 84e744b:
[1] A C G B E D F H
> E(L)
+ 14/14 edges from 84e744b (vertex names):
 [1] A->C A->G B->C B->A B->E C->A C->E D->B D->G D->E E->A F->E G->C G->F
> V(L)$name
[1] "A" "C" "G" "B" "E" "D" "F" "H"
> V(L)$name[5] <- "John"
> V(L)$color <- sample(c("yellow","cyan"),vcount(L),rep=TRUE)
> plot(L)
> ye <- V(L)[color=="yellow"]; cy <- V(L)[color=="cyan"]
> E(L)[ye %--% cy]$color <- "red"
> E(L)[ye %--% ye]$color <- "blue"
> E(L)[cy %--% cy]$color <- "blue"
> L$name <- "Example"
> E(L)$weight <- sample(1:10,ecount(L),rep=TRUE)
> graph_attr_names(L)
[1] "name"
> graph_attr(L)
$name
[1] "Example"
> vertex_attr_names(L)
[1] "name"  "color"
> edge_attr_names(L)
[1] "color"  "weight"
> w <- E(L)$weight;  plot(L,edge.width=w)
> write.graph(L,"Links.net",format="pajek")
> L
IGRAPH b8d9e7d DNW- 8 14 -- Example
+ attr: name (g/c), name (v/c), color (v/c), color (e/c), weight (e/n)
+ edges from b8d9e7d (vertex names):
 [1] A   ->C    A   ->G    B   ->C    B   ->A    B   ->John C   ->A    C   ->John
 [8] D   ->B    D   ->G    D   ->John John->A    F   ->John G   ->C    G   ->F   
> 

igraphdata

> library(igraphdata)
> data(package="igraphdata")
> data(UKfaculty) 
> ?UKfaculty
> UKfaculty
IGRAPH 6f42903 D-W- 81 817 -- 
+ attr: Type (g/c), Date (g/c), Citation (g/c), Author (g/c), Group (v/n),
| weight (e/n)
+ edges from 6f42903:
 [1] 57->52 76->42 12->69 43->34 28->47 58->51  7->29 40->71  5->37 48->55  6->58 21-> 8
[13] 28->69 43->21 67->58 65->42  5->67 52->75 37->64  4->36 12->49 19->46 37-> 9 74->36
[25] 62-> 1 15-> 2 72->49 46->62  2->29 40->12 22->29 71->69  4-> 3 37->69  5-> 6 77->13
[37] 23->49 52->35 20->14 62->70 34->35 76->72  7->42 37->42 51->80 38->45 62->64 36->53
[49] 62->77 17->61  7->68 46->29 44->53 18->58 12->16 72->42 52->32 58->21 38->17 15->51
[61] 22-> 7 22->69  5->13 29-> 2 77->12 37->35 18->46 10->71 22->47 20->19 19->31 68->13
[73] 49->69 30->63  5->49 53->75 62->57 73->81 29->69 71->40 19->58 49->42 37-> 5 18-> 2
+ ... omitted several edges
> 

Data sets in package igraphdata:

Koenigsberg Bridges of Koenigsberg from Euler's times
UKfaculty Friendship network of a UK university faculty
USairports US airport network, 2010 December
enron Enron Email Network
foodwebs A collection of food webs
immuno Immunoglobulin interaction network
karate Zachary's karate club network
kite Krackhardt's kite
macaque Visuotactile brain areas and connections
rfid Hospital encounter network data
yeast Yeast protein interaction network

Saving igraph network as Pajek project file

igraph's write.graph function produces incorrect Pajek's net file:

  • color names are not compatible with Pajek's names; they are exported in quotes ” ;
  • node labels are not exported

Here is a partial solution igraph+.R for exporting network as a Pajek project file:

empty <- character(0)

normalize <- function(x,marg=0) return ((1-2*marg)*(x-min(x))/(max(x)-min(x))+marg)

write.graph.paj <- function(N,file="test.paj",vname="name",coor=NULL,va=NULL,ea=NULL,
  weight="weight",ecolor="color"){
  n <- gorder(N); m <- gsize(N); ga <- graph_attr_names(N)
  if(is.null(va)) va <- vertex_attr_names(N)
  if(is.null(ea)) ea <- edge_attr_names(N)
  va <- union(va,vname); ea <- union(ea,weight)
  paj <- file(file,"w")
  cat("*network",file,"\n",file=paj)
  cat("% saved from igraph ",format(Sys.time(), "%a %b %d %X %Y"),"\n",sep="",file=paj)
  for(a in ga) cat("% ",a,": ",graph_attr(N,a),"\n",sep="",file=paj)
  cat('*vertices ',n,'\n',file=paj)
  lab <- if(vname %in% va) vertex_attr(N,vname) else paste("v",1:n,sep="") 
  if(is.null(coor)){  
    if(vname %in% va) for(v in V(N)) cat(v,' "',lab[v],'"\n',sep="",file=paj)
  } else { 
    for(v in V(N)) cat(v,' "',lab[v],'" ',paste(coor[v,],collapse=" "),'\n',sep="",file=paj) 
  }
  va <- setdiff(va,vname)
  cat(ifelse(is_directed(N),"*arcs\n","*edges\n"),file=paj)
  K <- ends(N,E(N),names=FALSE) 
  w <- if(weight %in% ea) edge_attr(N,weight) else rep(1,m)
  if(ecolor %in% ea){ C <- edge_attr(N,ecolor)
    for(e in 1:m) cat(K[e,1]," ",K[e,2]," ",w[e]," c ",as.character(C[e]),"\n",sep="",file=paj)
  } else 
    for(e in 1:m) cat(K[e,1]," ",K[e,2]," ",w[e],"\n",sep="",file=paj)
  ea <- setdiff(ea,c(weight,ecolor)); nr <- 1
  for(a in ea){nr <- nr+1; w <- edge_attr(N,a)
    cat(ifelse(is_directed(N),"*arcs","*edges"),file=paj)
    cat(" :",nr,' "',a,'"\n',sep="",file=paj)
    if(is.numeric(w)){
      for(e in 1:m) cat(K[e,1]," ",K[e,2]," ",w[e],"\n",sep="",file=paj)
    } else if(is.character(w)){ 
      W <- factor(w); lev <- levels(W)
      for(i in seq_along(lev)) cat("%",i,"-",lev[i],"\n",file=paj)
      for(e in 1:m) cat(K[e,1]," ",K[e,2]," ",W[e],' l "',w[e],'"\n',sep="",file=paj)
    } else warning(paste("unsupported type of",a),call.=FALSE)
  }
  cat("\n",file=paj)
  for(a in va){
    S <- vertex_attr(N,a); ok <- TRUE
    if(is.character(S)){
      cat("*partition ",a,"\n",sep="",file=paj)
      s <- factor(S); lev <- levels(s)
      for(i in seq_along(lev)) cat("%",i,"-",lev[i],"\n",file=paj)
    } else if(is.numeric(S)){ 
      s <- S; cat("*vector ",a,"\n",sep="",file=paj) 
    } else {warning(paste("unsupported type of",a),call.=FALSE); ok <- FALSE}
    if(ok){cat('*vertices ',n,'\n',file=paj)
      for(v in 1:n) cat(s[v],"\n",file=paj)
      cat("\n",file=paj) }
  }
  close(paj)
}

Additional options should be included and some temporary quantities eliminated.

> source("https://raw.githubusercontent.com/bavla/Rnet/master/R/igraph+.R")
# source("./igraph+.R")
> P <- tkplot(L,500,500,edge.curved=0.25,edge.width=w)
> coor <- tkplot.getcoords(P,norm=F)
> xy <- cbind(normalize(coor[,1],marg=0.05),normalize(coor[,2],marg=0.05))
> source("./igraphEx.R")
> write.graph.paj(L,"networkB.paj",coor=xy)
> source("./igraph+.R")
> data(USairports)
> ?USairports 
> USairports
> write.graph.paj(USairports,"USairports.paj",weight="Departures",va=empty,ea=empty)
> write.graph.paj(USairports,"USairports2.paj",weight="Departures",vname="City",va="City",ea=c("Seats","Passengers"))

Network from a sequence

Suppose that a sequence S = s1, s2, s3, …, sk-1, sk is given. It determines a directed “summary” network N = (V,A) where the set of nodes V is the set of all different elements (values) in S, and the set (list) of arcs A consists of all pairs (si, si+1), i = 1, …, k-1.

For an example we will take for a sequence the letters from a given text. The network construction can be applied to any sequence (pages on the web site visited by a user, attractions in a city or region visited by a tourist, …).

> setwd("C:/Users/batagelj/Documents/papers/2018/moskva/december/nets")
> s <- "we changed some weights and computed the alldegree vector and indegree partition"
> k <- nchar(s)
> S <- substring(s,1:k,1:k)
> F <- factor(S)
> V <- levels(F); n <- length(V)
> from <- as.integer(factor(S[1:(k-1)],levels=V))
> to <- as.integer(factor(S[2:k],levels=V))
> net <- file("seq.net","w"); cat('*vertices ',n,'\n',file=net)
> for(v in 1:n) cat(v,' "',V[v],'"\n',sep='',file=net)
> cat('*arcs\n',file=net)
> for(a in 1:(k-1)) cat(from[a],' ',to[a],'\n',sep='',file=net)
> close(net)

Using igraph

> setwd("C:/Users/batagelj/Documents/papers/2018/moskva/december/nets")
> library(igraph)
> source("https://raw.githubusercontent.com/bavla/Rnet/master/R/igraph+.R")
> s <- "we changed some weights and computed the alldegree vector and indegree partition"
> k <- nchar(s)
> S <- substring(s,1:k,1:k)
> G <- make_directed_graph(rbind(S[1:(k-1)],S[2:k]))
> graph_attr(G)$name <- s
> G
IGRAPH c07d9bb DN-- 19 79 -- we changed some weights and computed the alldegree vector an
+ attr: name (g/c), name (v/c)
+ edges from c07d9bb (vertex names):
 [1] w->e e->   ->c c->h h->a a->n n->g g->e e->d d->   ->s s->o o->m m->e e->   ->w w->e
[18] e->i i->g g->h h->t t->s s->   ->a a->n n->d d->   ->c c->o o->m m->p p->u u->t t->e
[35] e->d d->   ->t t->h h->e e->   ->a a->l l->l l->d d->e e->g g->r r->e e->e e->   ->v
[52] v->e e->c c->t t->o o->r r->   ->a a->n n->d d->   ->i i->n n->d d->e e->g g->r r->e
[69] e->e e->   ->p p->a a->r r->t t->i i->t t->i i->o o->n
> write.graph.paj(G,"seqi.paj")

CSV2Pajek

Data at Github.

# setwd("C:/Users/batagelj/work/Python/graph/SVG/EUSN")
# colC <- c(rep("character",4),rep("numeric",7)); nas=c("","NA","NaN")
colC <- c(rep("character",4),rep("numeric",5)); nas=c("","NA","NaN")
nodes <- read.csv2("bibNodes.csv",encoding='UTF-8',colClasses=colC,na.strings=nas)
n <- nrow(nodes); M <- factor(nodes$mode); S <- factor(nodes$sex)
mod <- levels(M); sx <- levels(S); S <- as.numeric(S); S[is.na(S)] <- 0
links <- read.csv2("bibLinks.csv",encoding='UTF-8',colClasses="character")
F <- factor(links$from,levels=nodes$name,ordered=TRUE)
T <- factor(links$to,levels=nodes$name,ordered=TRUE)
R <- factor(links$relation); rel <- levels(R)
net <- file("bib.net","w"); cat('*vertices ',n,'\n',file=net)
clu <- file("bibMode.clu","w"); sex <- file("bibSex.clu","w")
cat('%',file=clu); cat('%',file=sex)
for(i in 1:length(mod)) cat(' ',i,mod[i],file=clu)
cat('\n*vertices ',n,'\n',file=clu)
for(i in 1:length(sx)) cat(' ',i,sx[i],file=sex)
cat('\n*vertices ',n,'\n',file=sex)
for(v in 1:n) {
  cat(v,' "',nodes$name[v],'"\n',sep='',file=net);
  cat(M[v],'\n',file=clu); cat(S[v],'\n',file=sex)
}
for(r in 1:length(rel)) cat('*arcs :',r,' "',rel[r],'"\n',sep='',file=net)
cat('*arcs\n',file=net)
for(a in 1:nrow(links))
  cat(R[a],': ',F[a],' ',T[a],' 1 l "',rel[R[a]],'"\n',sep='',file=net)
close(net); close(clu); close(sex)

igraph to netJSON

To do: add node attributes and link attributes

# export igraph network in netSON basic format
# by Vladimir Batagelj, December 2018
# based on transforming CSV files to JSON file, by Vladimir Batagelj, June 2016 
# setwd("C:/Users/batagelj/Documents/papers/2018/moskva/december/nets")
library(igraph)
library(rjson)

write.graph.netJSON <- function(N,file="test.json",vname="name" ){
  n <- gorder(N); m <- gsize(N); dir <- is_directed(N)
  lType <- ifelse(dir,"arc","edge")
  va <- vertex_attr_names(N); ea <- edge_attr_names(N)
  vlab <- if(vname %in% va) vertex_attr(N,vname) else paste("v",1:n,sep="")
  va <- setdiff(va,vname)  
  nods <- vector('list',n); lnks <- vector('list',m)
  today <- format(Sys.time(), "%a %b %d %X %Y")
  for(i in 1:n) { L <- list(id=i,name=vlab[i]) 
    for(a in va) L[[a]] <- vertex_attr(N,a)[i]
    nods[[i]] <- L }
  for(i in 1:m) {uv <- ends(N,i,names=FALSE); u <- uv[1]; v <- uv[2]
    L <- list(id=i,type=lType,n1=u,n2=v)
    for(a in ea) L[[a]] <- edge_attr(N,a)[i]
    lnks[[i]] <- L }
  meta <- list(date=today,title="saved from igraph")
  leg <- list(mode="mod",sex="sx",rel="rel")
  inf <- graph_attr(N)
  if("name" %in% names(inf)) {inf["title"] <- inf$name; inf[["name"]] <- NULL}
  inf["network"] <- "bib"; inf["org"] <- 1
  inf["nNodes"] <- n; 
  if(dir) {inf["nArcs"] <- m; inf["nEdges"] <- 0} else {inf["nArcs"] <- 0; inf["nEdges"] <- m}
  inf[["legend"]] <- leg; 
  if("meta" %in% names(inf)) { k <- length(inf[["meta"]]); inf[["meta"]][[k+1]] <- meta
  } else inf[["meta"]] <- meta
  data <- list(netJSON="basic",info=inf,nodes=nods,links=lnks)
  json <- file(file,"w"); cat(toJSON(data),file=json); close(json)
}

> setwd("C:/Users/batagelj/Documents/papers/2018/moskva/december/nets")
> library(igraph)
> library(igraphdata)
> data(package="igraphdata")
> data(karate)
> library(igraph+)
> write.graph.netJSON(karate,file="karate.json")

Rnet

ru/hse/rnet18/crnet1.txt · Last modified: 2018/12/14 04:09 by vlado
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki