library(tidyverse)
library(igraph) # This is the package to analyze the network
library(visNetwork) # Creates visualizations of the network
library(DT)
library(plotly)
HP_nodes <- read_csv("HP_nodes.csv")
-- Column specification ------------------------------------------------------------------------
cols(
id = col_double(),
name = col_character(),
bio = col_character()
)
HP_links <- read_csv("HP_links.csv")
-- Column specification ------------------------------------------------------------------------
cols(
from = col_double(),
to = col_double()
)
The following network analysis is an analysis of characters from the bestselling books and fantasy films, Harry Potter. The following tables indicates there are 65 characters and 356 ways they are connected.
HP_nodes %>%
datatable(rownames = F)
HP_links %>%
datatable(rownames = F)
All relationships in this analysis are considered to be two-way and the density, which is the number of connections divided by the potential connections, is 0.17.
HP_network <- graph_from_data_frame(HP_links,
vertices = HP_nodes,
directed = F)
HP_network %>%
edge_density()
[1] 0.1711538
The shortest paths between two nodes is two. This graph indicates there are many nodes in this network that are only two or three hops apart, however, there are some nodes that are 6 or more hops apart. The graph shows 65 connections at 0. Sixty-five is the total number of characters in this analysis. They are counted as connected to themselves with 0 hops. The mean distance between two connections is calculated at 2.64.
HP_network %>%
distances() %>%
as.vector() %>% # these two lines convert the distances matrix
as_tibble() %>% # to something plotly can graph
plot_ly(x = ~value) %>%
add_histogram()
HP_network %>%
mean_distance()
[1] 2.64723
Next we will look at the length of the diameter of the network. The diameter is 7 which is the longest path between nodes.
HP_network %>%
get_diameter() %>%
length()
[1] 7
HP_nodes <- HP_nodes %>%
mutate(label = name)
HP_nodes <- HP_nodes %>%
mutate(title = name)
HP_nodes <- HP_nodes %>%
mutate(degree = degree(HP_network))
HP_nodes <- HP_nodes %>%
mutate(value = degree)
Now we will determine which character had the most connections in the Harry Potter Network. In the graph below, we can see that Vincent Crabbe, Sr., was the go-between for Severus Snape and 8 other characters.
HP_links <- HP_links %>%
mutate(betweenness = edge_betweenness(HP_network)) %>%
mutate(value = betweenness)
visNetwork(HP_nodes,
HP_links,
main = "Network of Harry Potter Characters") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T)
Finally, we will determine who is in which network.The graph below indicates that those in group 1 compromise the largest group which includes Harry Potter. However, Rita Skeeter, a reporter with the Daily Prophet, was in a group all by herself.
HP_network %>%
infomap.community()
IGRAPH clustering infomap, groups: 21, mod: 0.17
+ groups:
$`1`
[1] "Lavender Brown" "Hermione Granger" "Alastor \"Mad-Eye\" Moody"
[4] "Ron Weasley" "Dobby" "Aragog"
$`2`
[1] "Minerva McGonagall" "Harry Potter" "Severus Snape" "Hedwig"
[5] "Moaning Myrtle"
$`3`
[1] "Fred Weasley" "Molly Weasley" "Percy Weasley"
+ ... omitted several groups/vertices
HP_network %>%
infomap.community() %>%
membership()
Regulus Arcturus Black Sirius Black Lavender Brown
13 7 12
Cho Chang Vincent Crabbe Sr. Vincent Crabbe
12 13 11
Bartemius "Barty" Crouch Sr. Bartemius "Barty" Crouch Jr. Fleur Delacour
13 1 4
Cedric Diggory Alberforth Dumbledore Albus Dumbledore
12 2 2
Dudley Dursley Petunia Dursley Vernon Dursley
10 10 10
Argus Filch Seamus Finnigan Nicolas Flamel
3 12 2
Cornelius Fudge Goyle Sr. Gregory Goyle
19 11 11
Hermione Granger Rubeus Hagrid Igor Karkaroff
3 8 21
Viktor Krum Bellatrix Lestrange Alice Longbottom
21 18 20
Frank Longbottom Neville Longbottom Luna Lovegood
20 1 1
Xenophilius Lovegood Remus Lupin Draco Malfoy
1 14 15
Lucius Malfoy Narcissa Malfoy Olympe Maxime
15 18 4
Minerva McGonagall Alastor "Mad-Eye" Moody Peter Pettigrew
8 5 16
Harry Potter James Potter Lily Potter
5 14 2
Quirinus Quirrell Tom Riddle Sr. Mary Riddle
2 17 17
Lord Voldemort Rita Skeeter Severus Snape
16 22 3
Nymphadora Tonks Dolores Janes Umbridge Arthur Weasley
14 19 7
Bill Weasley Charlie Weasley Fred Weasley
9 4 9
George Weasley Ginny Weasley Molly Weasley
6 4 2
Percy Weasley Ron Weasley Dobby
6 1 1
Fluffy Hedwig Moaning Myrtle
8 5 5
Aragog Grawp
1 3
HP_nodes <- HP_nodes %>%
mutate(group = membership(infomap.community(HP_network)))
HP_nodes %>%
datatable()
NA
visNetwork(HP_nodes,
HP_links,
main = "Network of Harry Potter Characters") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T, selectedBy = "group")
NA