Kaitlin Kavlie PSYC-541 Lab #6: Harry Potter Network Analysis
The first code shown below was used to read the data into links and nodes.
HP_nodes <- read_csv("HP_nodes.csv")
Rows: 65 Columns: 3
-- Column specification ---------------------------------------------------------------------------------------------------------------------------------------------
Delimiter: ","
chr (2): name, bio
dbl (1): id
i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
HP_links <- read_csv("HP_links.csv")
Rows: 356 Columns: 2
-- Column specification ---------------------------------------------------------------------------------------------------------------------------------------------
Delimiter: ","
dbl (2): from, to
i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Then the code directly below was used to create the network in igraph.
HP_network <- graph_from_data_frame(HP_links,
vertices = HP_nodes,
directed = F)
HP_network %>%
edge_density()
[1] 0.1711538
Using the first code I analyzed for the distances in the network.
HP_network %>%
distances()
Regulus Arcturus Black Sirius Black Lavender Brown Cho Chang Vincent Crabbe Sr. Vincent Crabbe Bartemius "Barty" Crouch Sr.
Regulus Arcturus Black 0 4 4 4 1 2 2
Sirius Black 4 0 2 2 3 4 3
Lavender Brown 4 2 0 2 3 4 3
Cho Chang 4 2 2 0 3 4 3
Vincent Crabbe Sr. 1 3 3 3 0 1 1
Vincent Crabbe 2 4 4 4 1 0 2
Bartemius "Barty" Crouch Sr. 2 3 3 3 1 2 0
Bartemius "Barty" Crouch Jr. 3 3 2 2 2 3 3
Fleur Delacour 4 2 2 2 3 4 3
Cedric Diggory 4 2 3 1 3 4 3
Alberforth Dumbledore 4 2 2 2 3 4 3
Albus Dumbledore 3 1 2 2 2 3 2
Dudley Dursley Inf Inf Inf Inf Inf Inf Inf
Petunia Dursley Inf Inf Inf Inf Inf Inf Inf
Vernon Dursley Inf Inf Inf Inf Inf Inf Inf
Bartemius "Barty" Crouch Jr. Fleur Delacour Cedric Diggory Alberforth Dumbledore Albus Dumbledore Dudley Dursley Petunia Dursley
Regulus Arcturus Black 3 4 4 4 3 Inf Inf
Sirius Black 3 2 2 2 1 Inf Inf
Lavender Brown 2 2 3 2 2 Inf Inf
Cho Chang 2 2 1 2 2 Inf Inf
Vincent Crabbe Sr. 2 3 3 3 2 Inf Inf
Vincent Crabbe 3 4 4 4 3 Inf Inf
Bartemius "Barty" Crouch Sr. 3 3 3 3 2 Inf Inf
Bartemius "Barty" Crouch Jr. 0 2 3 3 3 Inf Inf
Fleur Delacour 2 0 2 2 2 Inf Inf
Cedric Diggory 3 2 0 2 2 Inf Inf
Alberforth Dumbledore 3 2 2 0 1 Inf Inf
Albus Dumbledore 3 2 2 1 0 Inf Inf
Dudley Dursley Inf Inf Inf Inf Inf 0 1
Petunia Dursley Inf Inf Inf Inf Inf 1 0
Vernon Dursley Inf Inf Inf Inf Inf 1 1
Vernon Dursley Argus Filch Seamus Finnigan Nicolas Flamel Cornelius Fudge Goyle Sr. Gregory Goyle Hermione Granger Rubeus Hagrid
Regulus Arcturus Black Inf 3 4 4 3 2 3 3 3
Sirius Black Inf 2 2 2 3 4 5 1 2
Lavender Brown Inf 3 1 3 3 4 4 2 2
Cho Chang Inf 3 1 3 3 4 4 1 2
Vincent Crabbe Sr. Inf 2 3 3 2 1 2 2 2
Vincent Crabbe Inf 2 4 4 2 2 1 3 3
Bartemius "Barty" Crouch Sr. Inf 3 3 3 2 2 3 3 3
Bartemius "Barty" Crouch Jr. Inf 4 2 4 3 2 2 2 2
Fleur Delacour Inf 3 2 3 3 4 4 1 2
Cedric Diggory Inf 3 2 3 4 4 5 2 2
Alberforth Dumbledore Inf 2 2 2 3 4 5 1 2
Albus Dumbledore Inf 1 2 1 2 3 4 1 1
Dudley Dursley 1 Inf Inf Inf Inf Inf Inf Inf Inf
Petunia Dursley 1 Inf Inf Inf Inf Inf Inf Inf Inf
Vernon Dursley 0 Inf Inf Inf Inf Inf Inf Inf Inf
Igor Karkaroff Viktor Krum Bellatrix Lestrange Alice Longbottom Frank Longbottom Neville Longbottom Luna Lovegood Xenophilius Lovegood
Regulus Arcturus Black 3 4 2 5 5 4 4 5
Sirius Black 3 2 4 3 3 2 2 3
Lavender Brown 4 3 4 2 2 1 2 3
Cho Chang 3 2 4 2 2 1 2 3
Vincent Crabbe Sr. 2 3 1 4 4 3 3 4
Vincent Crabbe 3 4 2 5 5 4 4 5
Bartemius "Barty" Crouch Sr. 1 2 2 4 4 3 3 4
Bartemius "Barty" Crouch Jr. 4 3 2 2 2 1 2 3
Fleur Delacour 2 1 4 2 2 1 2 3
Cedric Diggory 2 1 4 3 3 2 2 3
Alberforth Dumbledore 3 2 4 3 3 2 2 3
Albus Dumbledore 2 2 3 3 3 2 2 3
Dudley Dursley Inf Inf Inf Inf Inf Inf Inf Inf
Petunia Dursley Inf Inf Inf Inf Inf Inf Inf Inf
Vernon Dursley Inf Inf Inf Inf Inf Inf Inf Inf
Remus Lupin Draco Malfoy Lucius Malfoy Narcissa Malfoy Olympe Maxime Minerva McGonagall Alastor "Mad-Eye" Moody Peter Pettigrew
Regulus Arcturus Black 4 3 2 3 4 4 4 2
Sirius Black 1 5 4 5 2 1 1 4
Lavender Brown 2 5 4 4 3 2 2 3
Cho Chang 2 5 4 4 3 2 2 3
Vincent Crabbe Sr. 3 2 1 2 3 3 3 1
Vincent Crabbe 4 1 2 2 4 3 4 2
Bartemius "Barty" Crouch Sr. 3 3 2 3 2 3 3 2
Bartemius "Barty" Crouch Jr. 3 3 2 2 3 3 3 1
Fleur Delacour 2 5 4 4 1 2 1 3
Cedric Diggory 2 5 4 5 3 2 2 4
Alberforth Dumbledore 2 5 4 5 2 2 2 4
Albus Dumbledore 1 4 3 4 1 1 1 3
Dudley Dursley Inf Inf Inf Inf Inf Inf Inf Inf
Petunia Dursley Inf Inf Inf Inf Inf Inf Inf Inf
Vernon Dursley Inf Inf Inf Inf Inf Inf Inf Inf
Harry Potter James Potter Lily Potter Quirinus Quirrell Tom Riddle Sr. Mary Riddle Lord Voldemort Rita Skeeter Severus Snape
Regulus Arcturus Black 3 4 3 4 Inf Inf 2 Inf 2
Sirius Black 1 1 1 2 Inf Inf 4 Inf 2
Lavender Brown 2 3 3 3 Inf Inf 3 Inf 2
Cho Chang 1 2 2 3 Inf Inf 3 Inf 2
Vincent Crabbe Sr. 2 3 2 3 Inf Inf 1 Inf 1
Vincent Crabbe 3 4 3 4 Inf Inf 2 Inf 2
Bartemius "Barty" Crouch Sr. 3 3 3 3 Inf Inf 2 Inf 2
Bartemius "Barty" Crouch Jr. 2 3 3 4 Inf Inf 1 Inf 3
Fleur Delacour 1 2 2 3 Inf Inf 3 Inf 2
Cedric Diggory 1 2 2 3 Inf Inf 4 Inf 2
Alberforth Dumbledore 1 2 2 2 Inf Inf 4 Inf 2
Albus Dumbledore 1 1 1 1 Inf Inf 3 Inf 1
Dudley Dursley Inf Inf Inf Inf Inf Inf Inf Inf Inf
Petunia Dursley Inf Inf Inf Inf Inf Inf Inf Inf Inf
Vernon Dursley Inf Inf Inf Inf Inf Inf Inf Inf Inf
Nymphadora Tonks Dolores Janes Umbridge Arthur Weasley Bill Weasley Charlie Weasley Fred Weasley George Weasley Ginny Weasley
Regulus Arcturus Black 5 3 4 4 4 3 3 3
Sirius Black 1 3 1 2 2 1 1 1
Lavender Brown 3 4 2 2 2 2 2 2
Cho Chang 3 4 2 2 2 2 2 2
Vincent Crabbe Sr. 4 2 3 3 3 2 2 2
Vincent Crabbe 5 1 4 4 4 3 3 3
Bartemius "Barty" Crouch Sr. 4 3 2 2 2 2 2 2
Bartemius "Barty" Crouch Jr. 4 4 3 3 3 3 3 3
Fleur Delacour 3 4 2 1 2 2 2 1
Cedric Diggory 3 4 2 2 2 2 2 2
Alberforth Dumbledore 3 3 2 2 2 2 2 2
Albus Dumbledore 2 2 1 1 1 1 1 1
Dudley Dursley Inf Inf Inf Inf Inf Inf Inf Inf
Petunia Dursley Inf Inf Inf Inf Inf Inf Inf Inf
Vernon Dursley Inf Inf Inf Inf Inf Inf Inf Inf
Molly Weasley Percy Weasley Ron Weasley Dobby Fluffy Hedwig Moaning Myrtle Aragog Grawp
Regulus Arcturus Black 4 3 3 4 4 4 4 4 4
Sirius Black 1 2 1 2 3 2 2 2 2
Lavender Brown 2 2 1 2 3 3 3 2 2
Cho Chang 2 2 1 2 3 2 2 2 2
Vincent Crabbe Sr. 3 2 2 3 3 3 3 3 3
Vincent Crabbe 4 3 3 4 4 4 4 4 4
Bartemius "Barty" Crouch Sr. 2 1 2 3 4 4 4 3 3
Bartemius "Barty" Crouch Jr. 3 3 2 3 3 3 3 3 3
Fleur Delacour 1 2 1 2 3 2 2 2 2
Cedric Diggory 2 3 2 2 3 2 2 3 2
Alberforth Dumbledore 2 2 1 1 3 2 2 2 2
Albus Dumbledore 1 1 1 2 2 2 2 2 2
Dudley Dursley Inf Inf Inf Inf Inf Inf Inf Inf Inf
Petunia Dursley Inf Inf Inf Inf Inf Inf Inf Inf Inf
Vernon Dursley Inf Inf Inf Inf Inf Inf Inf Inf Inf
[ reached getOption("max.print") -- omitted 50 rows ]
Then I used the code directly below to create the histogram displaying the network’s distances.
HP_network %>%
distances() %>%
as.vector() %>%
as_tibble() %>%
plot_ly(x = ~value) %>%
add_histogram()
HP_network %>%
get_diameter() %>%
length()
[1] 7
With the code below I mutated label into the HP_nodes data.
HP_nodes <- HP_nodes %>%
mutate(label = name)
With the code below I mutated title into the HP_nodes data.
HP_nodes <- HP_nodes %>%
mutate(title = name)
With the code below I mutated degree into the HP_network data.
HP_network %>%
degree()
Regulus Arcturus Black Sirius Black Lavender Brown Cho Chang Vincent Crabbe Sr.
1 21 3 8 11
Vincent Crabbe Bartemius "Barty" Crouch Sr. Bartemius "Barty" Crouch Jr. Fleur Delacour Cedric Diggory
7 4 3 11 4
Alberforth Dumbledore Albus Dumbledore Dudley Dursley Petunia Dursley Vernon Dursley
6 37 5 4 3
Argus Filch Seamus Finnigan Nicolas Flamel Cornelius Fudge Goyle Sr.
4 10 1 5 4
Gregory Goyle Hermione Granger Rubeus Hagrid Igor Karkaroff Viktor Krum
7 40 18 4 6
Bellatrix Lestrange Alice Longbottom Frank Longbottom Neville Longbottom Luna Lovegood
5 4 4 15 9
Xenophilius Lovegood Remus Lupin Draco Malfoy Lucius Malfoy Narcissa Malfoy
1 15 7 8 6
Olympe Maxime Minerva McGonagall Alastor "Mad-Eye" Moody Peter Pettigrew Harry Potter
5 17 14 4 34
James Potter Lily Potter Quirinus Quirrell Tom Riddle Sr. Mary Riddle
11 9 2 2 2
Lord Voldemort Rita Skeeter Severus Snape Nymphadora Tonks Dolores Janes Umbridge
9 0 19 4 5
Arthur Weasley Bill Weasley Charlie Weasley Fred Weasley George Weasley
32 27 26 25 32
Ginny Weasley Molly Weasley Percy Weasley Ron Weasley Dobby
28 21 23 43 5
Fluffy Hedwig Moaning Myrtle Aragog Grawp
2 1 1 3 5
With the code below I mutated degree into the HP_nodes data.
HP_nodes <- HP_nodes %>%
mutate(degree = degree(HP_network))
Then with the code below I mutated set value = degree into the HP_nodes data.
HP_nodes <- HP_nodes %>%
mutate(value = degree)
With the code below I piped HP_network into edge_betweenness.
HP_network %>%
edge_betweenness()
[1] 5.9979038 12.5808831 0.9905196 3.3361961 3.0655122 10.9566716 1.7929218 7.3602597 25.7197633 2.8981644 3.5222655 3.7068474 4.1508977
[14] 9.6112485 14.4652661 39.1421951 4.5760496 1.7207071 14.1784719 13.7731679 13.7874573 17.9715881 58.0000000 45.3456915 23.6576223 54.8365213
[27] 52.3541186 36.0256857 42.3181422 335.9160513 45.3456915 22.6908066 13.6728485 13.8166590 23.6576223 28.6590002 52.9496952 117.0224097 66.6237490
[40] 21.2672881 8.0643112 15.7245729 3.0360920 10.8849132 4.9637798 6.1883116 4.1292830 8.2509554 4.5760496 45.6693293 9.3092507 13.5668900
[53] 12.4312801 13.3356078 1.9522792 5.9979038 9.3092507 13.1768422 13.0984769 14.4147512 2.2685839 2.2685839 4.2479120 21.7583600 14.6329761
[66] 12.3444993 44.3907682 4.2508050 7.6900293 7.9644069 6.2701914 6.0693863 6.4532563 4.4082011 28.8178296 13.7113064 0.0000000 0.5000000
[79] 1.0000000 0.0000000 0.5000000 1.0000000 40.1296817 27.3450656 44.7805231 4.3925388 1.7207071 10.2364470 9.2592719 3.0944444 12.7830043
[92] 7.9513701 8.2855157 58.0000000 50.7683950 7.1654359 92.3929643 54.6670008 6.2716317 9.2095960 6.2716317 13.6728485 3.3544515 24.1739578
[105] 3.7101449 1.8016122 4.0198396 5.0890741 19.3381571 15.9857588 23.0260231 29.0000000 11.0175587 5.1831519 11.3538352 10.6829105 39.9012614
[118] 19.8760295 26.7833896 11.9117560 4.4575941 13.0984769 16.7290479 16.7290479 33.0523689 7.7988248 5.1785714 34.2411356 8.2095960 3.7669468
[131] 5.9065320 0.3333333 0.3333333 57.0000000 0.3333333 57.0000000 42.6131287 23.0260231 28.5634334 9.9133987 9.9133987 22.9601265 15.9857588
[144] 18.0735570 58.0000000 1.3666990 0.9905196 10.7600789 22.0763422 5.0881701 13.8166590 3.3544515 13.6481651 4.8546675 13.6481651 11.5146668
[157] 14.2562135 11.5146668 21.8475757 5.9065320 11.9117560 9.5938225 33.2377785 20.8215081 23.1880242 4.4575941 2.2685839 7.1526930 7.1526930
[170] 6.1768998 6.1768998 5.8896029 5.8896029 2.2685839 2.2685839 5.1196129 3.0870407 2.1147178 5.1196129 4.6974882 4.6974882 4.6974882
[183] 4.6974882 4.2479120 4.2479120 20.4053705 3.1458333 3.1458333 13.2130005 13.2130005 1.3666990 1.3666990 1.7929218 1.7929218 4.3310803
[196] 4.3310803 7.4657810 7.4657810 0.5000000 0.5000000 40.3561161 40.3561161 19.3381571 19.3381571 19.3381571 19.3381571 29.0000000 29.0000000
[209] 7.4657810 7.4657810 10.7600789 10.7600789 7.1654359 7.1654359 5.8034615 2.8981644 4.2508050 9.2580191 5.8034615 0.4744882 0.4744882
[222] 0.4056912 0.4056912 0.7078080 0.7078080 0.5591294 0.5591294 0.5591294 0.5591294 0.9009686 0.9009686 4.5638368 4.5638368 10.1518565
[235] 5.9189965 5.9189965 0.4744882 0.4744882 0.5909091 0.5909091 0.8470304 0.8470304 0.6992740 0.6992740 0.6992740 0.6992740 0.8951073
[248] 0.8951073 4.0592133 4.0592133 6.3021141 10.4529337 6.3021141 0.4056912 0.4056912 0.7714668 0.7714668 0.6381334 0.6381334 0.6381334
[261] 0.6381334 0.9873758 0.9873758 3.7154435 3.7154435 5.4028689 6.7291406 8.5106981 18.2750306 0.7078080 0.8470304 0.7714668 0.5000000
[274] 0.7019231 1.0287601 4.5016066 5.2978704 4.0198396 7.3711725 7.8488355 19.8629159 0.5591294 0.6992740 0.6381334 0.5000000 0.6736111
[287] 0.9989918 4.5744185 4.6105000 4.6131389 8.1994242 8.2906389 19.6356280 0.9009686 0.8951073 0.9873758 0.6736111 0.7019231 1.0596802
[300] 4.6325682 4.6325682 4.3153595 3.6930482 4.4082011 5.0890741 8.3734102 1.0714286 1.1015298 1.1950705 1.0287601 0.9989918 1.0596802
[313] 5.8659706 4.7679382 4.5638368 4.0592133 3.7154435 4.5016066 4.5744185 4.6325682 5.8659706 29.0039171 9.6112485 8.2509554 13.7113064
[326] 8.2855157 1.8016122 11.3538352 28.5634334 18.0735570 22.1608511 5.8896029 6.9319393 7.8056241 38.9984855 7.1414594 7.3187067 7.6499137
[339] 5.2978704 4.6105000 4.3153595 4.7679382 29.0039171 13.1439521 16.7312641 15.0047094 14.7551072 13.1439521 29.0000000 58.0000000 58.0000000
[352] 11.0175587 15.5242926 5.1831519 15.3781395 35.9648827
Then with the following code I mutated betweenness into HP_links.
HP_links <- HP_links %>%
mutate(betweenness = edge_betweenness(HP_network)) %>%
mutate(value = betweenness)
visNetwork(HP_nodes,
HP_links,
main = "Network of Characters in Harry Potter") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T)
I used the first code below to analyze communities in the HP_network.
HP_network %>%
infomap.community() %>%
membership()
Regulus Arcturus Black Sirius Black Lavender Brown Cho Chang Vincent Crabbe Sr.
1 2 3 4 1
Vincent Crabbe Bartemius "Barty" Crouch Sr. Bartemius "Barty" Crouch Jr. Fleur Delacour Cedric Diggory
5 1 6 7 4
Alberforth Dumbledore Albus Dumbledore Dudley Dursley Petunia Dursley Vernon Dursley
8 8 9 9 9
Argus Filch Seamus Finnigan Nicolas Flamel Cornelius Fudge Goyle Sr.
10 4 8 10 5
Gregory Goyle Hermione Granger Rubeus Hagrid Igor Karkaroff Viktor Krum
5 11 12 13 13
Bellatrix Lestrange Alice Longbottom Frank Longbottom Neville Longbottom Luna Lovegood
14 15 15 3 3
Xenophilius Lovegood Remus Lupin Draco Malfoy Lucius Malfoy Narcissa Malfoy
3 16 17 17 14
Olympe Maxime Minerva McGonagall Alastor "Mad-Eye" Moody Peter Pettigrew Harry Potter
13 7 8 6 7
James Potter Lily Potter Quirinus Quirrell Tom Riddle Sr. Mary Riddle
2 18 8 19 19
Lord Voldemort Rita Skeeter Severus Snape Nymphadora Tonks Dolores Janes Umbridge
6 20 18 16 10
Arthur Weasley Bill Weasley Charlie Weasley Fred Weasley George Weasley
21 22 22 21 23
Ginny Weasley Molly Weasley Percy Weasley Ron Weasley Dobby
23 24 24 11 11
Fluffy Hedwig Moaning Myrtle Aragog Grawp
12 7 7 12 7
I used the code directly below to mutate a group column in the HP_nodes data.
HP_nodes <- HP_nodes %>%
mutate(group = membership(infomap.community(HP_network)))
Then I used the final code to create an interactive graph that includes the groups created above and has color-coded the groups.
visNetwork(HP_nodes,
HP_links,
main = "Network of Characters in Harry Potter") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T, selectedBy = "group")