Dataviz makeover 2
Pre-Requisite
Disable message and warning. Set all of plots width to 10
Installing packages are essential before moving on to the subsequent tasks
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)
}Import network data. Ensure the file path is correct
Task 1: Static Organisation Graph
Data Wringling from Hands-on Exercise 10
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()Creating Network Objects from Hands-on Exercise 10
GAStech_graph <-
tbl_graph(nodes = GAStech_nodes, edges = GAStech_edges_aggregated, directed = TRUE)
GAStech_graph %>%
activate(edges) %>%
arrange(desc(Weight))With reference to the organisation network graph in Section 6.1 of Hands-on Exercise 10, you are required to complete the following tasks:
1.1 Improve the code chunk used to create the organisation network graph by using the latest functions provided in ggraph2.0.
gggraph 2.0 releases a new feature called qgraph to create a quick Organisation Network Chart for exploratory purposes. It serves a similar purpose to the qmap to produce a quick map
Reference: https://cran.rstudio.com/web/packages/ggraph/news/news.html
g <-GAStech_graph %>%
mutate(betweenness_centrality = centrality_betweenness()) %>%
mutate(closeness_centrality = centrality_closeness()) %>%
#Modified code
qgraph(node_size = betweenness_centrality, node_colour = closeness_centrality)
g1.2 Identify three aspects of the graph visualisation in Section 6.1 that can be improved. - Lack of labelling in general poses a difficulty for users to interpret any meaningful information. The improvised chart will include the labels such as title, text for each node -
1.4 Using appropriate ggraph functions, plot the alternative design. “#BDBDBD”
g <- GAStech_graph %>%
mutate(betweenness_centrality = centrality_betweenness()) %>%
mutate(closeness_centrality = centrality_closeness()) %>%
ggraph(layout = "nicely") +
geom_edge_fan(aes(width=Weight), color="#BDBDBD", alpha=0.25) +
geom_node_point(aes(colour = Department, size=betweenness_centrality)) +
geom_node_text(aes(label = Title), size=3, color="black", repel=T) +
ggtitle("Centrality indices Chart")
g +theme_graph() Task 2: Interactive Organisation Graph
Data Preparation from Hands-on Exercise 10
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()Based on Hands-on exercise 10, the visNetwork() looks for a field called “group”. Rename Department column to group
2.1 Improve the design of the graph by incorporating the following interactivity:
When a name is selected from the drop-down list, the corresponding node will not only be highlighted but also will be labelled. Furthermore, all the linked nodes of the selected node will also be labelled too.
When a node of the interactive graph is selected, the node will not only be highlighted but also will be labelled. Furthermore, all the linked nodes of the selected node will be labelled as well.
In the code chunk below, VisNodes is used to incorporate the font size and scaling. With regards to scaling, there are 2 proterties:
Threshold - When zooming out, the font will be drawn smaller. It defines the minimum limit
maxVisible - When zooming in, the font will be drawn larger. It defines the maximum limit
visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visNodes(font = list(size=30),
scaling = list(label=list(Threshold=30, maxVisible=60))) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = list(enabled = T, degree = 0), nodesIdSelection = TRUE) visNetwork(GAStech_nodes, GAStech_edges_aggregated) %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = list(enabled = T, degree = 1, hover = T),
selectedBy = "group", nodesIdSelection = TRUE) %>%
visNodes(labelHighlightBold = TRUE, shape = "box", shadow = list(enabled = TRUE, size = 50),
font = list(size=30), scaling = list(label=list(Threshold=30, maxVisible=60)))