The following chunk reads the data into the program
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.
This next chunk then creates the network in igraph
HP_network <- graph_from_data_frame(HP_links,
vertices = HP_nodes,
directed = F)
The following chunk determines the density of the network which divides the number of connections by the total possible connections.
HP_network %>%
edge_density()
[1] 0.1711538
This chunk then converts the data into a histogram displaying the distances in the network
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()
This chunk then gives the diameter of the network which is the longest of the distances.
HP_network %>%
get_diameter() %>%
length()
[1] 7
This then mutates and adds a label, title, and degree set to value.
HP_nodes <- HP_nodes %>%
mutate(label = name) %>%
mutate(title = name) %>%
mutate(value = degree(HP_network))
To mutate further, this adds a column for betweenness set to value.
HP_links %>%
mutate(betweenness = edge_betweenness(HP_network)) %>%
mutate(value = betweenness)
Finally, the diagram is created with a title and set to highlight the nearest connection to whichever character the dot represents.It also gives a menu option to find a certain character.
visNetwork(HP_nodes,
HP_links,
main = "Network of HP characters") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T)
This next chunk finds the communities within the network which also means the characters that are most related and connected to one another.
HP_network %>%
infomap.community()
IGRAPH clustering infomap, groups: 24, mod: 0.17
+ groups:
$`1`
[1] "Regulus Arcturus Black" "Vincent Crabbe Sr." "Vincent Crabbe"
$`2`
[1] "Sirius Black" "Albus Dumbledore" "Nicolas Flamel" "Olympe Maxime" "Quirinus Quirrell"
$`3`
[1] "Lavender Brown" "Cho Chang" "Cedric Diggory" "Seamus Finnigan"
$`4`
+ ... omitted several groups/vertices
This chunk then puts the data into their group membership
HP_nodes <- HP_nodes %>%
mutate(group = membership(infomap.community(HP_network)))
HP_nodes %>%
datatable()
Finally, this network is complete with not only the above network’s title and highlighting, but also color coded to show which characters belong to each group.
visNetwork(HP_nodes,
HP_links,
main = "Network of HP Characters") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T)