[Author]

@ JungHwan Yun
@ Master Student in Data-Science
@ Seoul National University of Science & Technology(SeoulTech)
@ E-mail : junghwan.yun@seoultech.ac.kr



[Contents]

@ Topic : Social Network Analysis with R
@ Class : Social Network Analysis
@ Version : 2.0
@ Version date : 2017-05-10
@ Summary : Package “igraph”의 활용법을 다룹니다.


1. 패키지 로딩 및 작업환경 설정

# install.packages("igraph")
#setwd("D:/Google Drive/1_MASTER_Class/3_1_Social_Network_Analysis/1_Lecture_Material/C6_R_SNA/Train_Data")
library(igraph)


2. igraph package의 기본적 사용

edges <-c(1,2, 3,2, 2,4)
(g<-graph(edges, n=max(edges), directed=TRUE))
## IGRAPH D--- 4 3 -- 
## + edges:
## [1] 1->2 3->2 2->4
vcount(g)
## [1] 4
ecount(g)
## [1] 3
is.directed(g)
## [1] TRUE
get.edgelist(g)
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    2
## [3,]    2    4
plot(g, layout = layout.fruchterman.reingold, vertex.label=V(g)$number,edge.arrow.size=0.5)

3. Example1 : Knoke-information network

STEP1. Edge list형식으로 파일 읽어들이기
  • read.graph()를 사용하여 edge파일 불러오기
(knoke.infor.1 <-read.graph("Train_Data/knoke-infor-edgelist-1.txt",format="edgelist",n=10,directed=F))
## IGRAPH U--- 10 17 -- 
## + edges:
##  [1] 1-- 2 1-- 5 2-- 3 2-- 4 2-- 5 2-- 7 2-- 8 2-- 9 3-- 5 3-- 6 3--10
## [12] 4-- 5 4-- 7 5-- 7 5-- 8 5-- 9 5--10
  • V()$name : vertex들의 이름을 부여
V(knoke.infor.1)$name <-c('COUN','COMM','EDUC','INDU','MAYR','WRO','NEWS','UWAY','WELF','WEST')
plot(knoke.infor.1, layout=layout.fruchterman.reingold, vertex.label=V(knoke.infor.1)$name,vertex.shape="rectangle", vertex.shape="none", vertex.label.font=2, edge.arrow.size=0.5)

# Knokemoney network
knoke.money<-read.graph("Train_Data//knoke-money-edgelist.txt",format="edgelist")
V(knoke.money)$name <-c("COUN","COMM","EDUC","INDU","MAYR","WRO","NEWS","UWAY","WELF","WEST")
plot(knoke.money, layout=layout.fruchterman.reingold, vertex.label=V(knoke.money)$name, vertex.label.font=2, edge.arrow.size=0.5)

STEP2. 네트워크 그리기
  • sna패키지의 gplot()함수와 유사함. (igraph-plot() Document)[“http://igraph.org/r/doc/plot.common.html”]
    • edge.arrow.size : 엣지 화살표 크기
    • edge.color : 엣지 색깔
    • edge.width : 엣지 두께
    • vertex.color : 노드의 색상
    • vertex.size : 노드의 크기
    • layout : 레이아웃 형태
plot(g, edge.arrow.size= 0.5, edge.color= "gray", vertex.color= "Skyblue", vertex.size= 30, edge.width= 2, layout = layout.kamada.kawai)

STEP3. 네트워크 지표산출하기
edge.list<-c(0,3, 1,3, 2,3, 3,4, 4,5, 5,6, 6,7, 7,8) + 1
L <-graph(n=9,edge.list,directed=F)
plot(L, layout=layout.fruchterman.reingold, edge.arrow.mode="-", edge.width=2, vertex.label.cex=1.2)

  • 산출지표
    • degree()
    • betweenness()
    • closeness()
    • evcent() : 위세중앙성
    • centralization.degee() : degree기반 중심화
degree(L, mode="in")
## [1] 1 1 1 4 2 2 2 2 1
betweenness(L)
## [1]  0  0  0 18 16 15 12  7  0
round(closeness(L, mode="out"),2)
## [1] 0.04 0.04 0.04 0.06 0.06 0.06 0.05 0.04 0.03
evcent(L)$vector
## [1] 0.4723926 0.4723926 0.4723926 1.0000000 0.6997053 0.4811943 0.3189268
## [8] 0.1939366 0.0916142
centralization.degree(L)
## $res
## [1] 1 1 1 4 2 2 2 2 1
## 
## $centralization
## [1] 0.2777778
## 
## $theoretical_max
## [1] 72

4. 네트워크 시뮬레이션

n <-50
p <-0.1
nei<-1
g_01 <-watts.strogatz.game(1, n, nei,p)
plot(g_01, layout=layout.circle, vertex.shape= "none", edge.arrow.size= 0.5)

(deg<-table(degree(g_01)))
## 
##  1  2  3 
##  8 34  8
n <-50
p <-0.2
nei<-1
# small world network
g_01 <-watts.strogatz.game(1, n, nei,p)
plot(g_01, layout=layout.circle, vertex.shape= "none", edge.arrow.size= 0.5)

(deg<-table(degree(g_01)))
## 
##  1  2  3  5 
## 15 22 12  1
barabasi.1<-barabasi.game(n=100,power=1)
plot(barabasi.1,layout=layout.fruchterman.reingold, vertex.shape="none",vertex.label.cex=.5,edge.arrow.size=.1) 

centralization.degree(barabasi.1)
## $res
##   [1] 35  2  6  1  3  5  2  8  2  2  2  2  4  3  1  2  1  3  2  2  2  2  2
##  [24]  1  1  3  3  2  2  1  2  1  2  2  1  2  1  1  1  2  1  5  3  4  4  1
##  [47]  1  1  1  1  1  1  1  2  1  2  1  1  1  2  1  1  2  1  1  1  1  1  1
##  [70]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
##  [93]  1  1  1  1  1  1  1  1
## 
## $centralization
## [1] 0.1684522
## 
## $theoretical_max
## [1] 19602
mean_distance(barabasi.1)
## [1] 1.759804
barabasi.2<-barabasi.game(n=100,power=2)
plot(barabasi.2,layout=layout.fruchterman.reingold, vertex.shape="none",vertex.label.cex=.5,edge.arrow.size=.1) 

centralization.degree(barabasi.2)
## $res
##   [1] 93  1  3  1  1  1  1  1  2  1  1  1  1  1  1  2  1  1  1  2  1  1  1
##  [24]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  2
##  [47]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
##  [70]  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
##  [93]  1  1  1  1  1  1  1  1
## 
## $centralization
## [1] 0.4643404
## 
## $theoretical_max
## [1] 19602
mean_distance(barabasi.2)
## [1] 1.057143

5. Example2

# Generate adjacency matrix
adj_dat=read.csv("Train_Data//sample_adjmatrix.csv", header=TRUE,
row.names= 1,check.names=FALSE)
m1=as.matrix(adj_dat) # Transform data frame to matrix
m1
##       23732 23778 23824 23871 58009 58098 58256
## 23732     0     1     0     1     0     1     0
## 23778     1     0     1     1     0     1     0
## 23824     0     1     0     0     0     0     0
## 23871     1     1     0     0     1     1     0
## 58009     0     0     0     1     0     1     0
## 58098     1     1     0     1     1     0     1
## 58256     0     0     0     0     0     1     0
# Generate igraphdata using adjacency matrix
g1=graph.adjacency(m1,mode="undirected",weighted=NULL,diag=FALSE)
plot.igraph(g1) # Plot adjacency matrix

edge_dat=read.csv("Train_Data/sample_edgelist.csv",header=TRUE)
e1 <-as.character(edge_dat[,1]) # Extract Word Column 1
e2 <-as.character(edge_dat[,2]) # Extract Word Column 2
m2=as.matrix(data.frame(e1,e2))
# Generate igraphdata using edgelistdata matrix
g2=graph.edgelist(m2, directed = FALSE)
plot.igraph(g2)

V(g1)$name # Show vertex labels
## [1] "23732" "23778" "23824" "23871" "58009" "58098" "58256"
attr_dat<-read.csv("Train_Data//sample_attributes.csv", head=TRUE)
# Assign the "Sex" attribute
V(g1)$Sex <-as.character(attr_dat$Sex[match(V(g1)$name,attr_dat$Bird.ID)])
V(g1)$Sex
## [1] "F" "M" "M" "F" "F" "M" "M"
# assign the "Sex" attribute as the vertex color
V(g1)$color=V(g1)$Sex
V(g1)$color=gsub("F","red",V(g1)$color) # Females will be red
V(g1)$color=gsub("M","blue",V(g1)$color) # Males will be blue
plot(g1)