::p_load(tidyverse, jsonlite,
pacman tidygraph, ggraph)
In-Class Exercise (Lesson 5)
Load Packages
Import Data
<- fromJSON("data/MC1_graph.json") kg
Inspect Structure
str(kg, max.level = 1)
List of 5
$ directed : logi TRUE
$ multigraph: logi TRUE
$ graph :List of 2
$ nodes :'data.frame': 17412 obs. of 10 variables:
$ links :'data.frame': 37857 obs. of 4 variables:
Extract and inspect
<- as_tibble(kg$nodes)
nodes_tbl <- as_tibble(kg$links) edges_tbl
Initial EDA
ggplot(data = edges_tbl,
aes(y = `Edge Type`)) +
geom_bar()
ggplot(data = nodes_tbl,
aes(y = `Node Type`)) +
geom_bar()
Mapping from node id to row index
<- tibble(id = nodes_tbl$id,
id_map index = seq_len(
nrow(nodes_tbl)))
Step 2: Map source and target IDs to row indices
<- edges_tbl %>%
edges_tbl left_join(id_map, by = c("source" = "id")) %>%
rename(from = index) %>%
left_join(id_map, by = c("target" = "id")) %>%
rename(to = index)
Step 3: Filter out any unmatched (invalid) edges
<- edges_tbl %>%
edges_tbl filter(!is.na(from), !is.na(to))
Step 4: Creating the graph
visualing the knowledge graph
<- tbl_graph(nodes = nodes_tbl,
graph edges = edges_tbl,
directed = kg$directed)
class(graph)
[1] "tbl_graph" "igraph"
set.seed(1234)
Visualizing the whole Graph
ggraph(graph, layout = "fr") +
geom_edge_link(alpha = 0.3,
colour = "gray") +
geom_node_point(aes(color = `Node Type`),
size = 4) +
geom_node_text(aes(label = name),
repel = TRUE,
size = 2.5) +
theme_void()
Warning: ggrepel: 17411 unlabeled data points (too many overlaps). Consider
increasing max.overlaps
Step 1: Filter edges to only “Memberof”
<- graph %>%
graph_memberof activate(edges) %>%
filter(`Edge Type` == "MemberOf")
Step 2: Extract only connected nodes
<- graph_memberof %>%
used_node_indices activate(edges) %>%
as_tibble() %>%
select(from, to) %>%
unlist() %>%
unique()
Step 3: Keep only those nodes
<- graph_memberof %>%
used_node_indices activate(edges) %>%
as_tibble() %>%
select(from, to) %>%
unlist() %>%
unique()
Plot the sub-graph
ggraph(graph_memberof,
layout = "fr") +
geom_edge_link(alpha = 0.5,
colour = "gray") +
geom_node_point(aes(color = `Node Type`),
size = 1) +
geom_node_text(aes(label = name),
repel = TRUE,
size = 2.5) +
theme_void()
Warning: ggrepel: 17411 unlabeled data points (too many overlaps). Consider
increasing max.overlaps