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
Find the communities in the network, mutate a group column in the nodes data, and create another diagram displaying the colored groups.
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)
got_network %>%
edge_density()
## [1] 0.0545183
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.
got_network %>%
get_diameter() %>%
length()
## [1] 7
got_nodes <- got_nodes %>%
mutate(label = name) %>%
mutate(title = name) %>%
mutate(degree = degree(got_network)) %>%
mutate(value = degree)
got_nodes
got_links <- got_links %>%
mutate(betweenness = edge_betweenness(got_network)) %>%
mutate(value = betweenness)
got_links
visNetwork(got_nodes,
got_links,
main = "Game of thrones network analysis") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T)
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")