Week 1 Challenge Instructions

challenge_1
instructions
Loading Data and Creating a Network
Author

Amer Abuhasan

Published

February 13, 2023

Challenge Overview

Today’s challenge is to

  1. read in a dataset, and

  2. create a network object

Load the Data

Read in one (or more) of the following data sets, using the correct R package and command.

  • got_marriages.csv
  • fish_encounters dataset (available in the tidyr package)
  • got_like_dislike.csv

Find the _data folder, located inside the posts folder. Then you can read in the data, using base read.csv or read_csv standard tidy read command to read Comma Separated Values files or, alternatively, read the data in directly from loading a package.

Code
#load in DF
got.marriages <- read_csv('_data/got/got_marriages.csv')

head(got.marriages)
# A tibble: 6 × 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   
Code
table(got.marriages$Type)

 Affair Engaged Married 
     36       2     217 
Code
g.got.marriages <- graph_from_data_frame(got.marriages)

table(got.marriages$Type)

 Affair Engaged Married 
     36       2     217 
Code
plot(g.got.marriages,edge.arrow.size=0)

Code
#color edges based on link 


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

levels(got.marriages$Type.Color)
[1] "Affair"  "Engaged" "Married"
Code
#coloring 

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)

Code
head(fish_encounters)
# A tibble: 6 × 3
  fish  station  seen
  <fct> <fct>   <int>
1 4842  Release     1
2 4842  I80_1       1
3 4842  Lisbon      1
4 4842  Rstr        1
5 4842  Base_TD     1
6 4842  BCE         1
Code
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"    
Code
V(g.fish)$color[is.na(as.numeric(V(g.fish)$name))] <- 'lightblue'

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

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

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

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

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

Add any comments or documentation as needed. More challenging data sets may require additional code chunks and documentation.

Create a Network

Load the package igraph and create an igraph object (i.e. a graph or network) in the form of an edge list. The command may vary whether the data is given as a list of connections or an adjacency matrix. Is the network directed or undirected; weighted or unweighted; unimodal or bipartite? Can you plot it?