Directed Acyclic Graphs

Authors

[Editor] Hu Zheng;

[Contributors]

Modified

2026-02-03

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-02-03
 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.000186 ->        z1     0.518 -1.01  FALSE   
 2 v      1.50   0.000186 ->        z2     0.500  0.997 FALSE   
 3 w1    -0.913 -0.617    ->        x     -0.306 -0.434 FALSE   
 4 w1    -0.913 -0.617    ->        y     -0.961  0.557 FALSE   
 5 w1    -0.913 -0.617    ->        z1     0.518 -1.01  FALSE   
 6 w1    -0.913 -0.617    <->       w2    -0.343  0.503 FALSE   
 7 w2    -0.343  0.503    ->        x     -0.306 -0.434 FALSE   
 8 w2    -0.343  0.503    ->        y     -0.961  0.557 FALSE   
 9 w2    -0.343  0.503    ->        z2     0.500  0.997 FALSE   
10 x     -0.306 -0.434    ->        y     -0.961  0.557 FALSE   
11 y     -0.961  0.557    <NA>      <NA>  NA     NA     FALSE   
12 z1     0.518 -1.01     ->        x     -0.306 -0.434 FALSE   
13 z2     0.500  0.997    ->        y     -0.961  0.557 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