Search code examples
graphviz

Controlling the ordering of nodes within a GraphViz subgraph cluster


I've got two cluster subgraphs in GraphViz, with edges between the nodes in each subgraph. I'd like to be able to influence the top-to-bottom ordering of the nodes within the second subgraph so as to achieve symmetry across the horizontal axis, and have the nodes in the second subgraph ordered top-to-bottom as a, b, c, d.

digraph {
    rankdir=LR

    subgraph cluster_1 {
        bx -> cx
        by -> cy
    }

    subgraph cluster_2 {
        a; b; c; d
    }

    bx -> a
    cx -> b
    cy -> c
    by -> d
}

Right now, I get this:

Resulting GraphViz graph

Changing the ordering of node appearance within the cluster_2 subgraph doesn't seem to matter, nor does changing the ordering of the cross-cluster edges at the bottom.

Is there any way to get GraphViz to produce what I want here?


Solution

  • You can add some scaffolding edges and/or nodes i.e. invisible edges/nodes but it can be a bit triggy especially when clusters are involved

    e.g.:

    digraph {
        rankdir=LR
        
        subgraph cluster_1 {
            bx -> cx
            by -> _d [dir=none]
            by -> cy
            _a[shape=point height=0]
            _d[shape=point height=0]
            bx -> _a [dir=none]
        }
    
        subgraph cluster_2 {
            a; b; c; d
        }
    
        //bx -> a
        cx -> b
        cy -> c
        //by -> d
        
        _a->a
        _d->d
    }
    

    enter image description here