In-class_Ex05

Import the package and data

Show the code
pacman::p_load(igraph, tidygraph, ggraph, 
               visNetwork, lubridate, clock,
               tidyverse, graphlayouts,jsonlite)

Import json type data

Show the code
MC1 <- fromJSON("data/MC1.json")

Extract data frame from MC1 list as tibble table

Use select to pick columns and reorganize the sequence of data frame

Show the code
MC1_noeds <- as_tibble(MC1$nodes) %>% 
  select(id,type,country)
Show the code
MC1_edges <- as_tibble(MC1$links) %>% 
  select(source,target,type,weight,key)

Import other csv data

Show the code
GAStech_nodes <- read_csv("data/GAStech_email_node.csv")
GAStech_edges <- read_csv("data/GAStech_email_edge-v2.csv")
Show the code
head(GAStech_edges)
# A tibble: 6 × 8
  source target SentDate SentTime Subject    MainSubject sourceLabel targetLabel
   <dbl>  <dbl> <chr>    <time>   <chr>      <chr>       <chr>       <chr>      
1     43     41 6/1/2014 08:39    GT-Seismi… Work relat… Sven.Flecha Isak.Baza  
2     43     40 6/1/2014 08:39    GT-Seismi… Work relat… Sven.Flecha Lucas.Alca…
3     44     51 6/1/2014 08:58    Inspectio… Work relat… Kanon.Herr… Felix.Resu…
4     44     52 6/1/2014 08:58    Inspectio… Work relat… Kanon.Herr… Hideki.Coc…
5     44     53 6/1/2014 08:58    Inspectio… Work relat… Kanon.Herr… Inga.Ferro 
6     44     45 6/1/2014 08:58    Inspectio… Work relat… Kanon.Herr… Varja.Lagos

Wrangle data type

Show the code
GAStech_edges <- GAStech_edges %>% 
  mutate(SendDate = dmy(SentDate)) %>% 
  mutate(Weekday = wday(SentDate,
                        label = TRUE,
                        abbr = FALSE))
GAStech_edges
# A tibble: 9,063 × 10
   source target SentDate SentTime Subject   MainSubject sourceLabel targetLabel
    <dbl>  <dbl> <chr>    <time>   <chr>     <chr>       <chr>       <chr>      
 1     43     41 6/1/2014 08:39    GT-Seism… Work relat… Sven.Flecha Isak.Baza  
 2     43     40 6/1/2014 08:39    GT-Seism… Work relat… Sven.Flecha Lucas.Alca…
 3     44     51 6/1/2014 08:58    Inspecti… Work relat… Kanon.Herr… Felix.Resu…
 4     44     52 6/1/2014 08:58    Inspecti… Work relat… Kanon.Herr… Hideki.Coc…
 5     44     53 6/1/2014 08:58    Inspecti… Work relat… Kanon.Herr… Inga.Ferro 
 6     44     45 6/1/2014 08:58    Inspecti… Work relat… Kanon.Herr… Varja.Lagos
 7     44     44 6/1/2014 08:58    Inspecti… Work relat… Kanon.Herr… Kanon.Herr…
 8     44     46 6/1/2014 08:58    Inspecti… Work relat… Kanon.Herr… Stenig.Fus…
 9     44     48 6/1/2014 08:58    Inspecti… Work relat… Kanon.Herr… Hennie.Osv…
10     44     49 6/1/2014 08:58    Inspecti… Work relat… Kanon.Herr… Isia.Vann  
# ℹ 9,053 more rows
# ℹ 2 more variables: SendDate <date>, Weekday <ord>

Sum up by weekday

Show the code
GAStech_edges_aggregated <- GAStech_edges %>%
  filter(MainSubject == "Work related") %>%
  group_by(source, target, Weekday) %>%
    summarise(Weight = n(),.groups="drop") %>%
  filter(source!=target) %>%
  filter(Weight > 1) 

GAStech_edges_aggregated
# A tibble: 1,372 × 4
   source target Weekday   Weight
    <dbl>  <dbl> <ord>      <int>
 1      1      2 Sunday         5
 2      1      2 Monday         2
 3      1      2 Tuesday        3
 4      1      2 Wednesday      4
 5      1      2 Friday         6
 6      1      3 Sunday         5
 7      1      3 Monday         2
 8      1      3 Tuesday        3
 9      1      3 Wednesday      4
10      1      3 Friday         6
# ℹ 1,362 more rows

Build tidygraph data

Show the code
GAStech_graph <- tbl_graph(nodes = GAStech_nodes,
                           edges = GAStech_edges_aggregated,
                           directed = TRUE)
GAStech_graph
# A tbl_graph: 54 nodes and 1372 edges
#
# A directed multigraph with 1 component
#
# A tibble: 54 × 4
     id label               Department     Title                                
  <dbl> <chr>               <chr>          <chr>                                
1     1 Mat.Bramar          Administration Assistant to CEO                     
2     2 Anda.Ribera         Administration Assistant to CFO                     
3     3 Rachel.Pantanal     Administration Assistant to CIO                     
4     4 Linda.Lagos         Administration Assistant to COO                     
5     5 Ruscella.Mies.Haber Administration Assistant to Engineering Group Manag…
6     6 Carla.Forluniau     Administration Assistant to IT Group Manager        
# ℹ 48 more rows
#
# A tibble: 1,372 × 4
   from    to Weekday Weight
  <int> <int> <ord>    <int>
1     1     2 Sunday       5
2     1     2 Monday       2
3     1     2 Tuesday      3
# ℹ 1,369 more rows