Importing Packages for DataViz Makeover 2
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)
}Importing Data
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
#Inital Sketch Graph
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()gg2 <- ggraph(GAStech_edges_aggregated, layout = "nicely") +
geom_edge_link() +
geom_node_point(aes(colour = centrality_closeness(),size = centrality_betweenness()))
gg2 + theme_graph()Provide the sketch of your alternative design.
Using appropriate ggraph functions, plot the alternative design.
retrievedGraph <- GAStech_graph %>%
mutate(betweenness_centrality = centrality_betweenness()) %>%
mutate(closeness_centrality = centrality_closeness())
ggraph(retrievedGraph) +
geom_edge_link(colour = "Grey70") +
geom_node_circle(aes(fill = closeness_centrality,
col = Department,
r = log(betweenness_centrality)/50),
size = 2) +
geom_node_text(aes(label = Title), colour = "Black", size = 2, repel = TRUE)#Importing data from files
GAStech_nodes <- read_csv("data/GAStech_email_node.csv")
GAStech_edges <- read_csv("data/GAStech_email_edge-v2.csv")
#Wrangling Time
GAStech_edges$SentDate = dmy(GAStech_edges$SentDate)
GAStech_edges$Weekday = wday(GAStech_edges$SentDate, label = TRUE, abbr = FALSE)
#Data Preparation
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)
#Working with Layout
visNetwork(GAStech_nodes, GAStech_edges_aggregated, height = "650px", width = "100%") %>%
visIgraphLayout(layout = "layout_with_fr") %>%
#Task 2
visOptions(
highlightNearest = list(
degree = list(from = 1, to = 1),
enabled = TRUE
),
nodesIdSelection = TRUE
)Provide the sketch of your alternative design.
Using appropriate visNetwork functions, plot the alternative design.
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 %>%
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_edges_aggregated <- rename(GAStech_edges_aggregated, value = weight)
GAStech_nodes <- GAStech_nodes %>%
rename(group = Department)
GAStech_nodes$title <- paste0("<p>Job Title: ", GAStech_nodes$Title, "</p>")
GAStech_edges_aggregated$label <- GAStech_edges_aggregated$value
visNetwork(GAStech_nodes, GAStech_edges_aggregated, main = "Interactive Network Graph") %>%
visIgraphLayout(layout = "layout_with_fr") %>%
visEdges(
arrows = list(
to = list(enabled = TRUE),
from = list(enabled = TRUE)
)
) %>%
visPhysics(stabilization = FALSE) %>%
visInteraction(
dragNodes = TRUE,
dragView = TRUE,
zoomView = FALSE) %>%
visNodes(shape="box",
labelHighlightBold = TRUE,
font = list(size=20)) %>%
visOptions(
selectedBy = "group",
highlightNearest = list(
degree = list(enabled = T, degree = list(from = 1, to = 1), hover = F),
enabled = TRUE
),
nodesIdSelection = list(enabled = TRUE)
)