# Aligning Top Edge of Clusters in dot LR

**URL:** <https://forum.graphviz.org/t/aligning-top-edge-of-clusters-in-dot-lr/886>\
**Category:** Help\
**Created:** [October 20, 2021, 5:12am UTC](https://forum.graphviz.org/t/aligning-top-edge-of-clusters-in-dot-lr/886 "2021-10-20T05:12:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James007au](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@James007au](https://forum.graphviz.org/u/James007au)\
**Post date:** [October 20, 2021, 5:12am UTC](https://forum.graphviz.org/t/aligning-top-edge-of-clusters-in-dot-lr/886/1 "2021-10-20T05:12:13Z")

</div>

Hi there,  
I’m trying to align the top edge of clusters in a LR dot graph. I’ve tried numerous combinations of hidden nodes, clusters and edges but so far have not achieved my goal. I’ve also tried ordering=in | out, compound=true, newrank=true, outputorder=nodesfirst, pack=true, packmode=“graph”, packmode=“array” & sortv,

The best I’ve managed to get is the following (ZZ, 00, 10, 20, 30 and their respective clusters will be invisible, cluster\_n will be invisible).

![Graphviz 20.10.21](https://global.discourse-cdn.com/graphviz/original/1X/bf5902992c8211c1378b9564aaa78a7d73ed264b.jpeg)

As per the following. Please note that I won’t be able to control the order of edges between A[A-n], B[A-n]; C[A-n] and D[A-n]:  
strict digraph J  
{  
rankdir=LR;  
splines=curved;  
ZZ [group=dummy];  
subgraph “cluster\_0”  
{  
rankdir=TB;  
subgraph “cluster\_00”  
{  
00 [group=dummy];  
}  
subgraph “cluster\_01”  
{  
AA;  
AB;  
AC;  
AD;  
}  
}  
subgraph “cluster\_1”  
{  
rankdir=TB;  
subgraph “cluster\_10”  
{  
10 [group=dummy];  
}  
subgraph “cluster\_11”  
{  
BA;  
BB;  
BC;  
BD;  
BE;  
BF;  
BG;  
BH;  
}  
}  
subgraph “cluster\_2”  
{  
rankdir=TB;  
subgraph “cluster\_20”  
{  
20 [group=dummy];  
}  
subgraph “cluster\_21”  
{  
CA;  
CB;  
CC;  
CD;  
CE;  
CF;  
CG;  
}  
}  
subgraph “cluster\_3”  
{  
rankdir=TB;  
subgraph “cluster\_30”  
{  
30 [group=dummy];  
}  
subgraph “cluster\_31”  
{  
DA;  
DB;  
DC;  
DD;  
}  
}  
00-\>ZZ [weight=2];  
10-\>ZZ [weight=2];  
20-\>ZZ [weight=2];  
30-\>ZZ [weight=2];  
00-\>10 [weight=6];  
10-\>20 [weight=6];  
20-\>30 [weight=6];  
AA-\>BA;  
AA-\>BB;  
AA-\>BC;  
AA-\>CE;  
AB-\>BD;  
AB-\>BE;  
AB-\>CC;  
AC-\>BG;  
AC-\>BH;  
AC-\>CA;  
AC-\>CB;  
AC-\>DB;  
BA-\>CD;  
BB-\>CD;  
BC-\>CD;  
BF-\>CG;  
AD-\>CF;  
BH-\>DC;  
BD-\>DD;  
BE-\>DD;  
CD-\>DA;  
CG-\>DA;  
}

Kind regards and thanks in advance.

James.

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [October 20, 2021, 4:01pm UTC](https://forum.graphviz.org/t/aligning-top-edge-of-clusters-in-dot-lr/886/2 "2021-10-20T16:01:26Z")

</div>

- Do you also want to “order of edges between A[A-n], B[A-n]; C[A-n] and D[A-n]”?
- Also, only one **rankdir** per entire graph. As I remember, the last **rankdir** wins

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [October 20, 2021, 4:19pm UTC](https://forum.graphviz.org/t/aligning-top-edge-of-clusters-in-dot-lr/886/3 "2021-10-20T16:19:45Z")

</div>

[when providing source code, please us the “preformatted text” or do this ``` stuff ```]  
Most of the changes are just reformatting. **constraint=false** does all the heavy lifting (more than I expected).

```auto
strict digraph J
{
  rankdir=LR; // sorry, rankdir applies to entire graph
  splines=curved;
  ZZ [group=dummy];
  subgraph cluster_0 {
    rankdir=TB; // sorry, rankdir applies to entire graph
    subgraph cluster_00{
      00 [group=dummy];
    }
    subgraph cluster_01{
      AA;
      AB;
      AC;
      AD;
    }
  }
  subgraph cluster_1{
    rankdir=TB; // sorry, rankdir applies to entire graph
    subgraph cluster_10{
      10 [group=dummy];
    }
    subgraph cluster_11{
      BA;
      BB;
      BC;
      BD;
      BE;
      BF;
      BG;
      BH;
    }
  }
  subgraph cluster_2{
    rankdir=TB; // sorry, rankdir applies to entire graph
    subgraph cluster_20{
      20 [group=dummy];
    }
    subgraph cluster_21{
      CA;
      CB;
      CC;
      CD;
      CE;
      CF;
      CG;
    }
  }
  subgraph cluster_3{
    rankdir=TB; // sorry, rankdir applies to entire graph
    subgraph cluster_30{
      30 [group=dummy];
    }
    subgraph cluster_31{
      DA;
      DB;
      DC;
      DD;
    }
  }

00->10->20->30->ZZ

// allign the clusters AND order the nodes within the clusters (surprised me)
edge [constraint=false] 
00->ZZ 
10->ZZ 
20->ZZ 
30->ZZ 

AA->BA;
AA->BB;
AA->BC;
AA->CE;
AB->BD;
AB->BE;
AB->CC;
AC->BG;
AC->BH;
AC->CA;
AC->CB;
AC->DB;
BA->CD;
BB->CD;
BC->CD;
BF->CG;
AD->CF;
BH->DC;
BD->DD;
BE->DD;
CD->DA;
CG->DA;
}

```

Giving:

 ![alignClusters1](https://global.discourse-cdn.com/graphviz/original/1X/c3e7b04eb01d94f00c026d1aa8a498bbb69392b7.png)

---

<div class="post-metadata">

**Author:** ![James007au](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@James007au](https://forum.graphviz.org/u/James007au)\
**Post date:** [October 21, 2021, 2:47am UTC](https://forum.graphviz.org/t/aligning-top-edge-of-clusters-in-dot-lr/886/4 "2021-10-21T02:47:01Z")

</div>

Many thanks for your quick and comprehensive responses to my question - much appreciated. Of course you are right with “rankdir” - I suspect I was trying just about everything and forgot to remove it before uploading.  
I guess I have a choice to make regarding the use of “constraint” - either have the top edges of the clusters aligned or allow graphviz to reorder A[A-n], etc to minimise path lengths and crossing between them.  
Again, thanks for your time and consideration of my issue.  
James.
