challenge_1
Loading Data and Creating a Network
Author

Jerin Jacob

Published

February 15, 2023

Reading in the Data

GOT Marriage data is read in.

Code
got_marriage <- read_csv("_data/got/got_marriages.csv", show_col_types = FALSE) 
got_marriage
# A tibble: 255 × 5
   From      To        Type    Notes  Generation
   <chr>     <chr>     <chr>   <chr>  <chr>     
 1 Targaryen Stark     Married R+L=J  Current   
 2 Baratheon Martell   Engaged died   Current   
 3 Baratheon Stark     Engaged broken Current   
 4 Martell   Essos     Married <NA>   Current   
 5 Martell   Reach     Affair  <NA>   Current   
 6 Martell   Essos     Affair  <NA>   Current   
 7 Martell   Essos     Affair  <NA>   Current   
 8 Martell   Septa     Affair  <NA>   Current   
 9 Martell   Dorne     Affair  <NA>   Current   
10 Martell   Targaryen Married <NA>   Current   
# … with 245 more rows

The data is about the marriages happened between families in GOT. There are 5 variables and 255 case. First 2 variables are ‘From’ and ‘To’ which are the most important part of a ‘Network Analysis’ data. We also have the ‘Type’ of relationship and ‘Generation’ which classify the case as ‘Past’ or ‘Current’.

Creating igraph from dataframe

We are cretaing an igraph from the GOT marriage dataframe so that we can do the network analysis.

Code
got_marriage.net<-graph_from_data_frame(got_marriage,
                                      directed=FALSE)
#deg <- degree(got_marriage.net, mode="all")
  

got_marriage.net
IGRAPH c0f6780 UN-- 20 255 -- 
+ attr: name (v/c), Type (e/c), Notes (e/c), Generation (e/c)
+ edges from c0f6780 (vertex names):
 [1] Targaryen--Stark       Baratheon--Martell     Baratheon--Stark      
 [4] Martell  --Essos       Martell  --Reach       Martell  --Essos      
 [7] Martell  --Essos       Martell  --Septa       Martell  --Dorne      
[10] Targaryen--Martell     Targaryen--Essos       Targaryen--Essos      
[13] Baratheon--Lannister   Baratheon--Vale        Baratheon--Riverlands 
[16] Baratheon--Crownlands  Baratheon--Reach       Baratheon--Westerlands
[19] Baratheon--Crownlands  Lannister--Lannister   Baratheon--Reach      
[22] Baratheon--Tyrell      Tyrell   --Reach       Tyrell   --Reach      
+ ... omitted several edges
Code
 # got_marriage.net %>%
 #  group_by(From, To, Type) %>%
 #  summarise(total_count = n())

An igraph is created for the data that is groouped by ‘From’, ‘To’, and ‘Type’

Code
got_marriage_group <- got_marriage %>%
  group_by(From, To, Type) %>%
  summarise(total_count = n())
`summarise()` has grouped output by 'From', 'To'. You can override using the
`.groups` argument.
Code
got_marriage_group.net<-graph_from_data_frame(got_marriage_group,
                                      directed=FALSE)
  

got_marriage_group.net
IGRAPH ffe3463 UN-- 20 88 -- 
+ attr: name (v/c), Type (e/c), total_count (e/n)
+ edges from ffe3463 (vertex names):
 [1] Arryn     --Arryn       Arryn     --Tully       Arryn     --Vale       
 [4] Baratheon --Crownlands  Baratheon --Lannister   Baratheon --Martell    
 [7] Baratheon --Reach       Baratheon --Reach       Baratheon --Riverlands 
[10] Baratheon --Stark       Baratheon --Stormlands  Baratheon --Tyrell     
[13] Baratheon --Tyrell      Baratheon --Vale        Baratheon --Westerlands
[16] Crownlands--Essos       Crownlands--Riverlands  Crownlands--Stormlands 
[19] Crownlands--Westerlands Crownlands--Frey        Frey      --Essos      
[22] Frey      --Frey        Frey      --Lannister   Frey      --North      
+ ... omitted several edges

GOT marriage network has 20 vertices as listed below. It has 255 edges but the number of unique type relationship is 3; ‘Married’, ‘Engagedf’, ‘Affair’. Unique classification of ‘generation’ attribute are ‘Current’ and ‘Past’.

Code
#table(got_marriage$Type)

V(got_marriage.net)
+ 20/20 vertices, named, from c0f6780:
 [1] Targaryen   Baratheon   Martell     Lannister   Tyrell      Reach      
 [7] North       Riverlands  Westerlands Stark       Vale        Arryn      
[13] Tully       Frey        Crownlands  Stormlands  Essos       Septa      
[19] Dorne       Beyond Wall
Code
unique(E(got_marriage.net)$Type)
[1] "Married" "Engaged" "Affair" 
Code
unique(E(got_marriage.net)$Generation)
[1] "Current" "Past"   

Again, checking the vertex count and edge count of the network. The vertex count for grouped network data and the original network data are same as 20. But edge count differs. For original network data, there are 255 edges but for grouped data, edges are 88. This is because, in the grouped network, each edge is a type of relationship from a family to another family. In the original data, each edge represent a unique relationship between families and therefore it is 255.

Code
vcount(got_marriage.net)
[1] 20
Code
ecount(got_marriage.net)
[1] 255
Code
vcount(got_marriage_group.net)
[1] 20
Code
ecount(got_marriage_group.net)
[1] 88

The network is not bipartite which means it is a single mode network. It is un directed or other words symmetrical. It is also not weighted.

Code
is_bipartite(got_marriage.net)
[1] FALSE
Code
is_directed(got_marriage.net)
[1] FALSE
Code
is_weighted(got_marriage.net)
[1] FALSE

The vertex attributes and edge attributes are listed. Vertices has only one attribute named ‘name’. and edges have 3 attributes; ‘Type’, ‘Notes’, ‘Generation’.

Code
vertex_attr_names(got_marriage.net)
[1] "name"
Code
edge_attr_names(got_marriage.net)
[1] "Type"       "Notes"      "Generation"

The vertex attribute name is accessed.

Code
V(got_marriage.net)$name
 [1] "Targaryen"   "Baratheon"   "Martell"     "Lannister"   "Tyrell"     
 [6] "Reach"       "North"       "Riverlands"  "Westerlands" "Stark"      
[11] "Vale"        "Arryn"       "Tully"       "Frey"        "Crownlands" 
[16] "Stormlands"  "Essos"       "Septa"       "Dorne"       "Beyond Wall"
Code
E(got_marriage.net)$Generation
  [1] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
  [8] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [15] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [22] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [29] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [36] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [43] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [50] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [57] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [64] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [71] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [78] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [85] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [92] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
 [99] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
[106] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
[113] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[120] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[127] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[134] "Past"    "Current" "Current" "Current" "Current" "Current" "Current"
[141] "Current" "Current" "Current" "Current" "Current" "Current" "Current"
[148] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[155] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[162] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[169] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[176] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[183] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[190] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[197] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[204] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[211] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[218] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[225] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[232] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[239] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[246] "Past"    "Past"    "Past"    "Past"    "Past"    "Past"    "Past"   
[253] "Past"    "Past"    "Past"   

Plotting the GOT marriage network

Code
plot(got_marriage.net, 
            arrow.mode="-")

Plotting the grouped GOT marriage network

Code
plot(got_marriage_group.net, arrow_mode = "-")