ggdag

repository·main·Indexed 19 days ago

https://github.com/r-causal/ggdag

An R package for visualizing and analyzing causal directed acyclic graphs (DAGs). It provides a tidyverse-compatible interface to the dagitty package, enabling users to create, analyze, and plot DAGs using ggplot2 and ggraph. Key features include the dagify() function for defining causal relationships via R formulas, tidy_dagitty() for converting objects into tidy data frames, and specialized geoms and themes for DAG visualization.

Tokens
1.2K
Snippets
5
Records
5
Agent score
17%

What's inside ggdag

  1. How ggdag and dagitty work together

    main

    ggdag is designed to work with the dagitty package. It provides a tidyverse-friendly interface to create, analyze, and plot causal directed acyclic graphs (DAGs).

    You can use ggdag in two primary ways:

    1. Tidying existing dagitty objects: Use tidy_dagitty(dag) to convert a dagitty object into a tidy data frame suitable for ggplot2.
    2. Creating DAGs with R-like syntax: Use the dagify() function to define causal relationships using formula syntax, which is more intuitive for R users than the dagitty string syntax.

    To retrieve the original DAG object from a tidied object, use pull_dag(). To retrieve the underlying data frame, use pull_dag_data().

    library(ggdag)
    library(ggplot2)
    
    # Example: Creating a DAG using dagify syntax
    tidy_ggdag <- dagify(
      y ~ x + z2 + w2 + w1,
      x ~ z1 + w1 + w2,
      z1 ~ w1 + v,
      z2 ~ w2 + v,
      w1 ~ ~w2, # bidirected path
      exposure = "x",
      outcome = "y",
      coords = time_ordered_coords()
    ) |> 
      tidy_dagitty()
  2. Plot DAGs with ggplot2

    main

    Once a DAG is tidied, you can plot it using ggdag() and standard ggplot2 layers.

    Basic Plotting:

    ggdag(tidy_ggdag) + theme_dag()

    Plotting Adjustment Sets: Use ggdag_adjustment_set() to visualize adjustment sets.

    ggdag_adjustment_set(tidy_ggdag, node_size = 14) + theme(legend.position = "bottom")

    Advanced Customization: You can use specific ggdag geoms and scales for fine-grained control over edges, nodes, and text:

    • geom_dag_edges(): Draws edges (supports end_cap).
    • geom_dag_collider_edges(): Draws collider edges.
    • geom_dag_point(): Draws nodes.
    • geom_dag_text(): Draws node labels.
    • theme_dag(): A specialized theme for DAGs.
    • scale_adjusted(): Scales for adjusted/unadjusted status.
    • node_dconnected(): A function to identify d-connected nodes for specific paths.
    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(end_cap = ggraph::circle(10, "mm")) +
      geom_dag_collider_edges() +
      geom_dag_point() +
      geom_dag_text(col = "white") +
      theme_dag() +
      scale_adjusted(include_color = FALSE) +
      expand_plot(expand_y = expansion(c(0.2, 0.2))) +
      scale_color_viridis_d(
        name = "d-relationship",
        na.value = "grey85",
        begin = .35
      )
  3. Install ggdag

    main

    You can install the stable version of ggdag from CRAN or the development version from GitHub.

    From CRAN:

    install.packages("ggdag")

    From GitHub (Development version): Requires the devtools package.

    # install.packages("devtools")
    devtools::install_github("r-causal/ggdag")
    install.packages("ggdag")
  4. Visualize common bias structures

    main

    The package includes helper functions to quickly visualize common causal bias patterns:

    • Equivalent DAGs: ggdag_equivalent_dags(confounder_triangle()) shows equivalent DAG structures.
    • Butterfly Bias: ggdag_butterfly_bias(edge_type = "diagonal") visualizes butterfly bias patterns.
    ggdag_equivalent_dags(confounder_triangle())
    
    ggdag_butterfly_bias(edge_type = "diagonal")
  5. Create a DAG using dagify()

    main

    The dagify() function allows you to define a DAG using R formulas.

    Arguments:

    • Formulas (e.g., y ~ x + z): Define the causal relationships.
    • exposure: The name of the exposure variable.
    • outcome: The name of the outcome variable.
    • coords: Coordinate information (e.g., time_ordered_coords()).
    • Bidirected paths (e.g., w1 ~ ~w2) can be specified using the ~ ~ syntax.

    After creation, it is common to pipe the result into tidy_dagitty() to prepare it for plotting.

    tidy_ggdag <- dagify(
      y ~ x + z2 + w2 + w1,
      x ~ z1 + w1 + w2,
      z1 ~ w1 + v,
      z2 ~ w2 + v,
      w1 ~ ~w2,
      exposure = "x",
      outcome = "y",
      coords = time_ordered_coords()
    ) |> 
      tidy_dagitty()