Learn R Programming

⚠️There's a newer version (0.2.13) of this package.Take me there.

ggdag: An R Package for visualizing and analyzing directed acyclic graphs

Tidy, analyze, and plot directed acyclic graphs (DAGs). ggdag uses the powerful dagitty package to create and analyze structural causal models and plot them using ggplot2 and ggraph in a consistent and easy manner.

Installation

You can install ggdag from GitHub with:

# install.packages("devtools")
devtools::install_github("malcolmbarrett/ggdag")

Example

ggdag makes it easy to use dagitty in the context of the tidyverse. You can directly tidy dagitty objects or use convenience functions to create DAGs using a more R-like syntax:

library(ggdag)

#  example from the dagitty package
dag <- dagitty::dagitty( "dag {
    y <- x <- z1 <- v -> z2 -> y
               z1 <- w1 <-> w2 -> z2
               x <- w1 -> y
               x <- w2 -> y
               x [exposure]
               y [outcome]
               }")

tidy_dag <- tidy_dagitty(dag)

tidy_dag 
#> # A tibble: 13 x 8
#>    name      x     y direction to     xend  yend circular
#>    <chr> <dbl> <dbl> <fct>     <chr> <dbl> <dbl> <lgl>   
#>  1 v     11.8   8.03 ->        z1    10.4   7.77 FALSE   
#>  2 v     11.8   8.03 ->        z2    12.1   6.66 FALSE   
#>  3 w1    10.2   6.85 ->        x      9.95  6.28 FALSE   
#>  4 w1    10.2   6.85 ->        y     11.1   6.39 FALSE   
#>  5 w1    10.2   6.85 ->        z1    10.4   7.77 FALSE   
#>  6 w1    10.2   6.85 <->       w2    10.9   5.75 FALSE   
#>  7 w2    10.9   5.75 ->        x      9.95  6.28 FALSE   
#>  8 w2    10.9   5.75 ->        y     11.1   6.39 FALSE   
#>  9 w2    10.9   5.75 ->        z2    12.1   6.66 FALSE   
#> 10 x      9.95  6.28 ->        y     11.1   6.39 FALSE   
#> 11 z1    10.4   7.77 ->        x      9.95  6.28 FALSE   
#> 12 z2    12.1   6.66 ->        y     11.1   6.39 FALSE   
#> 13 y     11.1   6.39 <NA>      <NA>  NA    NA    FALSE

#  using more R-like syntax to create the same DAG
tidy_ggdag <- dagify(y ~ x + z2 + w2 + w1,
             x ~ z1 + w1,
             z1 ~ w1 + v,
             z2 ~ w2 + v,
             w1 ~~ w2, # bidirected path
             exposure = "x",
             outcome = "y") %>% tidy_dagitty()

tidy_ggdag
#> # A tibble: 12 x 8
#>    name      x     y direction to     xend  yend circular
#>    <chr> <dbl> <dbl> <fct>     <chr> <dbl> <dbl> <lgl>   
#>  1 v      7.25  17.2 ->        z1     7.59  18.5 FALSE   
#>  2 v      7.25  17.2 ->        z2     8.57  16.8 FALSE   
#>  3 w1     9.03  18.7 ->        x      8.35  19.3 FALSE   
#>  4 w1     9.03  18.7 ->        y      8.93  18.0 FALSE   
#>  5 w1     9.03  18.7 ->        z1     7.59  18.5 FALSE   
#>  6 w1     9.03  18.7 <->       w2     9.68  17.5 FALSE   
#>  7 w2     9.68  17.5 ->        y      8.93  18.0 FALSE   
#>  8 w2     9.68  17.5 ->        z2     8.57  16.8 FALSE   
#>  9 x      8.35  19.3 ->        y      8.93  18.0 FALSE   
#> 10 z1     7.59  18.5 ->        x      8.35  19.3 FALSE   
#> 11 z2     8.57  16.8 ->        y      8.93  18.0 FALSE   
#> 12 y      8.93  18.0 <NA>      <NA>  NA     NA   FALSE

ggdag also provides functionality for analyzing DAGs and plotting them in ggplot2:

ggdag(tidy_ggdag)
ggdag_adjustment_set(tidy_ggdag)

As well as geoms and other functions for plotting them directly in ggplot2:

dagify(m ~ x + y) %>% 
  tidy_dagitty() %>% 
  node_dconnected("x", "y", controlling_for = "m") %>%
  ggplot(aes(x = x, y = y, xend = xend, yend = yend, shape = adjusted, col = d_relationship)) +
    geom_dag_edges(aes(end_cap = ggraph::circle(10, "mm"))) +
    geom_dag_collider_edges() +
    geom_dag_node() +
    geom_dag_text(col = "white") +
    theme_dag() + 
    scale_dag(expand_y = expand_scale(c(0.2, 0.2)))

And common structures of bias:

ggdag_equivalent_dags(confounder_triangle())

ggdag_butterfly_bias(edge_type = "diagonal")

Copy Link

Version

Install

install.packages('ggdag')

Monthly Downloads

4,959

Version

0.1.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Malcolm Barrett

Last Published

March 27th, 2018

Functions in ggdag (0.1.0)

Assess d-separation between variables

D-relationship between variables
Canonicalize DAGs

Canonicalize a DAG
as_tbl_graph

Convert DAGS to tidygraph
Covariate Adjustment Sets

Covariate Adjustment Sets
Colliders

Find colliders
activate_collider_paths

Activate paths opened by stratifying on a collider
Adjust for variables

Adjust for variables and activate any biasing paths that result
coordinates

Manipulate DAG coordinates
as.data.frame.tidy_dagitty

Convert a tidy_dagitty object to data.frame
dplyr

Dplyr verb methods for tidy_dagitty objects
fortify

Fortify a tidy_dagitty object for ggplot2
as.tbl.tidy_daggity

Convert a tidy_dagitty object to tbl
DAG Edges

Directed DAG edges
Equivalent DAGs and Classes

Generating Equivalent Models
geom_dag_collider_edges

Edges for paths activated by stratification on colliders
dag

Create a dagitty DAG
dagify

Create a dagitty DAG using R-like syntax
ggdag

Quickly plot a DAG in ggplot2
Pathways

Find Pathways Between Variables
geom_dag_edges

Directed and bidirected DAG edges
Exogenous Variables

Find Exogenous Variables
geom_dag_text

Node text
is_confounder

Assess if a variable confounds a relationship
expand_scale

Generate expansion vector for scales.
ggdag_classic

Quickly plot a DAG in ggplot2
is.tidy_dagitty

Test for object class for tidy_dagitty
Test if Variable Is Collider

Detecting colliders in DAGs
%>%

Pipe operator
ggrepel functions

Repulsive textual annotations
Nodes

DAG Nodes
print.tidy_dagitty

Print a tidy_dagitty
Instrumental Variables

Find Instrumental Variables
tbl_df.tidy_daggity

Convert a tidy_dagitty object to tbl_df
scale_dag

Common scale adjustments for DAGs
Assess familial relationships between variables

Familial relationships between variables
theme_dag_blank

A minimalist DAG theme
DAG Labels

DAG labels
simulate_data

Simulate Data from Structural Equation Model
Quick Plots for Common DAGs

Quickly create a DAGs with common structures of bias
Variable Status

Find variable status
reexports

Objects exported from other packages
theme_dag_grey

Simple grey theme for DAGs
tidy_dagitty

Tidy a dagitty object