library(ggtargets)library(ggtargets)tar_ggplot() is the one-call entry point for a live targets pipeline, but everything downstream of as_dag() works for any directed acyclic graph.
The simplest input is a data frame with from and to columns - the node list is derived from the edges themselves, in the order they first appear:
edges <- data.frame(from = c("a", "a", "b"), to = c("b", "c", "c"))
dag <- as_dag(edges)
ggplot_dag(dag)Pass nodes to add attributes - a label distinct from name, or any column to color by (node_color) or to shape by (node_shape, see the Customizing article):
nodes <- data.frame(
name = c("a", "b", "c"),
type = c("input", "process", "process")
)
dag2 <- as_dag(edges, nodes = nodes)
ggplot_dag(dag2, node_color = "type")dag_collapse() merges the nodes that share a value of a column into one node, labeled with how many it stands for, and draws the edges between the groups once each, with an n that counts them. A node with no value (NA) stays on its own. The collapsed graph is a dag like any other, so node_size = "n" and edge_width = "n" can show the counts:
stages <- as_dag(
data.frame(
from = c("raw", "clean", "tidy", "tidy", "fit", "eval"),
to = c("clean", "tidy", "fit", "eval", "eval", "report")
),
nodes = data.frame(
name = c("raw", "clean", "tidy", "fit", "eval", "report"),
stage = c("data", "data", "data", "model", "model", "report")
)
)
small <- dag_collapse(stages, "stage")
ggplot_dag(small, node_color = "stage", node_size = "n", edge_width = "n")If grouping would make a loop, with a group that depends on another group that depends back on it, dag_collapse() stops with an error that names the groups and the edges. The names row, col, x, y and color_index can’t be used for a node column, because the layout keeps them for its own. For a targets pipeline, see tar_ggplot(collapse_by = ).
tar_network()-shaped listas_dag() also accepts the list targets::tar_network() returns directly (tar_dag() is a thin wrapper around exactly this) - useful for a network object saved from another project, without needing that project’s _targets.R or store on hand:
# saved <- targets::tar_network() # from inside the other project, once
saved <- list(
vertices = data.frame(
name = c("a", "b", "c"),
type = c("stem", "stem", "stem")
),
edges = data.frame(from = c("a", "b"), to = c("b", "c"))
)
ggplot_dag(as_dag(saved))The type column of such a list is what draws a node as a circle (a regular target), a square (dynamic branches) or a triangle (an imported function or object); see targets_node_shapes(). The example above has only regular targets, so it has only circles and no shape legend.
For a live pipeline elsewhere (rather than a previously saved network object), use withr::with_dir() (see the Get started article for the full walkthrough, including why script/store alone aren’t enough):
project_dir <- withr::local_tempdir()
withr::with_dir(project_dir, {
targets::tar_script(
list(targets::tar_target(x, 1), targets::tar_target(y, x + 1))
)
})
withr::with_dir(project_dir, tar_ggplot())ggtargets reads edge-list data frames and tar_network()-shaped lists; it doesn’t have a method for igraph objects, Graphviz DOT files, or other graph classes directly (as_dag() gives a clear error naming the class). Convert to an edge-list data frame first - for igraph, igraph::as_data_frame() does exactly that:
g <- igraph::graph_from_edgelist(
matrix(c("a", "b", "a", "c", "b", "c"), ncol = 2, byrow = TRUE)
)
edges_from_igraph <- igraph::as_data_frame(g, what = "edges")
ggplot_dag(as_dag(edges_from_igraph))