library(sna)
library(igraph)
setwd("D:/R_Labs")
data<- "sample_adjmatrix.csv"
el<-read.table(data, header=T, row.names=1, sep=",")
m=as.matrix(el)
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)
#igraph object
g1=graph.adjacency(m,mode="undirected",weighted=NULL)
class(g1); plot(g1)
## [1] "igraph"
setwd("D:/R_Labs")
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)
setwd("D:/R_Labs")
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')
# igraph --> 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
# 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"
# weighted graph
is.weighted(g3)
## [1] FALSE
E(g3)$weight<-runif(ecount(g3) )
is.weighted(g3)
## [1] TRUE
E(g3)$weight
## [1] 0.77965655 0.06086829 0.99097351 0.05464308 0.40371985 0.08755811
## [7] 0.28608251 0.75620541 0.72238959 0.79841096
plot(g3, vertex.color="red", edge.color="black", edge.width=E(g3)$weight*10, vertex.size=10, main='My Weighted Graph')
# 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