Directed Acyclic Graphs

Authors

[Editor] Hu Zheng;

[Contributors]

Modified

2026-04-20

πŸ€– AI Skill β€” Download the Bizard unified skill for your AI assistant ⬇️ Download Skill ZIP
Note

Hiplot website

This page is the tutorial for source code version of the Hiplot Directed Acyclic Graphs plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:

https://hiplot.cn/basic/ggdag?lang=en

Visualizing directed acyclic graphs.

Setup

  • System Requirements: Cross-platform (Linux/MacOS/Windows)

  • Programming language: R

  • Dependent packages: ggdag

# Install packages
if (!requireNamespace("ggdag", quietly = TRUE)) {
  install.packages("ggdag")
}

# Load packages
library(ggdag)
sessioninfo::session_info("attached")
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.5.3 (2026-03-11)
 os       Ubuntu 24.04.4 LTS
 system   x86_64, linux-gnu
 ui       X11
 language (EN)
 collate  C.UTF-8
 ctype    C.UTF-8
 tz       UTC
 date     2026-04-21
 pandoc   3.1.3 @ /usr/bin/ (via rmarkdown)
 quarto   1.9.37 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package * version date (UTC) lib source
 ggdag   * 0.2.13  2024-07-22 [1] RSPM

 [1] /home/runner/work/_temp/Library
 [2] /opt/R/4.5.3/lib/R/site-library
 [3] /opt/R/4.5.3/lib/R/library
 * ── Packages attached to the search path.

──────────────────────────────────────────────────────────────────────────────

Data Preparation

# Load data
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()

# View data
head(tidy_ggdag)
$data
# A tibble: 13 Γ— 8
   name       x       y direction to      xend   yend circular
   <chr>  <dbl>   <dbl> <fct>     <chr>  <dbl>  <dbl> <lgl>   
 1 v     -1.50  -0.0356 ->        z1    -0.481 -1.02  FALSE   
 2 v     -1.50  -0.0356 ->        z2    -0.535  0.993 FALSE   
 3 w1     0.335 -0.459  ->        x      0.953 -0.568 FALSE   
 4 w1     0.335 -0.459  ->        y      0.920  0.610 FALSE   
 5 w1     0.335 -0.459  ->        z1    -0.481 -1.02  FALSE   
 6 w1     0.335 -0.459  <->       w2     0.304  0.478 FALSE   
 7 w2     0.304  0.478  ->        x      0.953 -0.568 FALSE   
 8 w2     0.304  0.478  ->        y      0.920  0.610 FALSE   
 9 w2     0.304  0.478  ->        z2    -0.535  0.993 FALSE   
10 x      0.953 -0.568  ->        y      0.920  0.610 FALSE   
11 y      0.920  0.610  <NA>      <NA>  NA     NA     FALSE   
12 z1    -0.481 -1.02   ->        x      0.953 -0.568 FALSE   
13 z2    -0.535  0.993  ->        y      0.920  0.610 FALSE   

$dag
dag {
v
w1
w2
x [exposure]
y [outcome]
z1
z2
v -> z1
v -> z2
w1 -> x
w1 -> y
w1 -> z1
w1 <-> w2
w2 -> x
w2 -> y
w2 -> z2
x -> y
z1 -> x
z2 -> y
}

Visualization

# Directed Acyclic Graphs
p <- ggdag(tidy_ggdag) +
  theme_dag() 

p
FigureΒ 1: Directed Acyclic Graphs