Making use of the igraph package to draw a network

The igraph package is a reference point for network visualization in the R environment.

igraph is actually more than an R package, since you can use igraph tools even with Python and C/C++.

If you are performing analysis that involves network visualizations and R, you should look at this package as one of your more relevant allies.

This recipe will let you enter the wide world of network visualization with R, showing you how to create a network plot starting from a data frame.

Getting ready

Install the igraph package:

install.packages("igraph")
library(igraph)

We will use the flo dataset showing the relationship between Renaissance Florentine families.

Getting ready

Marriage between Adimari and Cascioni families, from Cassone Adimari, around 1420, Florence

This dataset is provided as an adjacency matrix within the network package.

Therefore, in order to make it available, we have to install the network package and load the dataset by running the data() function on it:

install.packages("network")
library(network)
data(flo)

You can then visualize the flo dataset within the RStudio viewer pane by running the View command:

View(flo)

This will result in the following visualization:

Getting ready

This dataset is actually an adjacency matrix, where 0 at the I, J cell expresses no relationship between the i-esim and the j-esim families, while 1 represents evidence of a relationship.

For instance, we can assume from the matrix that some kind of relationship exists between the Albizzi and the Tornabuoni family. I am guessing you are curious, so I will let you know what this relationship is.

At the age of 18, on June 15, 1486, Giovanna Albizzi linked her family to one of the Tornabuonis by marrying Lorenzo Tornabuoni. Was it true love?

Ok, I think you are getting too curious now...

Note

Be aware that this recipe can be applied even having as an input and hedge list, simply by substituting steps 1 and 2 with a single step where the hedge list is passed as an argument to the graph.data.frame() function.

How to do it...

  1. Create a matrix object from the flo dataset:
    dataset <- as.matrix(flo)
  2. Create an igraph object from the defined dataset:
    net     <- graph.adjacency(dataset,mode = "undirected", weighted = NULL)
  3. Compute the degree of each vertex:
    deg <- degree(net, mode = "all")
  4. Set the size of each vertex equal to its degree:
    V(net)$size <- deg*4
  5. Set the arrow size:
    E(net)$arrow.size <- .2
  6. Set the color of the edges:
    E(net)$edge.color <- "gray"
  7. Plot your network:
    plot(net)

    You will then see the following plot:

    How to do it...

How it works…

In step 1, we create a matrix object from the flo dataset. The graph.adjacency() function requires you to pass to it a matrix object in order to create an igrpah object. Therefore, we cast our data frames into a matrix object, leveraging the as.matrix() function.

Then in step 2, we create an igraph object from the defined dataset. Creating an igraph object is necessary in order to make use of all the great utilities made available through the igraph package.

In this case, since we are working on an adjacency matrix, we will obtain this through the graph.adjacency() function. This function takes as an input an adjacency matrix and gives as an output an igraph object.

In step 3, we compute the degree of each vertex. The degree is a matrix associated to every node of a network. It basically measures the number of edges connected to that node.

To begin with, it is a convenient way to measure the relevance of a node in a network. Why? To answer this question, I will let you take a look at the Medici node.

How many connections did the Medici family have? Six connections, which is the maximum number in the matrix. As you may know (if not, go to https://en.wikipedia.org/wiki/House_of_Medici), the Medici were one of the most influent and powerful families of the Italian Renaissance period, and the degree computed for this family accurately reflected this reality.

We measure the degree of each node just by calling the degree() function on the net object.

Next, in step 4 we set the size of each vertex equal to its degree. We now set the value of an attribute of our network vertex (node). In particular, we set the node size, and this attribute is set equal to the previously computed degree for each node.

As you would expect if you have not skipped the explanation in the previous step, the Medici family is the one represented by the largest node. Note that using the same syntax, other attributes can be added or set to vertexes and to edges as well (as you can see in the next steps).

In step 5, we set the arrow size. In a way similar to what we have done with nodes, we now set the size of the edges. In this case, we set it to a fixed number, but we can even set it as a function of some other network parameter.

In step 6, we set the color of the edges. In this step, the edge color attribute is set to grey.

Lastly, in step 7 we plot our network. Plotting an igraph object is really straightforward, requiring you to only run the base R plot() function. As seen in the Looking at your data using the plot() function recipe, this function is able to change its output depending on the class of data given as input, thanks to specific methods that enrich the function's capabilities for a given kind of object. Refer to the cited recipe for further information on this function, but in the meanwhile, appreciate the result of your efforts!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset