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
Game of Throwns- links
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()
## )
Game of Throwns Network– Discover network
got_network <- graph_from_data_frame(got_links,
vertices = got_nodes,
directed = F)
Density of network
got_network %>%
edge_density()
## [1] 0.0545183
Histogram of Game of Throwns Network links
got_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.
Get network diameter
got_network %>%
get_diameter() %>%
length()
## [1] 7
Mutate the following into the nodes data: label, title, degree and set value = degree
got_nodes <- got_nodes %>%
mutate(label = name) %>%
mutate(tite = name) %>%
mutate(degree = degree(got_network)) %>%
mutate(value = degree)
got_nodes
got_links <- got_links %>%
mutate(betweenness = edge_betweenness(got_network)) %>%
mutate(value = betweenness)
Network Link of Game of Thrones. Hover over each dot and segment to understand relationship of characters.
visNetwork(got_nodes, got_links, main = "Network of Game of Thrones") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T)
Find communities
got_nodes <- got_nodes %>%
mutate(group = membership(infomap.community(got_network)))
Node are now color coded by group membership
visNetwork(got_nodes, got_links, main = "Network of Game of Thrones") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T, selectedBy = "group")
```