Word Network

library("navdata")
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.6     ✔ dplyr   1.0.8
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(tidygraph)
## 
## Attaching package: 'tidygraph'
## The following object is masked from 'package:stats':
## 
##     filter
library(ggraph)

data("phone.call2")
head(phone.call2)
## $nodes
## # A tibble: 16 × 2
##       id label         
##    <int> <chr>         
##  1     1 France        
##  2     2 Belgium       
##  3     3 Germany       
##  4     4 Danemark      
##  5     5 Croatia       
##  6     6 Slovenia      
##  7     7 Hungary       
##  8     8 Spain         
##  9     9 Italy         
## 10    10 Netherlands   
## 11    11 UK            
## 12    12 Austria       
## 13    13 Poland        
## 14    14 Switzerland   
## 15    15 Czech republic
## 16    16 Slovania      
## 
## $edges
## # A tibble: 18 × 3
##     from    to weight
##    <int> <int>  <dbl>
##  1     1     3    9  
##  2     2     1    4  
##  3     1     8    3  
##  4     1     9    4  
##  5     1    10    2  
##  6     1    11    3  
##  7     3    12    2  
##  8     3    13    2  
##  9     2     3    3  
## 10     3    14    2  
## 11     3    15    2  
## 12     3    10    2  
## 13     4     3    2  
## 14     5     3    2  
## 15     5    16    2  
## 16     5     7    2  
## 17     6     3    2  
## 18     7    16    2.5
phone.net <- tbl_graph(
  nodes = phone.call2$nodes, 
  edges = phone.call2$edges,
  directed = TRUE
)

head(phone.net)
## 6 x 16 sparse Matrix of class "dgCMatrix"
##                                     
## [1,] . . 9 . . . . 3 4 2 3 . . . . .
## [2,] 4 . 3 . . . . . . . . . . . . .
## [3,] . . . . . . . . . 2 . 2 2 2 2 .
## [4,] . . 2 . . . . . . . . . . . . .
## [5,] . . 2 . . . 2 . . . . . . . . 2
## [6,] . . 2 . . . . . . . . . . . . .
set.seed(123)
phone.net %>%
  activate(nodes) %>%
  mutate(centrality = centrality_authority()) %>% 
  ggraph(layout = "graphopt") + 
  geom_edge_link(width = 1, colour = "lightgray") +
  geom_node_point(aes(size = centrality, colour = centrality)) +
  geom_node_text(aes(label = label), repel = TRUE)+
  scale_color_gradient(low = "yellow", high = "red")+
  theme_graph()