Sunday, 14 January 2018

Decorating Network Graphs in igraph

Cited from the Book "Statistical Analysis of Network Data With R"

Vertex, Edge and Graph Attributes

Equipping a graph with attributes is referred to as decorating the graph. Typically, the vertices or edges of a graph (or both) are decorated with attributes, although the graph as a whole may be decorated as well. In igraph, the elements of graph objects (i.e., particularly the vertex and edge sequences, and subsets thereof) may be equipped with attributes simply by using the ‘$’ operator.

For example,

V(dg)$name 

V(dg)$gender <- c("M","F","M")

Edge attributes similarly are values of variables indexed by adjacent vertex pairs. A graph for which the edges are equipped with weights is referred to as a weighted graph. 

More generally, a weighted graph can be defined as a pair (V,E), where V is a set of vertices, as before, but the elements in E are now non-negative numbers, with one such number for each vertex pair. Analogously, the adjacency matrix A for a weighted graph is defined such that the entry A_{ij} is equal to the corresponding weight for the vertex pair i and j.

For example,

is.weighted(g)
wg <- g
E(wg)$weight <- runif(ecount(wg)) 
is.weighted(wg)

In principle, a graph itself may be decorated with an attribute, and indeed, it is possible to equip graph objects with attributes in igraph. The most natural use of this feature arguably is to equip a graph with relevant background information, such as a name.

For example,

g$name <- "Toy Graph

Using Data Frames

In R, a network graph and all vertex and edge attributes can be conveniently represented using two data frames, one with vertex information, and the other, with edge information. Under this approach, the first column of the vertex data frame contains the vertex names (i.e., either the default numerical labels or symbolic), while each of the other columns contain the values of a given vertex attribute. Similarly, the first two columns of the edge data frame contain an edge list defining the graph, while each of the other columns contain the values of a given edge attribute. 

For example,

list.vertex.attributes(g)

(in addition to the vertex name)

No comments:

Post a Comment