# 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
<- dagify(
tidy_ggdag ~ x + z2 + w2 + w1,
y ~ z1 + w1 + w2,
x ~ w1 + v,
z1 ~ w2 + v,
z2 ~ ~w2, # bidirected path
w1 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.00694 -> z1 0.511 1.01 FALSE
2 v 1.50 -0.00694 -> z2 0.502 -1.01 FALSE
3 w1 -0.933 0.593 -> x -0.315 0.472 FALSE
4 w1 -0.933 0.593 -> y -0.939 -0.585 FALSE
5 w1 -0.933 0.593 -> z1 0.511 1.01 FALSE
6 w1 -0.933 0.593 <-> w2 -0.322 -0.470 FALSE
7 w2 -0.322 -0.470 -> x -0.315 0.472 FALSE
8 w2 -0.322 -0.470 -> y -0.939 -0.585 FALSE
9 w2 -0.322 -0.470 -> z2 0.502 -1.01 FALSE
10 x -0.315 0.472 -> y -0.939 -0.585 FALSE
11 y -0.939 -0.585 <NA> <NA> NA NA FALSE
12 z1 0.511 1.01 -> x -0.315 0.472 FALSE
13 z2 0.502 -1.01 -> y -0.939 -0.585 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
<- ggdag(tidy_ggdag) +
p theme_dag()
p
