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?

DESIRED OUTPUT: A graph

LEVEL OF DIFFICULTY: Easy

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

TOOLS AND LIBRARIES: R/R Studio, igraph

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s