This vignette demonstrates how to convert Ladybug query results into
various R graph library objects, including igraph,
tidygraph, and g6R. It showcases the seamless
integration of lbugr with popular R packages for graph
analysis and visualization.
igraphThe igraph package is a powerful tool for graph
manipulation and analysis in R. lbugr provides a direct
conversion function as_igraph() to transform Ladybug query
results into igraph objects.
First, let’s set up a Ladybug database and load some sample graph data.
library(lbugr)
library(igraph)
# Create a connection
db_path <- tempfile()
con <- lb_connection(db_path)
# Create schema for nodes and relationships
lb_execute(con, paste("CREATE NODE TABLE Person(name STRING, age INT64,",
"PRIMARY KEY (name))"))
lb_execute(con, "CREATE REL TABLE Knows(FROM Person TO Person, since INT64)")
# Prepare data frames
persons_data <- data.frame(
name = c("Alice", "Bob", "Carol"),
age = c(35, 45, 25)
)
knows_data <- data.frame(
from_person = c("Alice", "Bob"),
to_person = c("Bob", "Carol"),
since = c(2010, 2015)
)
# Load data into Ladybug
lb_copy_from_df(con, persons_data, "Person")
lb_copy_from_df(con, knows_data, "Knows")Now, let’s execute a query that returns graph data and convert it to
an igraph object.
# Query to get all persons and their relationships
graph_query_result <- lb_execute(con, paste("MATCH (p1:Person)-[k:Knows]->",
"(p2:Person) RETURN p1, p2, k"))
# Convert the Ladybug result to an igraph object
igraph_graph <- as_igraph(graph_query_result)
# Print the igraph object summary
print(igraph_graph)
#> IGRAPH UN-- 3 2 --
#> + attr: name (v/c), label (v/c), since (e/n)
#> + edges (vertex names):
#> [1] Alice->Bob Bob ->Carol
V(igraph_graph)$label <- igraph::V(igraph_graph)$name
E(igraph_graph)$label <- "knows"
plot(igraph_graph,
vertex.color = "#dc2626",
vertex.label.color = "#f3f4f6",
vertex.label.font = 2,
edge.color = "#9ca3af",
edge.arrow.size = 0.8,
edge.arrow.width = 0.5,
bg = "#030712",
main = "igraph Graph")You can now perform standard igraph operations on
igraph_graph.
tidygraphThe tidygraph package offers a tidy data approach to
graph manipulation, integrating seamlessly with the tidyverse.
lbugr supports conversion to tidygraph objects
via as_tidygraph().
tidygraphUsing the same Ladybug query result, we can convert it to a
tidygraph object.
# Convert the Ladybug result to a tidygraph object
tidygraph_graph <- as_tidygraph(graph_query_result)
# Print the tidygraph object summary
print(tidygraph_graph)
#> # A tbl_graph: 3 nodes and 2 edges
#> #
#> # A directed acyclic simple graph with 3 nodes and 2 edges
#> #
#> # Node Data: 3 x 2 (active)
#> name age
#> <chr> <dbl>
#> 1 Alice 35
#> 2 Bob 45
#> 3 Carol 25
#> #
#> # Edge Data: 2 x 3
#> from to since
#> <int> <int> <dbl>
#> 1 1 2 2010
#> 2 2 3 2015
ggraph::ggraph(tidygraph_graph, layout = "kk") +
ggraph::geom_edge_link(color = "#9ca3af", arrow = grid::arrow(angle = 30, length = grid::unit(3, "mm")), arrow.fill = "#9ca3af") +
ggraph::geom_node_point(color = "#dc2626", size = 8) +
ggraph::geom_node_text(ggplot2::aes(label = name), color = "#f3f4f6", size = 4, vjust = -1) +
ggplot2::theme_void() +
ggplot2::theme(plot.background = ggplot2::element_rect(fill = "#030712", color = NA))g6RThe g6R package provides an R interface to the G6
JavaScript graph visualization library, enabling rich, interactive
visualizations directly within R environments. Since g6R
has built-in support for igraph objects, you can easily
create interactive visualizations by first converting your Ladybug query
result to an igraph object.
g6R GraphBuilding on the previous examples, we can convert the Ladybug query
result into a g6R object. We can then customize the
appearance of the nodes and edges for a more informative
visualization.
library(g6R)
graph_query_result <- lb_execute(con, paste("MATCH (p1:Person)-[k:Knows]->",
"(p2:Person) RETURN p1, p2, k"))
# Convert the Ladybug result to a g6R-compatible list
igraph_graph <- as_igraph(graph_query_result)
g6 <- g6_igraph(igraph_graph) |>
g6_layout(d3_force_layout()) |>
g6_options(
animation = FALSE,
node = list(
style = list(
labelText = JS("(d) => d.name")
)
),
edge = list(
style = list(
endArrow = TRUE,
labelText = JS("(d) => d.data.label")
)
)
) |>
g6_behaviors(
zoom_canvas(),
collapse_expand(),
drag_canvas(),
drag_element()
) |>
g6_plugins("toolbar")
# Display the graph
g6