# Set nodes from left to right, and other - from top to bottom on the same rank

I need rankdir=LR on graph, but TB on subgraph (or even mixed rankdir, I don’t know…).
I also need to put some nodes to a cluster with a background color (grey).
How can I achieve it?

1. Here is the best what I’ve managed to achieve:
`````` digraph {rankdir=LR;node[style=filled, fillcolor=white]
1 -> 2
1 -> 3

2 -> 4
2 -> 5
2 -> 6
2 -> 7
2 -> 8
2 -> 9

subgraph cluster1 {bgcolor=grey;

4 -> 5 [constraint=false;]
5 -> 6 [constraint=false;]
6 -> 7 [constraint=false;]
7 -> 8 [constraint=false;]
8 -> 9 [constraint=false;]

11 [fillcolor=violet]
12 [fillcolor=lightgrey]
13 [fillcolor=lightgreen]
14 [fillcolor=magenta]

6 -> 10
6 -> 11

4 [fillcolor=yellow]
5 [fillcolor=red]
6 [fillcolor=lightblue]
7 [fillcolor=orange]
8 [fillcolor=lime]
9 [fillcolor=pink]

10 -> 12
10 -> 13
10 -> 14

12 -> 13 [constraint=false;]
13 -> 14 [constraint=false;]
}

14 -> 15
}
``````

The only problem is “constraint=false” which sometimes(!) gives wrong results: nodes 12 → 13 → 14 go in wrong order (from bottom to top) without respecting direction of edges (I understand I disabled it).
But I strictly need = from top to bottom based on the way the edges are defined.
If I remove “constraint=false” the graph becomes awful:

Ideally, I want to define the direction myself (either from up to down on the same rank, or from left to right on the next rank) for each edge from one node to another so that nodes respect direction of the edges and nodes 12 → 13 → 14 have correct order on a graph (i.e. without “constraint=false”).

1. I also tried rank=same for subgraph (not cluster) with splitting nodes 4…9, 10-11, 12-14 to three subgraphs but failed to get good result - I didn’t find how to set background color for it.

This result is about to be perfect except subgraph-background color.
Is it possible to set it here for three subgraphs?

`````` digraph {rankdir=LR;node[style=filled, fillcolor=white]
1 -> 2
1 -> 3

2 -> 4
2 -> 5
2 -> 6
2 -> 7
2 -> 8
2 -> 9

subgraph clus_ter1 {bgcolor=grey;rank=same;
4 [fillcolor=yellow]
5 [fillcolor=red]
6 [fillcolor=lightblue]
7 [fillcolor=orange]
8 [fillcolor=lime]
9 [fillcolor=pink]
4 -> 5
5 -> 6
6 -> 7
7 -> 8
8 -> 9

}

subgraph clus_ter2 {bgcolor=orange;rank=same;
10
11 [fillcolor=violet]
10 -> 11
}
10 -> 12
10 -> 13
10 -> 14

subgraph clus_ter3 {bgcolor=yellow;rank=same;
12 [fillcolor=lightgrey]
13 [fillcolor=lightgreen]
14 [fillcolor=magenta]

}
12 -> 13
13 -> 14

6 -> 10
6 -> 11

14 -> 15
}
``````

This is a common request and to-date there is no guaranteed “fix”.
After many failed attempts, here is a pretty clean solution. It uses constraint=false, rank=same, and the group attributes.
[the input has also been edited to make it easier for me to read]

``````digraph {
rankdir=LR;
node[style=filled, fillcolor=white]

// success by using both group & constraint

subgraph cluster1 {
bgcolor=grey;

4 [fillcolor=yellow]
5 [fillcolor=red]
6 [fillcolor=lightblue]
7 [fillcolor=orange]
8 [fillcolor=lime]
9 [fillcolor=pink]
11 [fillcolor=violet group=S]

14 [fillcolor=magenta group=S]
13 [fillcolor=lightgreen]
12 [fillcolor=lightgrey]

{rank=same
edge [constraint=false]
4 -> 5 -> 6 -> 7  -> 8 -> 9
}
{rank=same
10 11
}
{rank=same
edge [constraint=false]
12 -> 13 -> 14
}

6 -> 10
6 -> 11
10 -> 12
10 -> 13
10 -> 14
}

1 -> 2
1 -> 3
2 -> 4
2 -> 5
2 -> 6
2 -> 7
2 -> 8
2 -> 9
14 -> 15
}
``````

Giving:

2 Likes

Excellent!