Created
June 8, 2026 01:42
-
-
Save abikoushi/33e189fcb09f14cb724e8029e8c5cc9b to your computer and use it in GitHub Desktop.
plot DAG for mediation variable method using DiagrammeR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 -> Z [style=invis, weight=100] | |
| X -> Z | |
| Z -> 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