Load packages

library(tidyverse)
## ── Attaching packages ────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0       ✔ purrr   0.3.0  
## ✔ tibble  2.0.1       ✔ dplyr   0.8.0.1
## ✔ tidyr   0.8.2       ✔ stringr 1.4.0  
## ✔ readr   1.3.1       ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(igraph)                    # This is the package to analyze the network
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:dplyr':
## 
##     as_data_frame, groups, union
## The following objects are masked from 'package:purrr':
## 
##     compose, simplify
## The following object is masked from 'package:tidyr':
## 
##     crossing
## The following object is masked from 'package:tibble':
## 
##     as_data_frame
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
library(visNetwork)                # Creates visualizations of the network
library(DT)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:igraph':
## 
##     groups
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Read HP data

HP_links <- read.csv("HP_links.csv")
HP_nodes <- read.csv("HP_nodes.csv")

Create network

HP_network <- graph_from_data_frame(HP_links,
                                   vertices = HP_nodes,
                                   directed = F)

Find network density

HP_network %>%
  edge_density()
## [1] 0.1711538

Create a histogram of the distances of 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()
## Warning: Calling `as_tibble()` on a vector is discouraged, because the behavior is likely to change in the future. Use `enframe(name = NULL)` instead.
## This warning is displayed once per session.

Find the diameter of the network.

HP_network %>% 
  get_diameter() %>% 
  length()
## [1] 7

Mutate the following into the nodes data: label, title, degree, and set value = degree.

HP_nodes <- HP_nodes %>%
  mutate(label = name) %>%
  mutate(title = name) %>%
  mutate(degree = degree(HP_network)) %>%
  mutate(value = degree)

Find edge_betweeness for the HP_network

HP_links <- HP_links %>%
  mutate(betweeness = edge_betweenness(HP_network)) %>%
  mutate(value = betweeness)

Create a diagram

visNetwork(HP_nodes,
           HP_links,
           main = "Harry Potter Diagram") %>%
  visIgraphLayout(layout = "layout_nicely") %>%
  visOptions(highlightNearest = T, nodesIdSelection = T)

Find communities and create a diagram

HP_nodes <- HP_nodes %>%
  mutate(group = membership(infomap.community(HP_network)))

Network diagram with communities

visNetwork(HP_nodes,
           HP_links,
           main = "Harry Potter Diagram Featuring Communities") %>%
  visIgraphLayout(layout = "layout_nicely") %>%
  visOptions(highlightNearest = T, nodesIdSelection = T, selectedBy = "group")