With reference to the organisation network graph in Section 6.1 of Hands-on Exercise 10, you are required to complete the following tasks:
Loading the 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)
}
Prepare the tbl_graph object:
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)
Original code:
g_original <- 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_original + theme_graph()
set_graph_style()
ggraph(GAStech_graph,layout = "nicely") +
geom_edge_link() +
geom_node_point(aes(colour = centrality_closeness(), size=centrality_betweenness()))
Improved iteration 1:
ggraph(GAStech_graph, layout = "nicely") +
geom_edge_link(colour="gray") +
geom_node_point(aes(colour = centrality_closeness(), size=centrality_betweenness()))+
geom_node_text(aes(label = label, color = centrality_closeness(), size=centrality_betweenness()), repel = TRUE)+
ggtitle("Network Metrics Analysis - Iteration 1")
Improved iteration 2:
ggraph(GAStech_graph, layout = "nicely") +
geom_edge_link(color="gray") +
geom_node_point(aes(colour = centrality_closeness(), size=centrality_betweenness()))+
geom_node_text(aes(label = ifelse(centrality_eigen() >= 0.3, label, NA), color = centrality_closeness(), size=centrality_betweenness()), repel = TRUE) +
ggtitle("Network Metrics Analysis - Iteration 2")
Improved iteration 3 (Final, best design):
ggraph(GAStech_graph, layout = "nicely") +
geom_edge_link(colour = "gray") +
geom_node_circle(aes(colour = centrality_eigen(), fill = centrality_closeness(),r = log(centrality_betweenness())/40), size = 1) +
scale_color_gradient(low = "pink", high = "red")+
scale_fill_gradient(low = "white", high = "blue")+
geom_node_text(aes(label =label), colour = "black",size = 3) +
ggtitle("Network Metrics Analysis - Final")+
labs(caption = "The size of the node indicates the betweenness_centrality")
With reference to the organisation network graph in Section 7.4 of Hands-on Exercise 10, you are required to complete the following tasks:
Prepare the edges of the tbl_graph object:
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 %>%
mutate(group = Department)
Original graph:
visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = list(enabled= TRUE, labelOnly = FALSE), nodesIdSelection = TRUE) %>%
visNodes(labelHighlightBold = TRUE, font = list(size=40))
Improved graph:
GAStech_nodes$title <- paste0("<p>Name:<br>",GAStech_nodes$label,"<p>Title:<br>",GAStech_nodes$Title,"<p>Department:<br>",GAStech_nodes$Department)
visNetwork(GAStech_nodes, GAStech_edges_aggregated, main="Interactive Organisation Graph") %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = list(enabled = TRUE,labelOnly= FALSE, degree = 1, hover = TRUE), nodesIdSelection = TRUE, selectedBy = "Department") %>%
visLegend(zoom=FALSE) %>%
visInteraction(navigationButtons = TRUE, tooltipDelay = 120) %>%
visNodes(shape="ellipse",labelHighlightBold = TRUE, font = list(size=40))
GGPLOT COLORS BEST TRICKS YOU WILL LOVE https://www.datanovia.com/en/blog/ggplot-colors-best-tricks-you-will-love/
visNetwork, an R package for interactive network visualization https://datastorm-open.github.io/visNetwork/nodes.html
R Markdown: The Definitive Guide https://bookdown.org/yihui/rmarkdown/