50

I would like these two nodes to appear on the same level:

enter image description here

digraph G {
    subgraph cluster1 {
        label="Local Datacenter";
        router1;
        host1;
    }
    subgraph cluster2 {
        label="Remote Datacenter";
        router2;
        host2;
    }
    router1 -> router2;
    router2 -> host2;
    router1 -> host1;
}

I have tried using rank=same and rank=min, but they aren't giving me what I need.

Interestingly, if I set rankdir=LR and comment out the two router-to-host edges, it gives me exactly the look I want - but I would like to leave the edges intact.

Potherca
  • 13,207
  • 5
  • 76
  • 94
tinypigdotcom
  • 503
  • 1
  • 4
  • 4

2 Answers2

101

You may use the newrank graph attribute (added in GraphViz 2.30) to activate the new ranking algorithm which allows defining rank=same for nodes which belong to clusters.

Add the following line at the top:

newrank=true;

Add the following line after the cluster definitions:

{ rank=same; router1; router2; }

Here's the resulting graph:

routers with same rank

dubek
  • 11,447
  • 5
  • 30
  • 23
  • 11
    This undocumented `newrank` thing also solved my (related but different) problem -- trying to get dot to put clusters on different rows, by putting an invisible edge between nodes in the clusters. Without `newrank`, this works as expected... *provided* you define "ordinary" subgraphs (whose names don't begin with "cluster"). If you make them clusters, suddenly dot doesn't mind having edges between nodes on the same rank! Ugh. – j_random_hacker Dec 17 '13 at 18:00
  • 2
    This is much more intuitive, and with predictable results than the `constraint=false` method. Thanks for this. – DannyB Sep 14 '17 at 09:17
  • 2
    `constraint = false` doesn't work for big graphs. It puts it somewhere in siberia and not right next to it. – jason Feb 26 '19 at 01:10
  • The really very sparse explanation in the official [`rank`](https://graphviz.org/docs/attrs/rank/) documentation refers for the details to the answer to the SO question [*Rank attribute is confusing to me*](https://stackoverflow.com/questions/6149834/rank-attribute-is-confusing-to-me). – Wolf Aug 30 '22 at 11:07
41

You may simply modify the edge between the routers:

router1 -> router2[constraint=false];

constraint indicates whether the edge should be used in the ranking of the nodes.

marapet
  • 54,856
  • 12
  • 170
  • 184