Search code examples
graphvizdot

Graphviz: using rankdir in subgraph?


I'd like to use TB rankdir for my top-level graph, but LR rankdir within subgraphs. Is this possible? The commented-out rankdir in the subgraph doesn't do anything.

    digraph G{
    graph [compound=true]
    subgraph "cluster_0" {
        label=top
        A->B->C
    }
    { rank=source A B C}
    
    subgraph "cluster_A" {
        label = A_zoom
        // rankdir=LR;
        A1->A2->A3->A6->A7
        A1->A4->A3->A5->A7
    }

    A->A1 [ltail="cluster_0" lhead="cluster_A"]
}

I would really appreciate any help!


Solution

  • Sorry, the rankdir attribute applies to the entire graph (https://graphviz.org/docs/attrs/rankdir/)

    However, often there are graph-specific work-arounds:

    digraph G{
        graph [compound=true]
        rankdir=LR;
        subgraph clusterX{ // increase separation between the clusters
        subgraph "cluster_0" {
            label=top
            A->B->C
        }
        peripheries=0 // hide this cluster, placed at end to prevent inheritance
        }
        //{ rank=source A B C}
        
        subgraph "cluster_A" {
            label = A_zoom
            // rankdir=LR;
            A1->A2->A3->A6->A7
            A1->A4->A3->A5->A7
        }
    
        //A->A1 [ltail="cluster_0" lhead="cluster_A"]
        A->A2 [ltail="cluster_0" lhead="cluster_A"]    
    }
    

    Giving:
    enter image description here