[.設定所需的函式庫(libraries)以及載入資料]

#install.packages("sna")
#install.packages("igraph")

library(sna)
library(igraph)

setwd("D:/Rdata Practice/SNA/Lab 1")

PART 1.Import from csv file: Matrix format

1-1.Using sna package

data<- "sample_adjmatrix.csv"
el<-read.table(data, header=T, row.names=1, sep=",") #讀進來是dataframe型態
m=as.matrix(el) #要轉成Matrix型態
m
##       X23732 X23778 X23824 X23871 X58009 X58098 X58256
## 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
gplot(m, displaylabels=TRUE)

1-2.using igraph package

g1=graph.adjacency(m,mode="undirected",weighted=NULL)
class(g1); plot(g1)
## [1] "igraph"

PART 2.Import from csv file: edge-list format

2-1.Using sna package

data2<- "n2.csv"
(el2<-read.table(data2, header=T, sep=","))
##    V1 V2 V3
## 1   1  2  1
## 2   1  3  1
## 3   1  5  1
## 4   2  6  1
## 5   2  7  1
## 6   2  5  1
## 7   3  4  1
## 8   3  5  1
## 9   4  5  1
## 10  5  7  1
(m2=as.matrix(el2))
##       V1 V2 V3
##  [1,]  1  2  1
##  [2,]  1  3  1
##  [3,]  1  5  1
##  [4,]  2  6  1
##  [5,]  2  7  1
##  [6,]  2  5  1
##  [7,]  3  4  1
##  [8,]  3  5  1
##  [9,]  4  5  1
## [10,]  5  7  1
attr(m2,"n")<-7 # number of nodes
attr(m2,"vnames")<-letters[1:7] # names of each node
gplot(m2,displaylabels=TRUE)

2-2.Using igraph package

data3<- "sample_edgelist.csv"
(el3<-read.table(data3, header=T, sep=","))
##    Source Target
## 1   23732  23778
## 2   23732  23871
## 3   23732  58098
## 4   23778  23824
## 5   23778  23871
## 6   23778  58098
## 7   23871  58009
## 8   23871  58098
## 9   58009  58098
## 10  58098  58256
# read from data.frame  (--> list)
g3=graph.data.frame(el3,directed=FALSE) 
class(g3); plot(g3)
## [1] "igraph"

plot(g3, vertex.color="red",  edge.color="black", edge.arrow.size=.3, 
     vertex.size=10, main='My Graph')

PART 3.Manipulating the graph

3-1.把edgelist轉成matrix

(m3<-get.adjacency(g3))
## 7 x 7 sparse Matrix of class "dgCMatrix"
##       23732 23778 23871 58009 58098 23824 58256
## 23732     .     1     1     .     1     .     .
## 23778     1     .     1     .     1     1     .
## 23871     1     1     .     1     1     .     .
## 58009     .     .     1     .     1     .     .
## 58098     1     1     1     1     .     .     1
## 23824     .     1     .     .     .     .     .
## 58256     .     .     .     .     1     .     .
class(m3) # dgMatrix
## [1] "dgCMatrix"
## attr(,"package")
## [1] "Matrix"
(m3<-as.matrix(m3)) # convert to regular matrix
##       23732 23778 23871 58009 58098 23824 58256
## 23732     0     1     1     0     1     0     0
## 23778     1     0     1     0     1     1     0
## 23871     1     1     0     1     1     0     0
## 58009     0     0     1     0     1     0     0
## 58098     1     1     1     1     0     0     1
## 23824     0     1     0     0     0     0     0
## 58256     0     0     0     0     1     0     0
class(m3)
## [1] "matrix"
V(g3) # vertex
## + 7/7 vertices, named:
## [1] 23732 23778 23871 58009 58098 23824 58256
E(g3) # edge
## + 10/10 edges (vertex names):
##  [1] 23732--23778 23732--23871 23732--58098 23778--23824 23778--23871
##  [6] 23778--58098 23871--58009 23871--58098 58009--58098 58098--58256
vcount(g3); ecount(g3) # counts of vertex and edges
## [1] 7
## [1] 10

3-2.graph attribute

V(g3)$name
## [1] "23732" "23778" "23871" "58009" "58098" "23824" "58256"
V(g3)$name <- c("Tom", "Mary","John","Amy","Eric","Sam","Jack")
V(g3)$name
## [1] "Tom"  "Mary" "John" "Amy"  "Eric" "Sam"  "Jack"

3-3.weighted graph

is.weighted(g3)
## [1] FALSE
E(g3)$weight<-runif(ecount(g3) )
is.weighted(g3)
## [1] TRUE
E(g3)$weight
##  [1] 0.3660790 0.4388650 0.2577157 0.9700835 0.5595097 0.6502159 0.6997582
##  [8] 0.7446162 0.3340291 0.6236772
plot(g3, vertex.color="red",  edge.color="black", edge.width=E(g3)$weight*10, vertex.size=10, main='My Weighted Graph')

3-4.Types of graphs

g.full<- graph.full(7)
g.ring<- graph.ring(7)
g.tree<-graph.tree(7, children = 3, mode= "undirected")
g.star<-graph.star(7, mode= "undirected" )
par(mfrow=c(2,2))
plot(g.full, main="full");plot(g.ring, main="ring");plot(g.tree, main="tree");plot(g.star, main="star")

dev.off()
## null device 
##           1