Tidy a dagitty
object
Arguments
- .dagitty
a
dagitty
- seed
a numeric seed for reproducible layout generation
- layout
a layout available in
ggraph
. Seeggraph::create_layout()
for details. Alternatively,"time_ordered"
will usetime_ordered_coords()
to algorithmically sort the graph by time.- ...
optional arguments passed to
ggraph::create_layout()
- use_existing_coords
(Advanced). Logical. Use the coordinates produced by
dagitty::coordinates(.dagitty)
? If the coordinates are empty,tidy_dagitty()
will generate a layout. Generally, setting this toFALSE
is thus only useful when there is a difference in the variables coordinates and the variables in the DAG, as sometimes happens when recompiling a DAG.
Examples
library(dagitty)
library(ggplot2)
dag <- dagitty("dag {
Y <- X <- Z1 <- V -> Z2 -> Y
Z1 <- W1 <-> W2 -> Z2
X <- W1 -> Y
X <- W2 -> Y
X [exposure]
Y [outcome]
}")
tidy_dagitty(dag)
#> # DAG:
#> # A `dagitty` DAG with: 7 nodes and 12 edges
#> # Exposure: X
#> # Outcome: Y
#> #
#> # Data:
#> # A tibble: 13 × 7
#> name x y direction to xend yend
#> <chr> <dbl> <dbl> <fct> <chr> <dbl> <dbl>
#> 1 V -2.70 -0.654 -> Z1 -3.47 0.538
#> 2 V -2.70 -0.654 -> Z2 -1.49 0.0798
#> 3 W1 -2.78 1.23 -> X -2.72 1.85
#> 4 W1 -2.78 1.23 -> Y -1.59 1.57
#> 5 W1 -2.78 1.23 -> Z1 -3.47 0.538
#> 6 W1 -2.78 1.23 <-> W2 -1.86 0.997
#> 7 W2 -1.86 0.997 -> X -2.72 1.85
#> 8 W2 -1.86 0.997 -> Y -1.59 1.57
#> 9 W2 -1.86 0.997 -> Z2 -1.49 0.0798
#> 10 X -2.72 1.85 -> Y -1.59 1.57
#> 11 Y -1.59 1.57 NA NA NA NA
#> 12 Z1 -3.47 0.538 -> X -2.72 1.85
#> 13 Z2 -1.49 0.0798 -> Y -1.59 1.57
#> #
#> # ℹ Use `pull_dag() (`?pull_dag`)` to retrieve the DAG object and `pull_dag_data() (`?pull_dag_data`)` for the data frame
tidy_dagitty(dag, layout = "fr") |>
ggplot(aes(x = .data$x, y = .data$y, xend = .data$xend, yend = .data$yend)) +
geom_dag_node() +
geom_dag_text() +
geom_dag_edges() +
theme_dag()