r-causal / ggdag

:arrow_lower_left: :arrow_lower_right: An R package for working with causal directed acyclic graphs (DAGs)
https://r-causal.github.io/ggdag/
Other
425 stars 28 forks source link

Suggestion for a vignette enhancement #112

Open fBedecarrats opened 1 year ago

fBedecarrats commented 1 year ago

Thanks again for this great package. One use-case I think for this package is to reproduce and enhance the DAG display proposed by daggity.net, that is node and edges with the following semiology: image I tried and found it not trivial to reproduce with {ggdag}. I think it might be useful to show how to reproduce such visualization in a vignette. Maybe it's just me, but I thought it might be of interest to other newcomers.

krassowski commented 11 months ago

It is not easy. I managed to get 90% there, e.g.

image

at which point there was almost no code left from ggdag as I had to rewrite everything using dagitty, ggforce and ggraph internals. And I did not find a way to get biasing paths 100% right (note how EDN1.9 → EDN1.10 is not coloured as biasing) as this is not exposed AFAIK as a public API of dagitty.

API-side it looks like this:

p = (
    ggplot()
    + aes(x=x, y=y, xend=xend, yend=yend)
    + geom_dagitty_edge2()
    + geom_dagitty_node()
    + geom_node_label(nudge_y=-0.3)
    + theme_dagitty()
    + scale_y_continuous(expand = expansion(mult=0.25))
    + scale_x_continuous(expand = expansion(mult=0.25))
)
(
    p %+% node_dagitty(dagitty::getExample('M-bias'))
    + p %+% node_dagitty(dagitty::getExample('mediator'))
    + p %+% node_dagitty(dagitty::getExample('Sebastiani'))
    + patchwork::plot_layout(design="AB\nCC", heights=c(1, 3), guides='collect')
    + patchwork::plot_annotation(tag_levels = 'a')
)
fBedecarrats commented 11 months ago

Thanks and bravo! Is there a snippet I could look at to understand how you approached this? (No hurry at all, I' m on holidays for the next 10 days)

malcolmbarrett commented 11 months ago

Can you all tell me your goal in replicating the dagitty site output? Do you prefer the look? Are the legends and colors helpful to you in some capacity? I definitely don't intend on being able to do @krassowski did out of box with ggdag, but there may be core components here that would be useful to add

krassowski commented 11 months ago

I uploaded my code here for reference https://github.com/krassowski/ggdagitty. Some places where I needed to extend ggdag include:

Some related upstream issues are:

malcolmbarrett commented 11 months ago

All three of those items would be good (preferably separate) PRs to ggdag

There's already an issue for the control point problem #21