Title: | Make Beautiful Directed Acyclic Graphs |
---|---|
Description: | This package allows the user the save directed acyclic graphs (DAGs) generated in DiagrammeR and export them to pdf, png, or svg format. Users may optionally choose to view the DAG without saving or to embed it in an RMarkdown document. In addition, quickDAG allows the user easily to reformat DAGs as single-world intervention graph (SWIG) templates. |
Authors: | Jason Gantenberg [aut, cre] |
Maintainer: | Jason Gantenberg <[email protected]> |
License: | GPL-3 |
Version: | 0.2.0 |
Built: | 2025-03-12 12:34:21 UTC |
Source: | https://github.com/jrgant/quickdag |
Save directed acyclic graphs (DAGs) generated in DiagrammeR and export them to pdf, png, or svg format. Users may optionally choose to view the DAG without saving or to embed it in an RMarkdown document.
makeDAG( graphcode = NULL, dagname = NULL, filetype = "pdf", text.nodes = NULL, box.nodes = NULL, solid.edges = NULL, dashed.edges = NULL, footnotes = NULL, direction = "LR", embed = FALSE, ... )
makeDAG( graphcode = NULL, dagname = NULL, filetype = "pdf", text.nodes = NULL, box.nodes = NULL, solid.edges = NULL, dashed.edges = NULL, footnotes = NULL, direction = "LR", embed = FALSE, ... )
graphcode |
Partial graphViz code object, which will give you the most control over the appearance of your DAG. |
dagname |
A path with which to name your DAG file, if |
filetype |
Output file format. Select from |
text.nodes |
A string containing the plain text nodes, separated by spaces. |
box.nodes |
A string containing the boxed nodes, separated by spaces. |
solid.edges |
A string specifying the paths you want to draw between measured covariates. Example: |
dashed.edges |
A string specifying paths containing unmeasured covariates. |
footnotes |
Add a footnote to the bottom of the graph. |
direction |
Specify the direction of diagram flow. Defaults to |
embed |
For use within R chunks in RMarkdown only. You will probably want to have the |
... |
Pass arguments to interior functions for PNG or SVG files. For instance, specify |
This is not a true DAG package in the sense that it will not prevent the inclusion of feedback loops or bidirectional arrows. It's meant mostly to create reasonable-looking DAGs quickly and easily with a minimum of layout or formatting code. DiagrammeR in general does a pretty good job at layout. Those interested in DAGs might check out other R packages like dagR or dagitty, both of which I've yet to explore in detail.
Suggestions and issue reports welcome at https://github.com/jrgant/quickDAG/issues!
A fair amount of the heavy lifting here is done thanks to code snippets from users HJAllen and puterleat on the following thread: https://github.com/rich-iannone/DiagrammeR/issues/133
Packages used: DiagrammeR, DiagrammeRsvg, rsvg
# Using your own graph code # Examples have been removed, as makeDAG() is now deprecated.
# Using your own graph code # Examples have been removed, as makeDAG() is now deprecated.
Provide simple syntax specifying paths between nodes to generate a graph object.
qd_dag( edgelist, node.labs = NULL, node.aes.opts = list(), edge.aes.opts = list(), verbose = TRUE, check.dag = TRUE, theme = "base", ... )
qd_dag( edgelist, node.labs = NULL, node.aes.opts = list(), edge.aes.opts = list(), verbose = TRUE, check.dag = TRUE, theme = "base", ... )
edgelist |
A vector of edge relationships. Must be strictly organized (see example for format). |
node.labs |
A named character vector containing label names. Defaults to |
node.aes.opts |
A list feeding aesthetic options for nodes to |
edge.aes.opts |
A list feeding aesthetic options for edges to |
verbose |
Indicate whether to print node and edge dataframes to the console. See NOTE below. Defaults to |
check.dag |
Logical. Check whether the graph conforms to the rules of DAGs. Defaults to |
theme |
Choose theme for plot output. Defaults to "base". Setting theme to NULL will use DiagrammeR's NULL attribute theme. |
... |
Pass optional |
Suggestions and bug reports welcome at https://github.com/jrgant/quickDAG/issues.
Packages used: DiagrammeR, stringr, purrr
Leaving the checks
option selected may be advisable to ensure labels and IDs have not been mismatched. By default, qd_dag()
alphabetizes nodes included in edgelist
and does the same for node.labs
under a first assumption that labels will begin with the same letter as their corresponding alpha.id
, which may not always be the case.
# Provide a list of edges, with nodes specified as letters. # Do not list a node as a parent more than once. # Each line should contain a single edge character '->'. edges <- c("A -> { B C }", "B -> C") # make a DAG object and render the graph using the default theme g.obj <- qd_dag(edges) render_graph(g.obj) # Pass labels and aesthetic options for nodes or edges g.obj2 <- qd_dag(edges, node.labs = c("A" = "Alcohol", "B" = "BP", "C" = "CVD"), node.aes.opts = list(shape = "plaintext", fillcolor = "none", color = "black"), edge.aes.opts = list(arrowsize = 0.5, color = "gray"), theme = NULL) render_graph(g.obj2)
# Provide a list of edges, with nodes specified as letters. # Do not list a node as a parent more than once. # Each line should contain a single edge character '->'. edges <- c("A -> { B C }", "B -> C") # make a DAG object and render the graph using the default theme g.obj <- qd_dag(edges) render_graph(g.obj) # Pass labels and aesthetic options for nodes or edges g.obj2 <- qd_dag(edges, node.labs = c("A" = "Alcohol", "B" = "BP", "C" = "CVD"), node.aes.opts = list(shape = "plaintext", fillcolor = "none", color = "black"), edge.aes.opts = list(arrowsize = 0.5, color = "gray"), theme = NULL) render_graph(g.obj2)
A wrapper around qd_save()
meant for use within R code chunks in RMarkdown documents.
qd_embed(...)
qd_embed(...)
... |
Pass arguments to |
Export high-quality, scalable graphics for both print and online.
qd_save(graph, filename = NULL, filetype = "pdf", embed = F, ...)
qd_save(graph, filename = NULL, filetype = "pdf", embed = F, ...)
graph |
Either a DiagrammeR graph object or a diagram generated by |
filename |
String for filename. Defaults to |
filetype |
One of |
embed |
Defaults to FALSE. Automatically set to TRUE by |
... |
Pass arguments to |
qd_save()
:
Take a DAG graph object and, in the simplest case, create a single-world intervention template corresponding to a world in which the fixed nodes are set to a given value. Alternatively, tell qd_swig which values fixed nodes will be set to.
qd_swig( graph.obj, fixed.nodes, custom.values = NULL, fixed.sep = "vlin", sep.point.size = 15 )
qd_swig( graph.obj, fixed.nodes, custom.values = NULL, fixed.sep = "vlin", sep.point.size = 15 )
graph.obj |
A DAG object created by |
fixed.nodes |
A vector containing the nodes to be intervened upon. |
custom.values |
A named vector containing alternative labels identifying explicit values for fixed nodes (e.g., a = 1). |
fixed.sep |
A character string indicating which character to use as a separator in fixed nodes. Defaults to "vlin". Run |
sep.point.size |
A numerical value specifying the point size for fixed node separators. |
# Provide a DAG object and a list of nodes to be fixed edges <- c("A -> Y", "L -> { A Y }") dag <- qd_dag(edges) swig <- dag %>% qd_swig(fixed.nodes = "A", custom.values = c("A" = "1")) swig %>% render_graph()
# Provide a DAG object and a list of nodes to be fixed edges <- c("A -> Y", "L -> { A Y }") dag <- qd_dag(edges) swig <- dag %>% qd_swig(fixed.nodes = "A", custom.values = c("A" = "1")) swig %>% render_graph()
Apply various pre-fabricated themes to diagrams.
qd_themes(graph.obj, theme, ...) theme_base(graph.obj, font = "serif", ...) theme_circles(graph.obj, font = "serif", ...) theme_dots(graph.obj, font = "serif", ...) get_conditioned_nodes(graph.obj, conditioned = NULL)
qd_themes(graph.obj, theme, ...) theme_base(graph.obj, font = "serif", ...) theme_circles(graph.obj, font = "serif", ...) theme_dots(graph.obj, font = "serif", ...) get_conditioned_nodes(graph.obj, conditioned = NULL)
graph.obj |
A DAG object created by |
theme |
A character string indicating the theme to use. Defaults to "base". Set to |
... |
Pass arguments to theme call (e.g., |
font |
A character vector indicating the font family to use for node labels. Defaults to "serif". |
conditioned |
A character vector indicating which nodes are conditioned upon. The shape for these nodes will be set to "square". |
Format an edgelist and send it to dagitty to identify variable adjustment sets.
qd_todagitty( edgelist, diagram_type = "dag", showplot = FALSE, send.global = FALSE, dagitty.obj.name = NULL, exposure, outcome, ... )
qd_todagitty( edgelist, diagram_type = "dag", showplot = FALSE, send.global = FALSE, dagitty.obj.name = NULL, exposure, outcome, ... )
edgelist |
A vector of edge relationships. Must be strictly organized (see example for format). |
diagram_type |
Character identifying the diagram type. Defaults to |
showplot |
Logical indicating whether to produce a dagitty plot. Defaults to |
send.global |
Logical indicating whether to make the dagitty object available in the global environment. Defaults to |
dagitty.obj.name |
Character specifying the name of the dagitty object. Only used and required if |
exposure |
Character. Specify exposure of interest. (Required) |
outcome |
Character. Specifiy outcome of interest. (Required) |
... |
Pass arguments to |
The exposure
and outcome
options map to dagitty functions of the same name.
edges <- c("A -> { B C D }", "B -> C", "E -> { B C }") # must pass exposure and outcome arguments to dagitty::adjustmentSets() qd_todagitty(edges, exposure = "A", outcome = "C") qd_todagitty(edges, exposure = "A", outcome = "C", type = "minimal")
edges <- c("A -> { B C D }", "B -> C", "E -> { B C }") # must pass exposure and outcome arguments to dagitty::adjustmentSets() qd_todagitty(edges, exposure = "A", outcome = "C") qd_todagitty(edges, exposure = "A", outcome = "C", type = "minimal")
Preview character options for use as the fixed node separator in SWIGs.
sep_opts(table = FALSE)
sep_opts(table = FALSE)
table |
Logical to show or hide HTML table display of available characters. Defaults to |