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)
}
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)
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()
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font
## family not found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
Based on ggraph 2.0, Tidygraph library has been included in ggprah. This means that all functions/algorithms can be used as parameters in the aesthetic and layout function. Hence, creating new variables for centality closeness and centrality betweeness functions as shown in the inital graph is not neccessary anymore. All we need to do is directly include the functions in the asethetic paramters within geom_node_point() function.
ggraph(GAStech_graph, layout="nicely") +
geom_edge_link() +
geom_node_point(aes(size=centrality_betweenness(),colour=centrality_closeness()))
To complement the pros & and cons of the proposed alternate design. Two network graphs are being proposed. One showing the whole network and the other being facted. The purpose of this to breakdown the network graph so that the links between nodes can be identified and seen clearer.
ggraph(GAStech_graph, layout="kk") +
geom_edge_link(colour="darkgrey") +
geom_node_point(aes(size=centrality_betweenness(),colour=centrality_closeness())) +
scale_colour_gradient(low="red",high="green") +
geom_node_text(aes(label=label), repel=TRUE, size =2.5,colour="black") +
th_foreground(foreground = "grey80", border = TRUE)
ggraph(GAStech_graph, layout="kk") +
geom_edge_link(colour="darkgrey") +
geom_node_point(aes(size=centrality_betweenness(),colour=centrality_closeness())) +
#geom_node_circle(aes(fill = closeness_centralitycol= Department),size=2) +
scale_colour_gradient(low="red",high="green") +
geom_node_text(aes(label=label), repel=TRUE, size =2.5,colour="black") +
facet_nodes(~Department,scale="free") +
th_foreground(foreground = "grey80", border = TRUE)
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)
The nodes and corresponding nodes now being labelled & highlighted when selected in the drop down box or in the interactive network graph. In addition, the linkes of the selected nodes and its corresponding nodes will be highlighted to.
GAStech_nodes <- GAStech_nodes %>%
mutate(font.size = 48)
visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = list(enabled= TRUE, labelOnly = FALSE), nodesIdSelection = TRUE)
GAStech_nodes <- GAStech_nodes %>%
mutate(font.size = 48)
p <- visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visLegend(position = "left", main = "Department") %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(selectedBy = list(variable = "group",
style = 'width: 160px; height: 26px;'),
highlightNearest = list(enabled= TRUE,
labelOnly = FALSE),
nodesIdSelection = list(enabled = TRUE,
style = 'width: 175px; height: 26px;'))
visInteraction(p,dragNodes = TRUE,navigationButtons = T)