Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Last active June 6, 2026 00:26
Show Gist options
  • Select an option

  • Save abikoushi/c296205d474aa6dc02de53a18962e286 to your computer and use it in GitHub Desktop.

Select an option

Save abikoushi/c296205d474aa6dc02de53a18962e286 to your computer and use it in GitHub Desktop.
plot DAG using DiagrammeR
library(DiagrammeR)
library(DiagrammeRsvg)
library(rsvg)
g1 <- grViz("digraph{
node[shape = plaintext]
U[label = 'U', shape=circle]
X[label = 'X']
Z[label = 'Z']
Y[label = 'Y']
{ rank=same; X; Z; Y; }
U -> X
X -> Z
Z -> Y
U -> Y
}")
print(g1)
rsvg_png(charToRaw(export_svg(g1)),
"graph1.png",
width = 500, height = 500)
g2 <- grViz("digraph{
node[shape = plaintext]
U[label = 'U', shape=circle]
X[label = 'X']
Z[label = 'Z']
Y[label = 'Y']
{ rank=same; X; Z; Y; }
U -> X
Z -> X
X -> Y
U -> Y
}")
print(g2)
rsvg_png(charToRaw(export_svg(g2)),
"graph2.png",
width = 500, height = 500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment