library(sand)
## Loading required package: igraph
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
## Loading required package: igraphdata
## 
## Statistical Analysis of Network Data with R
## Type in C2 (+ENTER) to start with Chapter 2.
library(igraph)
data(karate)
hist(degree(karate),col="lightblue",xlim=c(0,50),
     xlab="Vertex Degree", ylab="Frequency", main="")

hist(graph.strength(karate),col="pink",
     xlab="Vertex strength", ylab="Frequency",main="")

library(igraphdata)
data(yeast)
ecount(yeast)
## [1] 11855
vcount(yeast)
## [1] 2617
par(mfrow=c(1,2))
d.yeast=degree(yeast)
hist(d.yeast,col="blue",xlab="Degree",ylab="Frequency",main="Degree Distribution")
dd.yeast=degree.distribution(yeast)
d=1:max(d.yeast)-1
ind=(dd.yeast !=0)
plot(d[ind],dd.yeast[ind],log="xy",col="blue",xlab=c("Log-Degree"),
     ylab=c("Log-Intensity"),main="Log-Log Degree Distribution")

a.nn.deg.yeast=graph.knn(yeast,V(yeast))$knn
plot(d.yeast,a.nn.deg.yeast,log="xy",col="goldenrod",
     xlab=c("Log Vertex Degree"),ylab=c("Log Average Neighbor Degree"))

#install.packages("network", repos="http://cran.us.r-project.org")
#install.packages("sna", repos="http://cran.us.r-project.org")
A=get.adjacency(karate,sparse=FALSE)
library(network)
## network: Classes for Relational Data
## Version 1.16.0 created on 2019-11-30.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
##                     Mark S. Handcock, University of California -- Los Angeles
##                     David R. Hunter, Penn State University
##                     Martina Morris, University of Washington
##                     Skye Bender-deMoll, University of Washington
##  For citation information, type citation("network").
##  Type help("network-package") to get started.
## 
## Attaching package: 'network'
## The following objects are masked from 'package:igraph':
## 
##     %c%, %s%, add.edges, add.vertices, delete.edges, delete.vertices,
##     get.edge.attribute, get.edges, get.vertex.attribute, is.bipartite,
##     is.directed, list.edge.attributes, list.vertex.attributes,
##     set.edge.attribute, set.vertex.attribute
g=as.network.matrix(A)
library(sna)
## Loading required package: statnet.common
## 
## Attaching package: 'statnet.common'
## The following object is masked from 'package:base':
## 
##     order
## sna: Tools for Social Network Analysis
## Version 2.5 created on 2019-12-09.
## copyright (c) 2005, Carter T. Butts, University of California-Irvine
##  For citation information, type citation("sna").
##  Type help(package="sna") to get started.
## 
## Attaching package: 'sna'
## The following objects are masked from 'package:igraph':
## 
##     betweenness, bonpow, closeness, components, degree, dyad.census,
##     evcent, hierarchy, is.connected, neighborhood, triad.census
par(mfrow=c(1,2))
gplot.target(g,degree(g),main="Degree",circ.lab=FALSE,circ.col="skyblue",
             usearrows=FALSE,vertex.col=c("blue",rep("red",32),"yellow"),
             edge.col="darkgray")
## Warning in plot.window(...): "circ.lab"는 그래픽 매개변수가 아닙니다
## Warning in plot.xy(xy, type, ...): "circ.lab"는 그래픽 매개변수가 아닙니다
## Warning in title(...): "circ.lab"는 그래픽 매개변수가 아닙니다
gplot.target(g,closeness(g),main="Closeness",circ.lab=FALSE,circ.col="skyblue",
             usearrows=FALSE,vertex.col=c("blue",rep("red",32),"yellow"),
             edge.col="darkgray")
## Warning in plot.window(...): "circ.lab"는 그래픽 매개변수가 아닙니다
## Warning in plot.xy(xy, type, ...): "circ.lab"는 그래픽 매개변수가 아닙니다
## Warning in title(...): "circ.lab"는 그래픽 매개변수가 아닙니다

#install.packages("network",repos="http://cran.us.r-project.org")
#install.packages("sna",repos="http://cran.us.r-project.org")
A=get.adjacency(karate,sparse=FALSE)
library(network)
g=as.network.matrix(A)
library(sna)
par(mfrow=c(1,2))
gplot.target(g,betweenness(g), main="Betweenness", circ.lab=FALSE, circ.col="skyblue", usearrows=FALSE, vertex.col=c("blue",rep("red",32),"yellow"), edge.col="darkgrey")
## Warning in plot.window(...): "circ.lab"는 그래픽 매개변수가 아닙니다
## Warning in plot.xy(xy, type, ...): "circ.lab"는 그래픽 매개변수가 아닙니다
## Warning in title(...): "circ.lab"는 그래픽 매개변수가 아닙니다
gplot.target(g,evcent(g), main="Eigenvalue", circ.lab=FALSE, circ.col="skyblue", usearrows=FALSE, vertex.col=c("blue",rep("red",32),"yellow"), edge.col="darkgrey")
## Warning in plot.window(...): "circ.lab"는 그래픽 매개변수가 아닙니다
## Warning in plot.xy(xy, type, ...): "circ.lab"는 그래픽 매개변수가 아닙니다
## Warning in title(...): "circ.lab"는 그래픽 매개변수가 아닙니다

l=layout.kamada.kawai(aidsblog)
par(mfrow=c(1,2))
plot(aidsblog, layout=l, main="Hubs",vertex.label="",
     vertex.size=10 * sqrt(hub.score(aidsblog)$vector))
plot(aidsblog, layout=l, main="Authorities", vertex.label="",
     vertex.size=10 * sqrt(authority.score(aidsblog)$vector))

eb=edge.betweenness(karate)
E(karate)[order(eb,decreasing=T)[1:3]]
## + 3/78 edges from 4b458a1 (vertex names):
## [1] Actor 20--John A   Mr Hi   --Actor 20 Mr Hi   --Actor 32
table(sapply(cliques(karate),length))
## 
##  1  2  3  4  5 
## 34 78 45 11  2
cliques(karate)[sapply(cliques(karate), length)==5]
## [[1]]
## + 5/34 vertices, named, from 4b458a1:
## [1] Mr Hi    Actor 2  Actor 3  Actor 4  Actor 14
## 
## [[2]]
## + 5/34 vertices, named, from 4b458a1:
## [1] Mr Hi   Actor 2 Actor 3 Actor 4 Actor 8
table(sapply(maximal.cliques(karate),length))
## 
##  2  3  4  5 
## 11 21  2  2
clique.number(yeast)
## [1] 23
cores=graph.coreness(karate)
gplot.target(g, cores, circ.lab=FALSE, circ.col="skyblue",
             usearrows= FALSE, vertex.col=cores, edge.col="darkgray")
## Warning in plot.window(...): "circ.lab"는 그래픽 매개변수가 아닙니다
## Warning in plot.xy(xy, type, ...): "circ.lab"는 그래픽 매개변수가 아닙니다
## Warning in title(...): "circ.lab"는 그래픽 매개변수가 아닙니다

detach("package:sna")
detach("package:network")
aidsblog=simplify(aidsblog)
dyad.census(aidsblog)
## $mut
## [1] 3
## 
## $asym
## [1] 177
## 
## $null
## [1] 10405
ego.instr=induced.subgraph(karate,neighborhood(karate, 1, 1)[[1]])
ego.admin=induced.subgraph(karate,neighborhood(karate, 1, 34)[[1]])
graph.density(karate)
## [1] 0.1390374
graph.density(ego.instr)
## [1] 0.25
graph.density(ego.admin)
## [1] 0.2091503
transitivity(karate)
## [1] 0.2556818
transitivity(karate, "local", vids=c(1,34))
## [1] 0.1500000 0.1102941
reciprocity(aidsblog,mode="default")
## [1] 0.03278689
reciprocity(aidsblog,mode="ratio")
## [1] 0.01666667
is.connected(yeast)
## [1] FALSE
comps=decompose.graph(yeast)
table(sapply(comps, vcount))
## 
##    2    3    4    5    6    7 2375 
##   63   13    5    6    1    3    1
yeast.gc=decompose.graph(yeast)[[1]]
average.path.length(yeast.gc)
## [1] 5.09597
diameter(yeast.gc)
## [1] 15
transitivity(yeast.gc)
## [1] 0.4686663
vertex.connectivity(yeast.gc)
## [1] 1
edge.connectivity(yeast.gc)
## [1] 1
yeast.cut.vertices=articulation.points(yeast.gc)
length(yeast.cut.vertices)
## [1] 350
kc=fastgreedy.community(karate)
length(kc)
## [1] 3
sizes(kc)
## Community sizes
##  1  2  3 
## 18 11  5
membership(kc)
##    Mr Hi  Actor 2  Actor 3  Actor 4  Actor 5  Actor 6  Actor 7  Actor 8 
##        2        2        2        2        3        3        3        2 
##  Actor 9 Actor 10 Actor 11 Actor 12 Actor 13 Actor 14 Actor 15 Actor 16 
##        1        1        3        2        2        2        1        1 
## Actor 17 Actor 18 Actor 19 Actor 20 Actor 21 Actor 22 Actor 23 Actor 24 
##        3        2        1        2        1        2        1        1 
## Actor 25 Actor 26 Actor 27 Actor 28 Actor 29 Actor 30 Actor 31 Actor 32 
##        1        1        1        1        1        1        1        1 
## Actor 33   John A 
##        1        1
plot(kc,karate)

#install.packages("ape")
library(ape)
## 
## Attaching package: 'ape'
## The following objects are masked from 'package:igraph':
## 
##     edges, mst, ring
dendPlot(kc,mode="phylo")

k.lap=graph.laplacian(karate)
eig.anal=eigen(k.lap)
plot(eig.anal$values, col="blue", ylab="Eigenvalues of Graph Laplacian")

f.vec=eig.anal$vectors[,33]
faction=get.vertex.attribute(karate,"Faction")
f.colors=as.character(length(faction))
f.colors[faction==1]="red"
f.colors[faction==2]="cyan"
plot(f.vec, pch=16, xlab="Actor Number", ylab="Fideler Vector Entry", col=f.colors)
abline(0,0, lwd=2, col="lightgray")

func.class=get.vertex.attribute(yeast.gc,"Class")
table(func.class)
## func.class
##   A   B   C   D   E   F   G   M   O   P   R   T   U 
##  51  98 122 238  95 171  96 278 171 248  45 240 483
yc=fastgreedy.community(yeast.gc)
c.m=membership(yc)
table(c.m,func.class,useNA = c("no"))
##     func.class
## c.m    A   B   C   D   E   F   G   M   O   P   R   T   U
##   1    0   0   0   1   3   7   0   6   3 110   2  35  14
##   2    0   2   2   7   1   1   1   4  39   5   0   4  27
##   3    1   9   7  18   4   8   4  20  10  23   8  74  64
##   4   25  11  10  22  72  84  81 168  14  75  16  27 121
##   5    1   7   5  14   0   4   0   2   3   6   1  34  68
##   6    1  24   1   4   1   4   0   7   0   1   0  19  16
##   7    6  18   6  76   7   9   3   7   8   5   1   7  33
##   8    8  12  67  59   1  34   0  19  60  10   7   6  73
##   9    4   1   7   7   2  10   5   3   2   0   3   0  11
##   10   0   0   0   6   0   0   0   2   0   5   0  11   1
##   11   0   9   0  10   1   3   0   0   0   0   0   2   4
##   12   0   1   3   0   0   0   0   6  10   0   0   0   2
##   13   0   1   1   2   0   1   0   0   2   0   0  16  10
##   14   1   0   4   1   0   1   0   0   4   0   1   0  11
##   15   0   1   0   0   0   2   0   2   0   0   1   0   8
##   16   0   1   2   0   0   1   0   0  10   0   0   0   0
##   17   0   0   1   3   0   0   0   2   0   0   0   2   3
##   18   0   0   0   0   3   1   0   9   0   0   1   0   1
##   19   0   1   1   1   0   0   0   0   0   0   0   0   3
##   20   0   0   0   6   0   0   0   1   0   0   0   1   2
##   21   1   0   0   0   0   0   0   0   6   0   0   1   0
##   22   0   0   0   0   0   0   0   1   0   0   0   0   8
##   23   0   0   0   0   0   0   0   4   0   0   0   0   0
##   24   0   0   0   0   0   0   2   2   0   0   0   1   0
##   25   0   0   0   0   0   0   0   5   0   0   0   0   0
##   26   0   0   1   0   0   0   0   4   0   0   1   0   1
##   27   3   0   4   0   0   1   0   0   0   0   0   0   0
##   28   0   0   0   0   0   0   0   0   0   6   0   0   0
##   29   0   0   0   1   0   0   0   1   0   0   3   0   0
##   30   0   0   0   0   0   0   0   0   0   2   0   0   2
##   31   0   0   0   0   0   0   0   3   0   0   0   0   0
#assortativity.nominal(yeast, #(V(yeast)$Class=="P")+1,directed=FALSE)
assortativity.degree(yeast)
## [1] 0.4610798