Ch3

library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
library(sand)
## Loading required package: igraphdata
## 
## Statistical Analysis of Network Data with R
## Type in C2 (+ENTER) to start with Chapter 2.
g.l=graph.lattice(c(5,5,5))
data(aidsblog)
summary(aidsblog)
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
## IGRAPH NA D--- 146 187 --
aidsblog=upgrade_graph(aidsblog)
igraph.options(vertex.size=3,vertex.label=NA,edge.arrow.size=0.5)
par(mfrow=c(1,2))
plot(g.l,layout=layout.circle)
title("5*5*5 Lattice")
plot(aidsblog,layout=layout.circle)
title("Blog Network")

par(mfrow=c(1,2))
plot(g.l,layout=layout.fruchterman.reingold)
title("5*5*5 Lattice")
plot(aidsblog,layout=layout.fruchterman.reingold)
title("Blog Network")

par(mfrow=c(1,2))
plot(g.l,layout=layout.kamada.kawai)
title("5*5*5 Lattice")
plot(aidsblog,layout=layout.kamada.kawai)
title("Blog Network")

g.tree=graph.formula(1-+2,1-+3,1-+4,2-+5,2-+6,2-+7,3-+8,3-+9,4-+10)
par(mfrow=c(1,3))
igraph.options(vertex.size=30,edge.arrow.size=0.5,vertex.label=NULL)
plot(g.tree,layout=layout.circle)
plot(g.tree,layout=layout.reingold.tilford(g.tree,circular=T))
plot(g.tree,layout=layout.reingold.tilford)

g.bip=upgrade_graph(g.bip)
plot(g.bip,layout=-layout.bipartite(g.bip)[,2:1],
     vertex.size=30, vertex.shape=ifelse(V(g.bip)$type, "rectangle","circle"),
     vertex.color=ifelse(V(g.bip)$type,"red","cyan"))

library(igraphdata)
data(karate)
#Reproducible Layout
set.seed(42)
l=layout.kamada.kawai(karate)
#Plot undecorated first.
par(mfrow=c(1,2))
plot(karate,layout=l,vertex.label=NA)
#Now decorate, starting with labels.
V(karate)$label=sub("Actor ","",V(karate)$name)
#Two leaders get shapes different from club members.
V(karate)$shape="circle"
V(karate)[c("Mr Hi","John A")]$shape="rectangle"
#Differentiate two factions by color.
V(karate)[Faction==1]$color="red"
V(karate)[Faction==2]$color="dodgerblue"
#vertex area proportional to vertex strength
#(i.e., total weight of incident edges).
V(karate)$size=4*sqrt(graph.strength(karate))
V(karate)$size2=V(karate)$size*.5
#Weight edges by number of common activities
E(karate)$width=E(karate)$weight
#Weight edges by within/between faction.
F1=V(karate)[Faction==1]
F2=V(karate)[Faction==2]
E(karate)[F1 %--% F1]$color="pink"
E(karate)[F2 %--% F2]$color="lightblue"
E(karate)[F1 %--% F2]$color="yellow"
#Offset vertex labels for smaller points (default=0)
V(karate)$label.dis=ifelse(V(karate)$size>=10,0,0.75)
#Plot decorated graph, using same layout.
plot(karate,layout=l)

library(sand)
data(lazega)
lazega=upgrade_graph(lazega)
#Office location indicated by color.
colbar=c("red","dodgerblue","goldenrod")
v.colors=colbar[V(lazega)$Office]

각 office별로 색상을 지정한 vertex를 만들고자 함

#Type of practice indicated by vertex shape.
v.shapes=c("circle","square")[V(lazega)$Practice]

Practice (1=litigation; 2=corporate)를 기준으로 각 vertex에 circle과 square를 적용함

#Vertex size proportional to years with firm.
v.size=3.5*sqrt(V(lazega)$Years)
set.seed(42)
l=layout.fruchterman.reingold(lazega)

year에 따라 각 vertex의 사이즈 조절

plot(lazega,layout=l,vertex.color=v.colors,vertex.shape=v.shapes,vertex.size=v.size)

여러 장식들을 이용하여 위와 같은 plot을 얻었다.

장식을 하지 않았을 때는 vertex들의 아무런 특징이 나타나지 않았지만 특징을 적용시킨 후에는 각 vertex가 의미하는 바를 시각적으로 쉽게 이해할 수 있다.

또한 size를 적용함으로서 더 많은 정보를 vertex에 담을 수 있어 각 특징뿐아니라 관계를 이해하는데에도 큰 도움이 된다.

위 플랏에 대한 책의 설명은 다음과 같다.

Visualization of Lazega`s network of collaborative working relationships among lawyers.

Vertices: partners and are labeled according to their seniority (with 1 being most senior)

Vertexcolors: three different office locations,while vertex shape corresponds to the type of practice

Vertex area: proportional to number of years with the law firm.

Edges: collaboration between partners.

library(sand)
summary(fblog)
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
## IGRAPH NA UN-- 192 1431 -- 
## + attr: name (v/c), PolParty (v/c)
party.names=sort(unique(V(fblog)$PolParty))
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
party.names
## [1] " Cap21"                   " Commentateurs Analystes"
## [3] " Les Verts"               " liberaux"               
## [5] " Parti Radical de Gauche" " PCF - LCR"              
## [7] " PS"                      " UDF"                    
## [9] " UMP"
set.seed(42)
l=layout.kamada.kawai(fblog)
party.nums.f=as.factor(V(fblog)$PolParty)
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
party.nums=as.numeric(party.nums.f)
plot(fblog,layout=l,vertex.label=NA,vertex.color=party.nums,vertex.size=3)

set.seed(42)
l=layout.drl(fblog)
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
plot(fblog,layout=l,vertex.size=5,vertex.label=NA,vertex.color=party.nums)

fblog.c=contract.vertices(fblog,party.nums)
E(fblog.c)$weight=1
fblog.c=simplify(fblog.c)
party.size=as.vector(table(V(fblog)$PolParty))
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
## This graph was created by an old(er) igraph version.
##   Call upgrade_graph() on it to use with the current igraph version
##   For now we convert it on the fly...
plot(fblog.c,vertex.size=5*sqrt(party.size),vertex.label=party.names,
     vertex.color=V(fblog.c),edge.width=sqrt(E(fblog.c)$weight),
     vertex.label.dist=1.5, edge.arrow.size=0)

k.nbhds<-graph.neighborhood(karate,order=1)
sapply(k.nbhds, vcount)
##  [1] 17 10 11  7  4  5  5  5  6  3  4  2  3  6  3  3  3  3  3  4  3  3  3  6  4
## [26]  4  3  5  4  5  5  7 13 18
k.1=k.nbhds[[1]]
k.34=k.nbhds[[34]]
par(mfrow=c(1,2))
plot(k.1,vertex.label=NA, vertex.color=c("red",rep("lightblue",16)))
plot(k.34,vertex.label=NA, vertex.color=c(rep("lightblue",17),"red"))