# load library igraph
# read http://igraph.org/r/doc/aaa-igraph-package.html
library(igraph)
Read a graph
# read graph from data frame
actors <- data.frame(
name = c("Alice", "Bob", "Cecil", "David", "Esmeralda"),
age = c(48, 33, 45, 34, 21),
gender = c("F", "M", "F", "M", "F")
)
relations <- data.frame(
from = c("Bob", "Cecil", "Cecil", "David", "David", "Esmeralda"),
to = c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
same.dept = c(FALSE, FALSE, TRUE, FALSE, FALSE, TRUE),
friendship = c(4, 5, 5, 2, 1, 1),
advice = c(4, 5, 5, 4, 2, 3)
)
g = graph_from_data_frame(relations, directed = TRUE, vertices = actors)
Explore a graph
# print graph
g
## IGRAPH 1c80ad7 DN-- 5 6 --
## + attr: name (v/c), age (v/n), gender (v/c), same.dept (e/l),
## | friendship (e/n), advice (e/n)
## + edges from 1c80ad7 (vertex names):
## [1] Bob ->Alice Cecil ->Bob Cecil ->Alice David ->Alice
## [5] David ->Bob Esmeralda->Alice
# nodes and node count
V(g)
## + 5/5 vertices, named, from 1c80ad7:
## [1] Alice Bob Cecil David Esmeralda
vcount(g)
## [1] 5
# edges and edge count
E(g)
## + 6/6 edges from 1c80ad7 (vertex names):
## [1] Bob ->Alice Cecil ->Bob Cecil ->Alice David ->Alice
## [5] David ->Bob Esmeralda->Alice
ecount(g)
## [1] 6
## Attributes
# A graph can have attributes for the entire graph, for nodes and for edges
# add graph attribute
g$name = "Alice & friends"
g
## IGRAPH 1c80ad7 DN-- 5 6 -- Alice & friends
## + attr: name (g/c), name (v/c), age (v/n), gender (v/c), same.dept
## | (e/l), friendship (e/n), advice (e/n)
## + edges from 1c80ad7 (vertex names):
## [1] Bob ->Alice Cecil ->Bob Cecil ->Alice David ->Alice
## [5] David ->Bob Esmeralda->Alice
# add node attribute
V(g)$height = c(122, 155, 178, 167, 198)
g
## IGRAPH 1c80ad7 DN-- 5 6 -- Alice & friends
## + attr: name (g/c), name (v/c), age (v/n), gender (v/c), height
## | (v/n), same.dept (e/l), friendship (e/n), advice (e/n)
## + edges from 1c80ad7 (vertex names):
## [1] Bob ->Alice Cecil ->Bob Cecil ->Alice David ->Alice
## [5] David ->Bob Esmeralda->Alice
# add edge attribute
E(g)$weight = c(1, 2, 2, 5, 5, 4)
g
## IGRAPH 1c80ad7 DNW- 5 6 -- Alice & friends
## + attr: name (g/c), name (v/c), age (v/n), gender (v/c), height
## | (v/n), same.dept (e/l), friendship (e/n), advice (e/n), weight
## | (e/n)
## + edges from 1c80ad7 (vertex names):
## [1] Bob ->Alice Cecil ->Bob Cecil ->Alice David ->Alice
## [5] David ->Bob Esmeralda->Alice
# delete attribute
g <- delete_graph_attr(g, "name")
g <- delete_vertex_attr(g, "height")
g <- delete_edge_attr(g, "weight")
# add/remove nodes/edges
g = add_vertices(g, 1, attr = list(name = "Rabbit", age = 12, gender = "M"))
V(g)
## + 6/6 vertices, named, from 865d0ce:
## [1] Alice Bob Cecil David Esmeralda Rabbit
g = add_edges(g, c("Rabbit", "Alice"), attr = list(same.dept = FALSE, friendship = 5, advice = 5))
E(g)
## + 7/7 edges from 059d7e3 (vertex names):
## [1] Bob ->Alice Cecil ->Bob Cecil ->Alice David ->Alice
## [5] David ->Bob Esmeralda->Alice Rabbit ->Alice
g = delete_edges(g, "Rabbit|Alice")
E(g)
## + 6/6 edges from 08b58cc (vertex names):
## [1] Bob ->Alice Cecil ->Bob Cecil ->Alice David ->Alice
## [5] David ->Bob Esmeralda->Alice
g = delete_vertices(g, "Rabbit")
V(g)
## + 5/5 vertices, named, from 2b103f2:
## [1] Alice Bob Cecil David Esmeralda
## Iterators
# Iterators allow to access groups of nodes and edges in a smart way
# access nodes with their indices
V(g)[1]
## + 1/5 vertex, named, from 2b103f2:
## [1] Alice
V(g)["Alice"]
## + 1/5 vertex, named, from 2b103f2:
## [1] Alice
V(g)[1:3]
## + 3/5 vertices, named, from 2b103f2:
## [1] Alice Bob Cecil
V(g)[age <= 30]
## + 1/5 vertex, named, from 2b103f2:
## [1] Esmeralda
V(g)[gender == "M"]
## + 2/5 vertices, named, from 2b103f2:
## [1] Bob David
E(g)[1]
## + 1/6 edge from 2b103f2 (vertex names):
## [1] Bob->Alice
E(g)["Bob|Alice"]
## + 1/6 edge from 2b103f2 (vertex names):
## [1] Bob->Alice
E(g)[1:3]
## + 3/6 edges from 2b103f2 (vertex names):
## [1] Bob ->Alice Cecil->Bob Cecil->Alice
E(g)[same.dept == TRUE]
## + 2/6 edges from 2b103f2 (vertex names):
## [1] Cecil ->Alice Esmeralda->Alice
Visualize a graph
# read http://igraph.org/r/doc/plot.common.html
# plot graph
plot(g)
plot(g, vertex.shape = "none")
# save the layout of the graph in a variable
coords = layout_nicely(g)
coords
## [,1] [,2]
## [1,] 18.82791 15.14335
## [2,] 18.53810 14.07293
## [3,] 17.68893 14.67471
## [4,] 19.58194 14.16517
## [5,] 19.17344 16.48374
# plot graph with a layout
plot(g, layout = coords, vertex.shape = "none")
# 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 = TRUE)
Convert a graph to other representations
# write the graph to data frame
as_data_frame(g, what="vertices")
## name age gender
## Alice Alice 48 F
## Bob Bob 33 M
## Cecil Cecil 45 F
## David David 34 M
## Esmeralda Esmeralda 21 F
as_data_frame(g, what="edges")
## from to same.dept friendship advice
## 1 Bob Alice FALSE 4 4
## 2 Cecil Bob FALSE 5 5
## 3 Cecil Alice TRUE 5 5
## 4 David Alice FALSE 2 4
## 5 David Bob FALSE 1 2
## 6 Esmeralda Alice TRUE 1 3
as_data_frame(g, what="both")
## $vertices
## name age gender
## Alice Alice 48 F
## Bob Bob 33 M
## Cecil Cecil 45 F
## David David 34 M
## Esmeralda Esmeralda 21 F
##
## $edges
## from to same.dept friendship advice
## 1 Bob Alice FALSE 4 4
## 2 Cecil Bob FALSE 5 5
## 3 Cecil Alice TRUE 5 5
## 4 David Alice FALSE 2 4
## 5 David Bob FALSE 1 2
## 6 Esmeralda Alice TRUE 1 3
# write graph to sparse adjacency matrix
as_adjacency_matrix(g)
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Alice Bob Cecil David Esmeralda
## Alice . . . . .
## Bob 1 . . . .
## Cecil 1 1 . . .
## David 1 1 . . .
## Esmeralda 1 . . . .
# non-sparse matrix
as_adjacency_matrix(g, sparse = FALSE)
## Alice Bob Cecil David Esmeralda
## Alice 0 0 0 0 0
## Bob 1 0 0 0 0
## Cecil 1 1 0 0 0
## David 1 1 0 0 0
## Esmeralda 1 0 0 0 0
# weighted matrix
as_adjacency_matrix(g, attr = "friendship")
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Alice Bob Cecil David Esmeralda
## Alice . . . . .
## Bob 4 . . . .
## Cecil 5 5 . . .
## David 2 1 . . .
## Esmeralda 1 . . . .
# read graph from adjacency matrix
A = as_adjacency_matrix(g)
graph_from_adjacency_matrix(A)
## IGRAPH e7e69cf DN-- 5 6 --
## + attr: name (v/c)
## + edges from e7e69cf (vertex names):
## [1] Bob ->Alice Cecil ->Alice David ->Alice Esmeralda->Alice
## [5] Cecil ->Bob David ->Bob
# read as undirected graph
graph_from_adjacency_matrix(A, mode = "undirected")
## IGRAPH 83778c6 UN-- 5 6 --
## + attr: name (v/c)
## + edges from 83778c6 (vertex names):
## [1] Alice--Bob Alice--Cecil Alice--David Alice--Esmeralda
## [5] Bob --Cecil Bob --David
A = as_adjacency_matrix(g, attr = "friendship")
# multi-graph
graph_from_adjacency_matrix(A)
## IGRAPH e408a8d DN-- 5 18 --
## + attr: name (v/c)
## + edges from e408a8d (vertex names):
## [1] Bob ->Alice Bob ->Alice Bob ->Alice Bob ->Alice
## [5] Cecil ->Alice Cecil ->Alice Cecil ->Alice Cecil ->Alice
## [9] Cecil ->Alice David ->Alice David ->Alice Esmeralda->Alice
## [13] Cecil ->Bob Cecil ->Bob Cecil ->Bob Cecil ->Bob
## [17] Cecil ->Bob David ->Bob
# weighted graph on weight attribute
graph_from_adjacency_matrix(A, weighted = TRUE)
## IGRAPH f37d22a DNW- 5 6 --
## + attr: name (v/c), weight (e/n)
## + edges from f37d22a (vertex names):
## [1] Bob ->Alice Cecil ->Alice David ->Alice Esmeralda->Alice
## [5] Cecil ->Bob David ->Bob
# weighted graph on friendship attribute
graph_from_adjacency_matrix(A, weighted = "friendship")
## IGRAPH e62aabe DN-- 5 6 --
## + attr: name (v/c), friendship (e/n)
## + edges from e62aabe (vertex names):
## [1] Bob ->Alice Cecil ->Alice David ->Alice Esmeralda->Alice
## [5] Cecil ->Bob David ->Bob
# write to graphml format
write_graph(g, file="Alice.xml", format="graphml")
# read from graphml format (notice the new id vertex attribute)
read_graph(file="Alice.xml", format="graphml")
## IGRAPH acb6a2f DN-- 5 6 --
## + attr: name (v/c), age (v/n), gender (v/c), id (v/c), same.dept
## | (e/l), friendship (e/n), advice (e/n)
## + edges from acb6a2f (vertex names):
## [1] Bob ->Alice Cecil ->Bob Cecil ->Alice David ->Alice
## [5] David ->Bob Esmeralda->Alice