Let’s do some graph theory! (part 1)

1 Balls Header


** For those not familiar with graph theory, please note that term “graph” does not refer to bar graphs, line graphs, etc. A graph is a specific mathematical object of nodes/vertices and edge. You can visualize a graph a lot like a network.**

KEY QUESTION: Given a two-column list representing pairs of vertices of an edge, or given an adjacency matrix, how can I visualize them as a (network) graph?



ESTIMATED SCRIPT TIME: less than 4 hours

CATEGORY OF ANALYSIS: Graph theory, social network analysis, data visualization

PREREQUISITE KNOWLEDGE: Graph theory, basic manipulation in R


CONTEXT AND DESCRIPTION: You have either a two-column list representing pairs of vertices of an edge or an adjacency matrix, which usually represents some sort of (network) graph.

Maybe a vertex represents a person and an edge represents a relationship i.e. a social network. Maybe you want to organize the graph as a cycle or as a tree. Maybe you want to perform calculations like betweeness centrality or degree centrality.

Before doing any of that, we will first plot the graph and do some basic visual manipulations. In this example, we will start out with writing our own small network in the R console.

VALUE ADDED: As with most of my introductory examples, this particular script and this particular data set are both very simple, and do not lend themselves to a lot of analysis. This is however the foundational script and building block for more complex analyses to be explored later on. Coupled with other data sets and additional functions, a lot more powerful and insightful analyses can be made later on.

In general, a social network analysis offers many benefits. Many technical and non-technical people can interpret and digest the information in the graph visualization very easily. Most importantly, the vertices in a graph illustrates which players are central or significant players. We can even change the size of the nodes to further emphasize which vertex is more significant.

METHODOLOGY: The key idea is to load your dataset and then convert the edges or the adjacency matrix into a graph object. From there, many functions can be applied on your graph object. Again, in this example, we will start out with writing our own small network in the R console.

Here is the script:


Figure 1: R script of igraph script

igraph script


And voila! Here is one of the outputted graphs:

File for Graph


Notice the direction of the edges and compare that to your script. Notice how the edges will change when you change “directed” to  FALSE.

This is the first post for our igraph series. Later, we will load in more complicated files and begin to use more of the manipulation functions and calculation functions.