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
Because of the newly implemented functions given in ggraph2.0, Tidygraph is now part of the ggraph, hence we do not need to create new variables for centrality closness and centrality between fuctions. Thus we can just include the functions in the asthetic parememters within geom_node_point() function. This helps to reduce the number of lines written therefore, less trouble.
ggraph(GAStech_graph, layout="nicely") +
geom_edge_link() +
geom_node_point(aes(size=centrality_betweenness(),colour=centrality_closeness()))
ggraph(GAStech_graph, layout="nicely") +
geom_edge_link(colour="darkgrey") +
geom_node_point(aes(size=centrality_betweenness(),colour=centrality_closeness())) +
scale_colour_gradient(low="yellow",high="blue") +
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())) +
scale_colour_gradient(low="yellow",high="blue") +
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)
GAStech_nodes <- GAStech_nodes %>%
mutate(font.size = 55)
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 = 55)
p <- visNetwork(GAStech_nodes, GAStech_edges_aggregated, height = "700px", width = "100%") %>%
visLegend(position = "right", main = "Department", zoom = FALSE) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(selectedBy = list(variable = "group"), highlightNearest = list(enabled= TRUE, labelOnly = FALSE), nodesIdSelection = list(enabled = TRUE))
visInteraction(p,dragNodes = TRUE,navigationButtons = T)