Search code examples
positiongraphvizdirected-acyclic-graphs

Position a node to the right of a subgraph with graphviz


I'm using graphviz to plot the DAG of a simple latent variable model:

digraph G {

    splines=line;

    subgraph cluster {
        node [style=filled, shape=circle];
        edge [color=blue]
        z[fillcolor=white, color=black, pos = "0,0!"]
        z -> x;
    }

    theta[label = "θ", shape=circle, pos = "10,0!"]
    edge [color=black, style="dashed"]
    theta->z
    theta->x

}

The output is decent:

enter image description here

but I'd like θ to be at the same height as z. Is that possible? I tried to use the pos attribute, but as you see, it's nicely ignored. I'm working in HackMD.


Solution

  • You can add constraint=false attribute to your theta edges so they won't affect layout and nodes will remain side by side:

    digraph G {
    
        splines=line;
    
        subgraph cluster1 {
            node [style=filled, shape=circle];
            edge [color=blue]
            z[fillcolor=white, color=black, pos = "0,0!"]
            z -> x;
        }
        theta[label = "θ", shape=circle, pos = "10,0!"]
        edge [color=black, style="dashed"]
        theta -> z [constraint=false]
        theta -> x [constraint=false] // actually this one is unnecessary, may be omited in this example
    
    }
    

    Also, you may experiment with edge direction (e.g. change places a->b, b->a), this sometimes helps with positioning clusters.