Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphviz: using rankdir in subgraph?

Tags:

graphviz

dot

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!

like image 452
LearnDude Avatar asked Feb 03 '26 11:02

LearnDude


1 Answers

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

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

  • break the graph into separate pieces with different rankdir values, run each through dot, and then "glue them back together": using the gvpack program (https://www.graphviz.org/pdf/gvpack.1.pdf) plus neato -n2 (https://graphviz.org/faq/#FaqDotWithCoords)
  • or accept a single rankdir value and "fake" the other rankdir values by using other dot features, like group, rank, and/or invisible edges. See below:
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

like image 86
sroush Avatar answered Feb 05 '26 09:02

sroush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!