Week 1 Challenge Solutions

challenge_1
solution
Author

Oriol Vallès Codina

Before starting, the necessary packages should be loaded:

library(tidyverse)
library(igraph)

knitr::opts_chunk$set(echo = TRUE, warning=FALSE,
                      message=FALSE)

Marriages in Game of Thrones

got.marriages <- read_csv('_data/got/got_marriages.csv')

head(got.marriages)

The first two columns indicate the list of edges in the network. The network is symmetric and unweighted, but with 3 types of links (married, engaged, or affair):

table(got.marriages$Type)

 Affair Engaged Married 
     36       2     217 

Finally, a graph can be created using the igraph package

g.got.marriages <- graph_from_data_frame(got.marriages)

and plotted:

plot(g.got.marriages, edge.arrow.size = 0)

We could also color the edges depending on the type of link by creating another column as a factor and changing its levels to color names:

got.marriages$Type.Color <- as.factor(got.marriages$Type)

levels(got.marriages$Type.Color)
[1] "Affair"  "Engaged" "Married"
levels(got.marriages$Type.Color) <- c('gray', 'blue', 'green')

E(g.got.marriages)$color <- got.marriages$Type.Color

plot(g.got.marriages, edge.arrow.size = 0)

Fish Encounters

fish_encounters dataset is directly loaded with tidyverse or tidyr:

head(fish_encounters)

The graph can be created using:

g.fish <- graph_from_data_frame(fish_encounters)

V(g.fish)$name
 [1] "4842"    "4843"    "4844"    "4845"    "4847"    "4848"    "4849"   
 [8] "4850"    "4851"    "4854"    "4855"    "4857"    "4858"    "4859"   
[15] "4861"    "4862"    "4863"    "4864"    "4865"    "Release" "I80_1"  
[22] "Lisbon"  "Rstr"    "Base_TD" "BCE"     "BCW"     "BCE2"    "BCW2"   
[29] "MAE"     "MAW"    

We can color the stations by selecting the nodes with names that cannot be converted into numbers:

V(g.fish)$color[is.na(as.numeric(V(g.fish)$name))] <- 'lightblue'

plot(g.fish, edge.arrow.size = .5)

Likes and Dislikes in Game of Thrones

got.likes <- read.csv('_data/got/got_like_dislike.csv')

head(got.likes)

Although the data is given in the form of an adjacency matrix, the object is a data frame that needs to be coerced into a matrix. The network is asymmetric and weighted, where negative weights indicate dislike.

g.likes <- graph_from_adjacency_matrix(as.matrix(got.likes[ , - c(1 : 3)]), mode = 'directed', weighted = TRUE)

kableExtra :: kable(as.matrix(got.likes[ , - c(1 : 3)]))
Lysa.Arryn Petyr.Baelish Joffrey.Baratheon Margaery.Tyrell Renly.Baratheon Robert.Baratheon Stannis.Baratheon Brienne.of.Tarth Bronn Gregor.Clegane Sandor.Clegane Xaro.Xhoan.Daxos Gendry Balon.Greyjoy Theon.Greyjoy Jaqen.H.ghar Hodor Khal.Drogo Cersei.Lannister Jaime.Lannister Lancel.Lannister Tyrion.Lannister Tywin.Lannister Maester.Luwin Melisandre Jeor.Mormont Jorah.Mormont Osha Podrick.Payne Pyat.Pree Grand.Maester.Pycelle Ros Davos.Seaworth Shae Jon.Snow Arya.Stark Bran.Stark Catelyn.Stark Robb.Stark Sansa.Stark Daenerys.Targaryen Viserys.Targaryen Samwell.Tarly Loras.Tyrell Varys Eddard.Stark
NA 3 0 0 0 1 0 1 0 0 0 0 0 -1 -1 0 2 0 -1 -1 -1 -1 -1 1 0 0 0 0 0 0 0 0 0 0 1 2 2 3 2 2 0 0 0 0 0 3
3 NA -1 1 -2 1 -1 0 -1 -1 -1 0 0 0 0 0 0 0 -2 -2 0 -1 -2 0 0 0 0 0 -1 0 -1 -2 0 -1 0 1 1 5 0 3 -1 0 0 1 -1 -3
0 -1 NA 4 -3 2 -5 -3 -1 3 4 0 -5 -2 -3 0 -1 -3 5 4 1 -2 3 -1 -4 0 -3 -2 -1 0 -1 -5 -2 -1 -1 -2 -2 -3 -5 0 -5 -3 0 2 -1 -5
0 1 3 NA 5 0 -5 -4 0 0 1 0 -5 -1 -2 0 0 0 2 1 0 -1 3 0 -4 0 -2 0 0 0 0 0 -2 -1 0 -1 -1 -2 -5 0 -5 -3 0 5 0 0
0 -2 -5 5 NA 3 -5 2 -1 -2 -2 0 -4 0 -3 0 0 0 -4 -3 -1 -2 -5 1 -5 0 -2 0 -1 0 -1 0 -3 -1 0 2 2 3 2 2 -5 -3 0 5 -1 1
2 1 4 0 3 NA 2 0 0 -1 0 0 4 -1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 0 0 0 0 5 1 1 0 1 1 0 0 NA 0 0 1
0 1 -2 5 5 0 -5 -3 -1 -3 0 0 -1 -1 -1 0 0 0 -2 -1 0 -1 -3 0 -3 0 -1 -3 -1 0 -1 0 -1 0 0 -1 -1 -1 -4 -1 0 0 0 NA -1 0
0 -1 -1 0 -1 -2 -2 -1 -2 0 -1 0 0 0 -1 0 0 0 2 0 0 2 1 0 -3 0 0 -2 -2 0 -3 0 -2 0 0 -5 -5 -5 -5 -1 -3 -1 0 -1 NA -5
3 -1 -5 0 1 5 0 0 -1 -1 0 0 3 -5 3 0 4 0 -4 -5 0 -1 0 5 -5 1 -2 2 0 0 -1 0 0 0 5 5 5 5 5 5 -2 0 1 0 -5 NA

Characters in Game of Thrones seem to dislike each other much more than like each other…

hist(E(g.likes)$weight)

E(g.likes)$color <- colorRampPalette(c("yellow", "blue"))(11)[E(g.likes)$weight + 6]

plot(g.likes, edge.arrow.size = 0.5)