Skip to contents

Tidy a dagitty object

Usage

tidy_dagitty(
  .dagitty,
  seed = NULL,
  layout = "nicely",
  ...,
  use_existing_coords = TRUE
)

Arguments

.dagitty

a dagitty

seed

a numeric seed for reproducible layout generation

layout

a layout available in ggraph. See ggraph::create_layout() for details. Alternatively, "time_ordered" will use time_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 to FALSE 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.

Value

a tidy_dagitty object

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)
#> # A DAG with 7 nodes and 12 edges
#> #
#> # Exposure: X
#> # Outcome: Y
#> #
#> # A tibble: 13 × 8
#>    name       x       y direction to      xend   yend circular
#>    <chr>  <dbl>   <dbl> <fct>     <chr>  <dbl>  <dbl> <lgl>   
#>  1 V     -0.810 -0.935  ->        Z1     0.608 -0.914 FALSE   
#>  2 V     -0.810 -0.935  ->        Z2    -0.849  0.478 FALSE   
#>  3 W1     0.839  0.0307 ->        X      1.31   0.438 FALSE   
#>  4 W1     0.839  0.0307 ->        Y      0.133  0.662 FALSE   
#>  5 W1     0.839  0.0307 ->        Z1     0.608 -0.914 FALSE   
#>  6 W1     0.839  0.0307 <->       W2     0.441  1.22  FALSE   
#>  7 W2     0.441  1.22   ->        X      1.31   0.438 FALSE   
#>  8 W2     0.441  1.22   ->        Y      0.133  0.662 FALSE   
#>  9 W2     0.441  1.22   ->        Z2    -0.849  0.478 FALSE   
#> 10 X      1.31   0.438  ->        Y      0.133  0.662 FALSE   
#> 11 Y      0.133  0.662  NA        NA    NA     NA     FALSE   
#> 12 Z1     0.608 -0.914  ->        X      1.31   0.438 FALSE   
#> 13 Z2    -0.849  0.478  ->        Y      0.133  0.662 FALSE   

tidy_dagitty(dag, layout = "fr") %>%
  ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_dag_node() +
  geom_dag_text() +
  geom_dag_edges() +
  theme_dag()