Directed Acyclic Graphs

Authors

[Editor] Hu Zheng;

[Contributors]

Modified

2026-01-17

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.2 (2025-10-31)
 os       Ubuntu 24.04.3 LTS
 system   x86_64, linux-gnu
 ui       X11
 language (EN)
 collate  C.UTF-8
 ctype    C.UTF-8
 tz       UTC
 date     2026-01-17
 pandoc   3.1.3 @ /usr/bin/ (via rmarkdown)
 quarto   1.8.27 @ /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.2/lib/R/site-library
 [3] /opt/R/4.5.2/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.0115 ->        z1     0.534  1.01  FALSE   
 2 v      1.50  -0.0115 ->        z2     0.487 -0.999 FALSE   
 3 w1    -0.292  0.429  ->        x     -0.899  0.628 FALSE   
 4 w1    -0.292  0.429  ->        y     -0.974 -0.525 FALSE   
 5 w1    -0.292  0.429  ->        z1     0.534  1.01  FALSE   
 6 w1    -0.292  0.429  <->       w2    -0.358 -0.527 FALSE   
 7 w2    -0.358 -0.527  ->        x     -0.899  0.628 FALSE   
 8 w2    -0.358 -0.527  ->        y     -0.974 -0.525 FALSE   
 9 w2    -0.358 -0.527  ->        z2     0.487 -0.999 FALSE   
10 x     -0.899  0.628  ->        y     -0.974 -0.525 FALSE   
11 y     -0.974 -0.525  <NA>      <NA>  NA     NA     FALSE   
12 z1     0.534  1.01   ->        x     -0.899  0.628 FALSE   
13 z2     0.487 -0.999  ->        y     -0.974 -0.525 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