Getting started with igraph library

Explore the graph

# load library igraph
library(igraph)

# create a ring graph with 10 nodes
g = graph.ring(10)

# visualize the graph
plot(g)
plot of chunk unnamed-chunk-1
# print the graph
g
## IGRAPH U--- 10 10 -- Ring graph
## + attr: name (g/c), mutual (g/l), circular (g/l)
# nodes and node count
V(g)
## Vertex sequence:
##  [1]  1  2  3  4  5  6  7  8  9 10
vcount(g)
## [1] 10
# edges and edge count
E(g)
## Edge sequence:
##              
## [1]   2 --  1
## [2]   3 --  2
## [3]   4 --  3
## [4]   5 --  4
## [5]   6 --  5
## [6]   7 --  6
## [7]   8 --  7
## [8]   9 --  8
## [9]  10 --  9
## [10] 10 --  1
ecount(g)
## [1] 10

Attributes

A graph can have graph attributes, node attributes and edge attributes:

# list graph attributes
list.graph.attributes(g)
## [1] "name"     "mutual"   "circular"
# show attributes and their values
graph.attributes(g)
## $name
## [1] "Ring graph"
## 
## $mutual
## [1] FALSE
## 
## $circular
## [1] TRUE
# assign an attribute label to nodes
# the attribute is used in the plot function
V(g)$label = letters[1:vcount(g)]
plot(g)
plot of chunk unnamed-chunk-2
# assign an attribute color to nodes
# the attribute is used in the plot function
V(g)$color = "red"
plot(g)
plot of chunk unnamed-chunk-2
# remove an attribute
list.vertex.attributes(g)
## [1] "label" "color"
g = remove.vertex.attribute(g, "color")
list.vertex.attributes(g)
## [1] "label"
plot(g)
plot of chunk unnamed-chunk-2
# remove all node attributes
list.vertex.attributes(g)
## [1] "label"
vertex.attributes(g) = list()
list.vertex.attributes(g)
## character(0)
# assign all attributes
lab = LETTERS[1:vcount(g)]
col = rep(rgb(200, 64, 32, alpha = 100, maxColorValue = 255), vcount(g))
vertex.attributes(g) = list(label = lab, color = col)
vertex.attributes(g)
## $label
##  [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
## 
## $color
##  [1] "#C8402064" "#C8402064" "#C8402064" "#C8402064" "#C8402064"
##  [6] "#C8402064" "#C8402064" "#C8402064" "#C8402064" "#C8402064"
plot(g)
plot of chunk unnamed-chunk-2
# assign all attributes from another graph
vertex.attributes(g) = vertex.attributes(graph.ring(10))
vertex.attributes(g)
## list()
# assign a label to edges
E(g)$label = month.abb[1:ecount(g)]
plot(g)
plot of chunk unnamed-chunk-2

Iterators

Iterators allow to access groups of nodes and edges in a smart way:

# access nodes with their indices
V(g)[1:5]$color = "white"
V(g)[6:vcount(g)]$color = "grey"
plot(g)
plot of chunk unnamed-chunk-3
# access nodes with a filter
V(g)$number = sample(1:100, vcount(g), replace=TRUE)
V(g)$color = "white"
V(g)[ number < 50 ]$color = "red"
plot(g)
plot of chunk unnamed-chunk-3
# access edges along a path
E(g, path = c(1,2,3,4))
## Edge sequence:
##           
## [1] 2 -- 1
## [2] 3 -- 2
## [3] 4 -- 3

Visualization parameters

Visualization parameters are properties of the visualization of the graph. They work for the whole graph, for nodes and for edges and can be set either as corresponding attributes or as arguments of the plot function:

# remove all node and edge attributes
vertex.attributes(g) = list()
edge.attributes(g) = list()

# save the layout of the graph in a variable
coords = layout.fruchterman.reingold(g)
coords
##            [,1]       [,2]
##  [1,] 18.953585 -11.825474
##  [2,] 15.702967 -13.156875
##  [3,] 12.286887 -12.321755
##  [4,] 10.017839  -9.634660
##  [5,]  9.753675  -6.127501
##  [6,] 11.598423  -3.135268
##  [7,] 14.852391  -1.803926
##  [8,] 18.267861  -2.644340
##  [9,] 20.538756  -5.329096
## [10,] 20.802090  -8.834814
# visualization parameters for graph, nodes and edges
plot(g,
     layout=coords,
     vertex.size = 20, vertex.color = "white", vertex.shape = "square", vertex.label = letters[1:vcount(g)],
     edge.width = 2,   edge.color = "black",   edge.lty = 3,            edge.label = LETTERS[1:ecount(g)], edge.curved = T   )
plot of chunk unnamed-chunk-4

Write and read the graph in the file system

Different formats can be used to store and read the graph form the file system. In the following we use the GraphML XML format:

# save the graph in XML format in the file system
V(g)$name = letters[1:vcount(g)]
E(g)$weight = runif(ecount(g))
write.graph(g, file="ring.xml", format="graphml")

This is the saved XML graph.

# read the graph in in XML format from the file system
g = read.graph(file="ring.xml", format="graphml")