# Install packages
if (!requireNamespace("ggdag", quietly = TRUE)) {
install.packages("ggdag")
}
# Load packages
library(ggdag)Directed Acyclic Graphs
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:
Visualizing directed acyclic graphs.
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
ggdag
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.00901 -> z1 0.517 -1.00 FALSE
2 v 1.50 0.00901 -> z2 0.504 1.01 FALSE
3 w1 -0.320 -0.464 -> x -0.932 -0.603 FALSE
4 w1 -0.320 -0.464 -> y -0.329 0.460 FALSE
5 w1 -0.320 -0.464 -> z1 0.517 -1.00 FALSE
6 w1 -0.320 -0.464 <-> w2 -0.942 0.590 FALSE
7 w2 -0.942 0.590 -> x -0.932 -0.603 FALSE
8 w2 -0.942 0.590 -> y -0.329 0.460 FALSE
9 w2 -0.942 0.590 -> z2 0.504 1.01 FALSE
10 x -0.932 -0.603 -> y -0.329 0.460 FALSE
11 y -0.329 0.460 <NA> <NA> NA NA FALSE
12 z1 0.517 -1.00 -> x -0.932 -0.603 FALSE
13 z2 0.504 1.01 -> y -0.329 0.460 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
