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 data
sept11_nodes <- read_csv("Sept11_nodes.csv")
## Parsed with column specification:
## cols(
## id = col_double(),
## name = col_character()
## )
sept11_links <- read_csv("Sept11_links.csv")
## Parsed with column specification:
## cols(
## from = col_double(),
## to = col_double()
## )
Create network
sept11_network <- graph_from_data_frame(sept11_links,
vertices= sept11_nodes,
directed = F)
Find network density
sept11_network %>%
edge_density()
## [1] 0.07985193
Make a histogram of distances
sept11_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.
Network length diameter
sept11_network %>%
get_diameter() %>%
length()
## [1] 6
Add info to nodes
sept11_nodes <- sept11_nodes %>%
mutate(label= name) %>%
mutate(title= name) %>%
mutate(degree= degree(sept11_network)) %>%
mutate(value= degree)
sept11_nodes
Add betweenness to links
sept11_links<- sept11_links %>%
mutate(betweenness= edge_betweenness(sept11_network)) %>%
mutate(value= betweenness)
sept11_links
Create network diagram
visNetwork(sept11_nodes,
sept11_links,
main = "Network of 9/11 Terrorists") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T)
Communities
sept11_nodes <- sept11_nodes %>%
mutate(group= membership(infomap.community(sept11_network)))
sept11_nodes
Network diagram with communities
visNetwork(sept11_nodes,
sept11_links,
main = "Network of 9/11 Terrorists") %>%
visIgraphLayout(layout = "layout_nicely") %>%
visOptions(highlightNearest = T, nodesIdSelection = T, selectedBy = "group")