Claire Battaglia
challenge1
Author

Claire Battaglia

Published

February 22, 2023

Code
library(tidyverse)
library(igraph)

knitr::opts_chunk$set(echo = TRUE)

Data

This dataset represents the teams in Game of Thrones.

Code
# read in data
teams <- read_csv("_data/got/got_teams.csv", show_col_types = FALSE)

# specify from, to fields
teams <- teams %>%
  rename(from = team, to = joins) %>%
  relocate(from, to, alliance, gh, .before = id)

# preview
head(teams, 25)
# A tibble: 25 × 5
   from         to                    alliance        gh    id
   <chr>        <chr>                 <chr>        <dbl> <dbl>
 1 Drogo        follows Drogo         Opportunists     0     0
 2 Littlefinger follows Littlefinger  Opportunists     0     1
 3 Martell      joins the Martells    Opportunists     0     2
 4 Red God      worships the Red God  Opportunists     0     3
 5 Renly        follows Renly         Opportunists     0     4
 6 Second Sons  joins the Second Sons Opportunists     0     5
 7 Stannis      follows Stannis       Opportunists     0     6
 8 Stormcrows   joins the Stormcrows  Opportunists     0     7
 9 Targaryen    joins the Targaryens  Opportunists     0     8
10 Tyrell       joins the Tyrells     Opportunists     0     9
# … with 15 more rows

Now I’ll create a network object.

Code
# create network object
teams.ig <- graph_from_data_frame(teams, directed = FALSE)

# view
teams.ig
IGRAPH b1d14af UN-- 66 33 -- 
+ attr: name (v/c), alliance (e/c), gh (e/n), id (e/n)
+ edges from b1d14af (vertex names):
 [1] Drogo       --follows Drogo         Littlefinger--follows Littlefinger 
 [3] Martell     --joins the Martells    Red God     --worships the Red God 
 [5] Renly       --follows Renly         Second Sons --joins the Second Sons
 [7] Stannis     --follows Stannis       Stormcrows  --joins the Stormcrows 
 [9] Targaryen   --joins the Targaryens  Tyrell      --joins the Tyrells    
[11] Windblown   --joins the Windblown  
+ ... omitted several edges

This tells us a few things.

  1. The network is undirected.
  2. The network is named. I.e. the nodes have names.
  3. The edges are not weighted.
  4. It is not bipartite.
  5. There are 66 nodes/vertices.
  6. There are 33 edges/ties.

We can explore the nodes and edges further.

Code
# get nodes
V(teams.ig)
+ 66/66 vertices, named, from b1d14af:
 [1] Drogo                                  
 [2] Littlefinger                           
 [3] Martell                                
 [4] Red God                                
 [5] Renly                                  
 [6] Second Sons                            
 [7] Stannis                                
 [8] Stormcrows                             
 [9] Targaryen                              
[10] Tyrell                                 
+ ... omitted several vertices
Code
# get edges
E(teams.ig)
+ 33/33 edges from b1d14af (vertex names):
 [1] Drogo             --follows Drogo                          
 [2] Littlefinger      --follows Littlefinger                   
 [3] Martell           --joins the Martells                     
 [4] Red God           --worships the Red God                   
 [5] Renly             --follows Renly                          
 [6] Second Sons       --joins the Second Sons                  
 [7] Stannis           --follows Stannis                        
 [8] Stormcrows        --joins the Stormcrows                   
 [9] Targaryen         --joins the Targaryens                   
[10] Tyrell            --joins the Tyrells                      
+ ... omitted several edges

Let’s try to plot this.

Code
# not sure what this does but I found it on the internet so why not :)
teams.ig <- simplify(teams.ig, remove.multiple = F, remove.loops = T)

# assign weight
E(teams.ig)$weight <- E(teams.ig)$alliance

# plot network
plot(teams.ig,
     vertex.size = 10,
     vertex.color = "maroon",
     vertex.label.cex = .5,
     main = "Game of Thrones Teams")
Warning in (function (graph, coords = NULL, dim = 2, niter = 500, start.temp =
sqrt(vcount(graph)), : NAs introduced by coercion

That looks… sort of useless (but vaguely like a network!).