Disable message and warning. Set plots width to 9.
## Global options
knitr::opts_chunk$set(
message=FALSE,
warning=FALSE,
fig.width=9)
Install packages
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)
}
Import network data. Ensure the file path is correct
GAStech_nodes <- read_csv("data/GAStech_email_node.csv")
GAStech_edges <- read_csv("data/GAStech_email_edge-v2.csv")
Data Wringling from Hands-on Exercise 10
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()
Create Network Objects from Hands-on Exercise 10
GAStech_graph <-
tbl_graph(nodes = GAStech_nodes, edges = GAStech_edges_aggregated, directed = TRUE)
GAStech_graph %>%
activate(edges) %>%
arrange(desc(Weight))
g <-GAStech_graph %>%
mutate(betweenness_centrality = centrality_betweenness()) %>%
mutate(closeness_centrality = centrality_closeness()) %>%
#Modified code
qgraph(node_size = betweenness_centrality, node_colour = closeness_centrality)
g
GAStech_graph<- GAStech_graph %>%
mutate(betweenness_centrality = centrality_betweenness()) %>%
mutate(closeness_centrality = centrality_closeness())
g <- ggraph(GAStech_graph, layout="nicely") +
geom_edge_link0(aes(edge_width=Weight), color="grey66", alpha=0.3) +
geom_node_point(aes(colour = closeness_centrality, size=betweenness_centrality)) +
scale_edge_width_continuous(range = c(0.2,0.9))+
scale_size_continuous(range=c(2,8)) +
scale_color_gradient(low="#ff0000", high="#ffeb3b") +
theme_graph() + theme(legend.position = "left")
g + geom_node_label(aes(filter=closeness_centrality > 0.015, label= label), repel = TRUE)+
ggtitle("Centrality indices Chart")
The overall network graph does provide critical information regarding the key employees and its interaction with other employees. However, it does not display which department Mat, Birgitta, Hideki and Ruscella often interact with and the frequency of interaction as indicated by the weight of the edges. The thicker the edges, the higher the frequency of interaction.
The graph below is categorised/faceted by Department.
g + facet_nodes(~Department) +
geom_node_label(aes(filter=closeness_centrality > 0.010, label= label),
repel = TRUE) +
ggtitle("Centrality indices Chart by Department")
Data Preparation from Hands-on Exercise 10
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_edges_aggregated
## # A tibble: 839 x 3
## from to weight
## <dbl> <dbl> <int>
## 1 1 2 21
## 2 1 3 21
## 3 1 4 21
## 4 1 5 21
## 5 1 6 21
## 6 1 7 21
## 7 1 8 15
## 8 1 9 15
## 9 1 10 15
## 10 1 11 15
## # … with 829 more rows
Based on Hands-on exercise 10, the visNetwork() looks for a field called “group”. Rename Department column to group
GAStech_nodes <- GAStech_nodes %>%
rename(group = Department)
GAStech_nodes
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.
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.
In the code chunk below, VisNodes is used to incorporate the font size and scaling. With regards to scaling, there are 2 proterties:
Threshold - When zooming out, the font will be drawn smaller. It defines the minimum limit
maxVisible - When zooming in, the font will be drawn larger. It defines the maximum limit
visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visNodes(font = list(size=30),
scaling = list(label=list(Threshold=30, maxVisible=60))) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = list(enabled = T, degree = 0), nodesIdSelection = TRUE)
visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = list(enabled = T, degree = 1, hover = T,
algorithm ="hierarchical"),
selectedBy = list(variable= "group", main="Department"),
nodesIdSelection = list(main="Employee Name")) %>%
visNodes(labelHighlightBold = TRUE, shape = "box", shadow = list(enabled = TRUE, size = 50),
font = list(size=30), scaling = list(label=list(Threshold=30, maxVisible=60))) %>%
visLegend(position="left", zoom=FALSE) %>%
visEdges(arrows = "to", color = list(highlight="#424242"))