We’ll be working in igraph, so go ahead and load that package into R.
library(igraph)
Now you are ready to begin.
For best results, just copy and paste Steps 1 & 3, then enter the rest by hand. This will help you to enter the data correctly while giving you the opportunity to get sued to the igraph syntax.
** To do this, you will create two data frames containing the following elements:
actors <- data.frame(name=c("Barry","Buddy Rich","Burt Reynolds","Cheryl","Conrad Schotz","Conway Stern","Cyril","Framboise","Joshua Grey","Katya","Krieger","Lana","Len Trexler","Lucas Troy","Malory","Nikolai Jakov","Pam","Pigly","Randy Gillette","Ray","Ron Caddilac","Salvio Mascalzoni","Scatterbrain Jane","Skorpio","Sterling","Trinette Magoon"))
relations <- data.frame(from=c("Sterling","Sterling","Sterling","Sterling","Sterling","Sterling","Sterling","Sterling","Lana","Lana","Lana","Lana","Lana","Cheryl","Cheryl","Cheryl","Cheryl","Malory","Malory","Malory","Malory","Malory","Malory","Malory","Malory","Cyril","Cyril","Cyril","Cyril","Pam","Pam","Ray","Krieger","Krieger","Barry"),
to=c("Lana","Cheryl","Pam","Skorpio","Katya","Framboise","Trinette Magoon","Lucas Troy","Pam","Cyril","Barry","Skorpio","Joshua Grey","Cyril","Barry","Conway Stern","Randy Gillette","Cyril","Ron Caddilac","Salvio Mascalzoni","Nikolai Jakov","Burt Reynolds","Buddy Rich","Len Trexler","Conrad Schotz","Trinette Magoon","Scatterbrain Jane","Pam","Framboise","Conrad Schotz","Malory","Cyril","Pigly","Cheryl","Framboise"))
Personally, I enjoy the - apparently popular - convention of naming whatever active network data that I am working with “g”. That way, I do not have to modify my analytic scripts much when I am analyzing multiple networks.
g <- graph_from_data_frame(relations, vertices=actors, directed=FALSE)
In this case, we can list the gender of the various characters
gen <- c("Male","Male","Male","Female","Male","Male","Male","Female","Male","Female","Male","Female","Male","Male","Female","Male","Female","Male","Male","Male","Male","Male","Female","Male","Male","Female")
In this step, you are telling igraph that you are defining a vertex (V()), in the network named g. The dollar sign is an “assignment”, meaning that you are assigning an attribute named “gender” to describe the vertices in the network.
V(g)$gender <- gen
Note: Assigning attributes will help you to define your network and your network visualizations.
If you would like to emphasize certain nodes, or just change the size of all nodes in the network, then you can add a valued attribute to the network. (We will do so in a moment, below.)
You can also add attributes to describe the ties within the network. In this way, you could essentially stack a variety of types of relations into the same network object.
To give an example in the context of the Archer network, you do not only have to stick with one type of relationship (hook-ups). You can also add ties based on past missions, familial relationships, or anything else you can think to add. Later, you can visualize or subset the network to include only certain types of ties, or simply analyze the network in aggregate by not specifying which ties to include.
Another alternative is to add a valued tie attribute to place a value on each of the ties within the network. In this case, you could go back and count (or estimate) the number of hookups each pair of characters had. In a smaller network, visualized tie strengths (expressed in terms of the width of the line) can make the visualization much more interesting.
Try some visualizations.
Now that you have created an igraph network object with at least one attribute, you can visualize it. Start with the basic network visualization in igraph.
plot(g)
We can do better.
This looks a little crowded and either the names are too large, or the nodes are too small to accommodate them.
There is also very little to differentiate them for those who are not familiar with the TV series. Besides, the default color leaves something to be desired.
If you wanted you could give each actor a unique color. For example, you could base the color of each node on the character names V(g)$color <- V(g)$name, and then use the gsub command to specify who has what color.
For simplicity sake, we’ll just keep the coloration to indicating gender.
V(g)$color <- V(g)$gender # Base the color of vertices on the gender attribute
V(g)$color <- gsub("Female","red",V(g)$color) # Specify each of the colors using 'gsub'
V(g)$color <- gsub("Male","green",V(g)$color)
Now, plot it again to see the difference.
plot(g)
The Labels are still too large.
V(g)$label.cex <- 0.5 # To keep it readable, make the font smaller
Now plot it again, but this time use a graph theoretic layout.
plot.igraph(g,layout=layout.fruchterman.reingold)
That comes out fairly well. But you are free to try some of the many layout options that igraph offers. Fo example, you can try:
plot.igraph(g,layout=layout.kamada.kawai)
plot.igraph(g,layout=layout.circle)
plot.igraph(g,layout=layout.gem)
plot.igraph(g,layout=layout.davidson.harel)
plot.igraph(g,layout=layout.drl)
plot.igraph(g,layout=layout.mds)
plot.igraph(g,layout=layout.spring)
You get the idea. You can use the autocomplete function in RStudio to see some of the many options that you have for layouts in igraph.