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)
}
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
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))
## # 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
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()
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()
Aspect 1: Labels of nodes
Aspect 2: Overlapping of nodes links
Aspect 3: Colour density of the centrality_closeness
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
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)
visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
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)
Aspect 1: Lacking of legends
Aspect 2: Interaction between nodes
Aspect 3: Position of nodes
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)