• Stars
    star
    378
  • Rank 109,318 (Top 3 %)
  • Language
    R
  • License
    Other
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

↙️ β†˜οΈ An R package for working with causal directed acyclic graphs (DAGs)

R-CMD-check CRAN status Lifecycle: maturing Codecov test coverage Total CRAN downloads

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

Tidy, analyze, and plot causal 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 with:

install.packages("ggdag")

Or you can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("r-causal/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)
library(ggplot2)

#  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 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.496  -3.40  ->        z1     1.83   -2.92  FALSE   
#>  2 v     0.496  -3.40  ->        z2     0.0188 -2.08  FALSE   
#>  3 w1    1.73   -1.94  ->        x      2.07   -1.42  FALSE   
#>  4 w1    1.73   -1.94  ->        y      1.00   -0.944 FALSE   
#>  5 w1    1.73   -1.94  ->        z1     1.83   -2.92  FALSE   
#>  6 w1    1.73   -1.94  <->       w2     0.873  -1.56  FALSE   
#>  7 w2    0.873  -1.56  ->        x      2.07   -1.42  FALSE   
#>  8 w2    0.873  -1.56  ->        y      1.00   -0.944 FALSE   
#>  9 w2    0.873  -1.56  ->        z2     0.0188 -2.08  FALSE   
#> 10 x     2.07   -1.42  ->        y      1.00   -0.944 FALSE   
#> 11 y     1.00   -0.944 <NA>      <NA>  NA      NA     FALSE   
#> 12 z1    1.83   -2.92  ->        x      2.07   -1.42  FALSE   
#> 13 z2    0.0188 -2.08  ->        y      1.00   -0.944 FALSE

#  using more R-like syntax to create the same DAG
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"
) %>%
  tidy_dagitty()

tidy_ggdag
#> # 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     -3.58  3.30 ->        z1    -4.05  4.63 FALSE   
#>  2 v     -3.58  3.30 ->        z2    -2.23  3.74 FALSE   
#>  3 w1    -3.03  5.74 ->        x     -3.20  5.14 FALSE   
#>  4 w1    -3.03  5.74 ->        y     -1.98  5.22 FALSE   
#>  5 w1    -3.03  5.74 ->        z1    -4.05  4.63 FALSE   
#>  6 w1    -3.03  5.74 <->       w2    -2.35  4.72 FALSE   
#>  7 w2    -2.35  4.72 ->        x     -3.20  5.14 FALSE   
#>  8 w2    -2.35  4.72 ->        y     -1.98  5.22 FALSE   
#>  9 w2    -2.35  4.72 ->        z2    -2.23  3.74 FALSE   
#> 10 x     -3.20  5.14 ->        y     -1.98  5.22 FALSE   
#> 11 y     -1.98  5.22 <NA>      <NA>  NA    NA    FALSE   
#> 12 z1    -4.05  4.63 ->        x     -3.20  5.14 FALSE   
#> 13 z2    -2.23  3.74 ->        y     -1.98  5.22 FALSE

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

ggdag(tidy_ggdag) +
  theme_dag()

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

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(end_cap = ggraph::circle(10, "mm")) +
  geom_dag_collider_edges() +
  geom_dag_point() +
  geom_dag_text(col = "white") +
  theme_dag() +
  scale_adjusted() +
  expand_plot(expand_y = expansion(c(0.2, 0.2))) +
  scale_color_viridis_d(
    name = "d-relationship",
    na.value = "grey85",
    begin = .35
  )

And common structures of bias:

ggdag_equivalent_dags(confounder_triangle())

ggdag_butterfly_bias(edge_type = "diagonal")

More Repositories

1

causal_inference_r_workshop

Causal Inference in R Workshop
HTML
124
star
2

precisely

🎯 An R package to estimate sample size based on precision rather than power
R
90
star
3

tidymeta

🌲🌲🌲 Tidy and plot meta-analyses in R
R
49
star
4

designing.ggplots

Install Workshop Materials for Designing ggplots
R
47
star
5

ggokabeito

Colorblind-friendly, qualitative Okabe-Ito Scales for ggplot2 and ggraph
R
43
star
6

dagtex

lightly opinionated LaTeX DAGs in R
R
38
star
7

kakashi

kakashi theme for xaringan slides
CSS
30
star
8

causal_inference_r_workshop_solutions

24
star
9

kakashi-quarto-theme

JavaScript
24
star
10

causal-inference-in-R

Causal Inference in R: A book!
TeX
23
star
11

causal_inference_notebook

R Code for Causal Inference by HernΓ‘n and Robins
HTML
20
star
12

zenartofrpkgs

Zen and the Art of R Package Development: New York Open Statistical Programming Meetup, March 2
JavaScript
15
star
13

causalworkshop

Install Workshop Materials for Causal Inference in R
R
15
star
14

stan-book

Repository for book "Bayesian Statistics Using Stan", text files in Rmarkdown, plus example data, Stan models, and scripts
TeX
14
star
15

designing_ggplots

Workshop materials for "Designing ggplots"
13
star
16

designing_ggplots_slides

Slides for the workshop Designing ggplots
HTML
11
star
17

mastering_r_for_epi

Course Materials for Mastering R for Epidemiologic Research
HTML
11
star
18

malco.io

Malcolm Barrett's personal website
HTML
10
star
19

halfmoon

Techniques to Build Better Balance in Propensity Score Models
R
10
star
20

causalinfer

An R package for tidyverse-friendly causal inference
R
10
star
21

metaconfoundr

An R package for visualizing confounder control in meta-analyses
R
9
star
22

propensity

A Toolkit for Calculating and Working with Propensity Scores
R
8
star
23

happy_scientist

HTML
8
star
24

ma_viz_workshop

PM 605: Data Visualization for Meta-Analysis Workshop
HTML
7
star
25

causalpie

πŸ₯§ An R package for easily creating and visualizing sufficient-component cause models
R
7
star
26

cidata

A Data Package for Causal Inference by HernΓ‘n and Robins
R
7
star
27

claremontrun

X-Men Data from the Claremont Run Project
R
6
star
28

rmd_writing

An example of writing workflow in R Markdown
HTML
6
star
29

tidysmd

Tidy Standardized Mean Differences
R
6
star
30

here_here

An example of here
R
6
star
31

SER-tidyverse-workshop

HTML
6
star
32

avalanchr

Tools for the AVALANCHE Data Science Team: A toy pkg for R Package workshops
R
6
star
33

mbmisc

🦁 πŸ‘¨ A personal R package with functions useful for data analysis, formatting, data visualization, and more.
R
5
star
34

useRcausal2020

Install Workshop Materials for Causal Inference in R
R
5
star
35

firstrpkg

Install Workshop Materials for My Organization's First R Package
R
5
star
36

rsg.zenartofrpkgs

You're Already Ready: Zen and the Art of R Package Development
JavaScript
4
star
37

masterepir

Install Course Materials for Mastering R for Epidemiologic Research
R
4
star
38

lawrug_purrr

LA West R Users Talk: Functional Programming with purrr
HTML
4
star
39

ymlthis_edu_talk

HTML
3
star
40

ggecdf

Calculate Weighted and Unweighted ECDFs for ggplot2
R
3
star
41

precisely_talk

A talk on the precisely R package, PM 610
HTML
3
star
42

epibot

πŸ€– Deploy @epi_twit, a Twitter bot that retweets #EpiTwitter
R
3
star
43

aoc_rust

Rust
2
star
44

my-org-first-pkg-2020

HTML
2
star
45

ymlthis_poster

HTML
2
star
46

adventofcode22

Advent of Code 2022 in R and Rust
R
2
star
47

ser_conf_2019

2
star
48

rbootcamp_data_analysis

USC Preventive Medicine R Bootcamp: Data Analysis
TeX
2
star
49

masterepi_slides

Slides for the course Mastering R for Epidemiologic Research
HTML
2
star
50

epibookclub2

Keeping track of the second #EpiBookclub about Kieran Healy's Data Visualization
2
star
51

kiji

Calculate ingredients for various doughs that I like
R
2
star
52

talk_causal_whole_game

HTML
2
star
53

confoundr

An R package for working with confounders
R
1
star
54

talk_ggdag_new_tools

Talk: Causal Diagrams in R with ggdag: New tools for better DAGs
HTML
1
star
55

fp_example

1
star
56

allhands_slides

HTML
1
star
57

data612-sept-20

R
1
star
58

au.rcourse

Install Course Materials for Statistical Programming in R
R
1
star
59

ser_tweets

1
star
60

au_data_412-612_website

JavaScript
1
star
61

partition.benchmark

benchmarks for the partition package
R
1
star
62

epibookclub

1
star
63

causalinfer_ex

1
star
64

data-612-oct-18

1
star
65

data-612-nov-15

R
1
star
66

talk_ggdag_gg_extensions

ggplot extensions group talk: Causal Diagrams in R with ggdag
HTML
1
star
67

ymlthis_talk

HTML
1
star
68

toyrepo

a toy repo for a git workshop
HTML
1
star
69

advr_bookclub_24_25

https://advr-bookclub-24-25.netlify.com/
JavaScript
1
star
70

mbmisc2

A toy πŸ“¦ for teaching R package development
R
1
star
71

data612-sept-13

1
star
72

supercollidr

HTML
1
star
73

koanr

☸️ An R package containing data on important Zen texts
R
1
star
74

mb_slides_mofrp

HTML
1
star