This is a basic network tutorial. After this exercise, you will learn: 1) construct a network through edgelist or matrix (directed network v.s.undirected network; weighted network or unweighted network. 2) extract the edgeglist and matrix from the graph. 3) calculate the basic network characteristics, such as degree ( degree-in and degree-out),and diameter.
For more details of using R igraph see https://igraph.org/r/.

# set up the libraries we need.
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(lattice)
library(Matrix)

1.Construct network

You can also use other differnet formats of data to construct network.

g1 <- graph( c( 1,2, 2,3, 2,4, 3,4, 3,5, 1,5, 3,6 ), directed=FALSE  )
plot(g1)

2.Extract edgelist, matrix from graph

#get proper edge list
get.edgelist(g1)
##      [,1] [,2]
## [1,]    1    2
## [2,]    2    3
## [3,]    2    4
## [4,]    3    4
## [5,]    3    5
## [6,]    1    5
## [7,]    3    6
#get the vertex list in a specifc order
get.vertex.attribute(g1) #You can also extract the nodes of network.
## list()
#get adjacency matrix
adj_A1=get.adjacency(g1)
adj_A1
## 6 x 6 sparse Matrix of class "dgCMatrix"
##                 
## [1,] . 1 . . 1 .
## [2,] 1 . 1 1 . .
## [3,] . 1 . 1 1 1
## [4,] . 1 1 . . .
## [5,] 1 . 1 . . .
## [6,] . . 1 . . .

Here we can also see how a network can be represented in different visulisations styles

layouts <- grep("^layout_", ls("package:igraph"), value=TRUE)[-1] 
#grep, grepl, regexpr, gregexpr and regexec search for matches to argument pattern within each element of a character vector: they differ in the format of and amount of detail in the results.
# you can use View(layouts) to check how many styles igraph has been embeded here.
# ls and objects return a vector of character strings giving the names of the objects in the specified environment. 
# think about why we add [-1] in the end of this line.
layouts <- layouts[!grepl("bipartite|sugiyama", layouts)]
#grepl returns TRUE if a string contains the pattern, otherwise FALSE; if the parameter is a string vector, returns a logical vector (match or not for each element of the vector;
# ! indicates logical negation (NOT). see other logical operators here: https://stat.ethz.ch/R-manual/R-devel/library/base/html/Logic.html
par(mfrow=c(5,4), mai=c(.1,.1,.1,.1)) #set graphic parameters_5 rows and 3 columns
for (layout in layouts) {
  print(layout)
  l <- do.call(layout, list(g1)) 
  plot(g1, edge.arrow.mode=0, layout=l, vertex.size=20, vertex.colour="orange") }
## [1] "layout_as_star"
## [1] "layout_as_tree"
## [1] "layout_components"
## [1] "layout_in_circle"
## [1] "layout_nicely"
## [1] "layout_on_grid"
## [1] "layout_on_sphere"
## [1] "layout_randomly"
## [1] "layout_with_dh"
## [1] "layout_with_drl"
## [1] "layout_with_fr"
## [1] "layout_with_gem"
## [1] "layout_with_graphopt"
## [1] "layout_with_kk"
## [1] "layout_with_lgl"
## [1] "layout_with_mds"

3.Calculate the basic network characteristics, such as degree.

degree(g1, v= V(g1))
## [1] 2 3 4 2 2 1
degree
## function (graph, v = V(graph), mode = c("all", "out", "in", "total"), 
##     loops = TRUE, normalized = FALSE) 
## {
##     if (!is_igraph(graph)) {
##         stop("Not a graph object")
##     }
##     v <- as.igraph.vs(graph, v)
##     mode <- igraph.match.arg(mode)
##     mode <- switch(mode, out = 1, `in` = 2, all = 3, total = 3)
##     on.exit(.Call(C_R_igraph_finalizer))
##     res <- .Call(C_R_igraph_degree, graph, v - 1, as.numeric(mode), 
##         as.logical(loops))
##     if (normalized) {
##         res <- res/(vcount(graph) - 1)
##     }
##     if (igraph_opt("add.vertex.names") && is_named(graph)) {
##         names(res) <- V(graph)$name[v]
##     }
##     res
## }
## <bytecode: 0x7f9161498c10>
## <environment: namespace:igraph>
diameter (g1) 
## [1] 3

think about: what does degree mean here, and what does the diameter mean here? I would suggest you do it manually and compare it with what the computer calculate here.

what if we want to use the size of the node to indicate their degree?

plot(g1, vertex.size=10*degree(g1,V(g1)), vertex.colour="organge")

Wrap up today’s exercise

Today, we learned how the igraph works, from constructing, extracting to calulating. Now we would like to review the basic code we use today.

  1. g<- graph.data.frame ( )

    g<- graph.edgelist( )

    g<- graph.matrix( )

  2. edgelist <- get.edgelist ( )

    matrix<- get.adjancency( )

  3. degree <- degree ( )

    diameter ( )