Install relevant libraries needed

packages = c('igraph', 'tidygraph', 'ggraph', 'visNetwork', 'lubridate', 'tidyverse')

for(p in packages){library
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p, character.only = T)
}

Check if library is installed

p <- c('igraph', 'tidygraph', 'ggraph', 'visNetwork', 'lubridate', 'tidyverse')
lapply(p, require, character.only = TRUE)
## [[1]]
## [1] TRUE
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] TRUE
## 
## [[5]]
## [1] TRUE
## 
## [[6]]
## [1] TRUE

Importing network data from files

GAStech_nodes <- read_csv("data/GAStech_email_node.csv")
GAStech_edges <- read_csv("data/GAStech_email_edge-v2.csv")

Data Wrangling

GAStech_edges$SentDate  = dmy(GAStech_edges$SentDate)
GAStech_edges$Weekday = wday(GAStech_edges$SentDate, label = TRUE, abbr = FALSE)

GAStech_edges_aggregated <- GAStech_edges %>%
  filter(MainSubject == "Work related") %>%
  group_by(source, target, Weekday) %>%
    summarise(Weight = n()) %>%
  filter(source!=target) %>%
  filter(Weight > 1) %>%
  ungroup()

GAStech_graph <- tbl_graph(nodes = GAStech_nodes, edges = GAStech_edges_aggregated, directed = TRUE)

GAStech_graph %>%
  activate(edges) %>%
  arrange(desc(Weight))
## # A tbl_graph: 54 nodes and 1456 edges
## #
## # A directed multigraph with 1 component
## #
## # Edge Data: 1,456 x 4 (active)
##    from    to Weekday Weight
##   <int> <int> <ord>    <int>
## 1    40    41 Tuesday     23
## 2    40    43 Tuesday     19
## 3    41    43 Tuesday     15
## 4    41    40 Tuesday     14
## 5    42    41 Tuesday     13
## 6    42    40 Tuesday     12
## # ... with 1,450 more rows
## #
## # Node Data: 54 x 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
## # ... with 51 more rows

Task 1 Original Plot

g <- GAStech_graph %>%
  mutate(betweenness_centrality = centrality_betweenness()) %>%
  mutate(closeness_centrality = centrality_closeness()) %>%
  ggraph(layout = "nicely") + 
  geom_edge_link(aes()) +
  geom_node_point(aes(colour = closeness_centrality, size=betweenness_centrality))

g + theme_graph()

Task 1.1 Improve the code chunk used to create the organisation network graph by using the latest functions provided in ggraph2.0.

Due to the implementation of Tidygraph library into ggraph, functions can be used under aesthetic as parameters without having to create new columns by the ‘mutate’ function. Thus, the code can be quickly neaten by adding ‘centrality_closeness’ and ‘centrality_betweenness’ straight under the aesthetic function.

g <- GAStech_graph %>%
  ggraph(layout = "nicely") + 
  geom_edge_link(aes()) +
  geom_node_point(aes(colour = centrality_closeness(), size=centrality_betweenness()))

g + theme_graph()

Task 1.2 Identify three aspects of the graph visualisation in Section 6.1 that can be improved.

Aspect 1: Labels of nodes

Aspect 2: Overlapping of nodes links

Aspect 3: Colour density of the centrality_closeness

Task 1.3 Provide the sketch of your alternative design.

Task 1.4 Using appropriate ggraph functions, plot the alternative design.

g <- ggraph(GAStech_graph, layout = "kk")+
  geom_edge_link(colour="grey") +
  geom_node_point(aes(colour = centrality_closeness(), size=centrality_betweenness())) +
  geom_node_text(aes(label=label), repel=TRUE, size =2.5,colour="black") +
  scale_color_viridis(direction = -1) 

g

## Building Interactive Network Graph with visNetwork

Data Preparation

GAStech_edges_aggregated <- GAStech_edges %>%
  left_join(GAStech_nodes, by = c("sourceLabel" = "label")) %>%
  rename(from = id) %>%
  left_join(GAStech_nodes, by = c("targetLabel" = "label")) %>%
  rename(to = id) %>%
  filter(MainSubject == "Work related") %>%
  group_by(from, to) %>%
    summarise(weight = n()) %>%
  filter(from!=to) %>%
  filter(weight > 1) %>%
  ungroup()

GAStech_nodes <- GAStech_nodes %>%
  rename(group = Department)

Task 2 Original Plot

visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
  visIgraphLayout(layout = "layout_with_fr") %>%
  visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)

Task 2.1 Improve the design of the graph

  1. When a name is selected from the drop-down list, the corresponding node will not only be highlighted but also will be labelled. Furthermore, all the linked nodes of the selected node will also be labelled too.
  2. When a node of the interactive graph is selected, the node will not only be highlighted but also will be labelled. Furthermore, all the linked nodes of the selected node will be labelled as well.
GAStech_nodes_labels <- GAStech_nodes %>%
  mutate(font.size = 36)

visNetwork(GAStech_nodes_labels, GAStech_edges_aggregated) %>%
  visIgraphLayout(layout = "layout_with_fr") %>%
  visOptions(highlightNearest = list(enabled= TRUE, labelOnly = FALSE), nodesIdSelection = TRUE)

Task 2.2 Identify three aspects of the graph visualisation in Section 7.4 that can be improved.

Aspect 1: Lacking of legends

Aspect 2: Interaction between nodes

Aspect 3: Position of nodes

Task 2.3 Provide the sketch of your alternative design.

Task 2.4 Using appropriate visNetwork functions, plot the alternative design.

GAStech_nodes_labels <- GAStech_nodes %>%
  mutate(font.size = 36)

visNetwork(GAStech_nodes_labels, GAStech_edges_aggregated, height='420px', width='100%') %>%
  visIgraphLayout(layout = "layout_in_circle") %>%
  visOptions(selectedBy = 'group',
    highlightNearest = list(enabled= TRUE, labelOnly = FALSE), nodesIdSelection = TRUE) %>% 
  visEdges(arrows = list(to = list(enabled = TRUE), from = list(enabled = TRUE)), selectionWidth = 6) %>%
  visLegend(width = 0.2, position = "right", main = "Department", zoom=FALSE) %>% 
  visPhysics(stabilization = FALSE) %>% 
  visInteraction(hideEdgesOnDrag = TRUE)