In preparation to complete the two tasks, we will first import the library packages and data, and perform necessary data pre-processing tasks.
packages = c('tidygraph', 'ggraph', 'visNetwork', 'lubridate', 'tidyverse')
for(p in packages){library
if(!require(p, character.only = T)){
install.packages(p)
}
library(p, character.only = T)
}
GAStech_nodes <- read_csv("data/GAStech_email_node.csv")
GAStech_edges <- read_csv("data/GAStech_email_edge-v2.csv")
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))
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()
To optimise the code, we will remove the mutate() functions used in Network Graph 6.1. In the latest ggraph 2.0, the tidygraph library has been included in the internals of the lastest version, which means that all the algorithms will be available as inputs to the aesthetic (aes) mappings and the layout specifications. There is no need to create new variables for the centrality_closeness() and centrality_betweenness() functions. Hence we can include both functions directly in the aesthetic mappings inside the geom_node_point() parameters.
Next, the new qgraph() function is a new feature that shows an overview of the network data, although it should not be used for more complicated plots.
The attempt to optimise the code in the above graph is as shown:
qgraph(
GAStech_graph,
node_colour = centrality_closeness(),
node_size = centrality_betweenness()
)
From the initial network graph in 6.1, we can look into the following ways to improve the design:
Restrospective to the aspects that could be improved from Network Graph 6.1, the following sketch shows an alternative design:
Based on the sketch in Task 1c), the following network diagram aims improve the current Graph 6.1:
g2 <- GAStech_graph %>%
mutate(betweenness_centrality = centrality_betweenness()) %>%
mutate(closeness_centrality = centrality_closeness()) %>%
ggraph(layout = "nicely") +
geom_edge_bend(edge_colour = "gray69") +
geom_node_point(aes(colour= closeness_centrality, size=betweenness_centrality)) +
scale_colour_gradient(low = "#ff0015", high = "#00ff37")
g2 + theme_graph()
visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
The improved graph has additional interactive features by including two parameters labelOnly and hover, where both are set to FALSE. The code is as shown:
visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = list(enabled = TRUE,degree = 1, labelOnly = FALSE, hover = FALSE),nodesIdSelection = TRUE)
From the initial organisation graph in 7.4, we can look into the following ways to improve the design:
Restrospective to the aspects that could be improved from Network Graph 6.1, additional features are proposed in the alternative design sketch - (1) The node labels could be labelled in the circle of each node to prevent overlapping from the edges; (2) Option included to view nodes by department (group). The following sketch shows an alternative design:
Based on the sketch in Task 2c), the following organisation graph aims improve the current Graph 7.4:
visNetwork(GAStech_nodes, GAStech_edges_aggregated,main = "GAStech E-mail Correspondence Between Employees") %>%
visNodes(GAStech_nodes, shape = "circle") %>%
visLegend(position = "left", main = "Department") %>%
visEdges(arrows = "to") %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = list(enabled = TRUE,degree = 1, labelOnly = FALSE, hover = FALSE),nodesIdSelection = TRUE,selectedBy = "group") %>%
visInteraction(navigationButtons = T)