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)                    
## 
## 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)                
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
  1. Read the data into links and nodes, and create the network in igraph with graph_from_data_frame().
  2. Find the density of the network.
  3. Make a histogram of the distances in the network.
  4. Find the legnth of the diameter of the network.
  5. Mutate the following into the nodes data: label, title, degree, and set value = degree.
  6. Mutate betweenness into the links data, and set it to value.
  7. Create a diagram with visNetwork, with a title and options set for a menu and highlightNearest.
  8. Find the communities in the network, mutate a group column in the nodes data, and create another diagram displaying the colored groups.

  9. Read the data into links and nodes

got_nodes <- read_csv("got_nodes.csv")
## Parsed with column specification:
## cols(
##   id = col_character(),
##   name = col_character()
## )
got_links <- read_csv("got_links.csv")
## Parsed with column specification:
## cols(
##   from = col_character(),
##   to = col_character()
## )

Greate network:

got_network <- graph_from_data_frame(got_links, 
                                           vertices = got_nodes, 
                                           directed = F)
  1. Find network density:
got_network %>% 
  edge_density()
## [1] 0.0545183
  1. A histogram of the distances in the network:
got_network %>% 
  distances() %>% 
  as.vector() %>%              
  as_tibble() %>%              
  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.
  1. Network diameter:
got_network %>% 
  get_diameter() %>% 
  length()
## [1] 7
  1. Mutate the following into the nodes data: label, title, degree, and set value = degree.
got_nodes <- got_nodes %>% 
  mutate(label = name) %>% 
  mutate(title = name) %>% 
  mutate(degree = degree(got_network)) %>% 
  mutate(value = degree)

got_nodes
  1. Mutate betweenness into the links data, and set it to value.
got_links <- got_links %>% 
  mutate(betweenness = edge_betweenness(got_network)) %>% 
  mutate(value = betweenness) 

got_links 
  1. Diagram:
visNetwork(got_nodes, 
           got_links, 
           main = "Game of thrones network analysis") %>% 
  visIgraphLayout(layout = "layout_nicely") %>% 
  visOptions(highlightNearest = T, nodesIdSelection = T)
  1. Find communities:
got_nodes <- got_nodes %>% 
  mutate(group = membership(infomap.community(got_network)))

Network diagram wiht communities:

visNetwork(got_nodes, 
           got_links, 
           main = "Game of thrones network analysis") %>% 
  visIgraphLayout(layout = "layout_nicely") %>% 
  visOptions(highlightNearest = T, nodesIdSelection = T, selectedBy = "group")