# Keep rankdir above direction

**URL:** <https://forum.graphviz.org/t/keep-rankdir-above-direction/1019>\
**Category:** Help\
**Created:** [February 4, 2022, 3:39am UTC](https://forum.graphviz.org/t/keep-rankdir-above-direction/1019 "2022-02-04T03:39:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![agunicat](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@agunicat](https://forum.graphviz.org/u/agunicat)\
**Post date:** [February 4, 2022, 3:39am UTC](https://forum.graphviz.org/t/keep-rankdir-above-direction/1019/1 "2022-02-04T03:39:46Z")

</div>

Hi all,

I’m trying to fit a “mirrored” graph side by side.  
this works when the arrow direction is going to the same side like

 ![graphviztp1](https://global.discourse-cdn.com/graphviz/original/1X/8d26c2b775ee0089fffcbdbf16298111552a38e7.png)

the problem happens when the middle node shifts. the other half then goes to the side

 ![graphviztp2](https://global.discourse-cdn.com/graphviz/original/1X/49e0fce52d4e65058ee248d507580076c074b890.png)

i want the graph to keep LR ranking even though the arrow direction is reversed

---

<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:** [February 4, 2022, 5:01am UTC](https://forum.graphviz.org/t/keep-rankdir-above-direction/1019/2 "2022-02-04T05:01:04Z")

</div>

Please provide your input.

---

<div class="post-metadata">

**Author:** ![agunicat](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@agunicat](https://forum.graphviz.org/u/agunicat)\
**Post date:** [February 5, 2022, 4:06am UTC](https://forum.graphviz.org/t/keep-rankdir-above-direction/1019/3 "2022-02-05T04:06:28Z")

</div>

oh sorry

1st

```auto
strict digraph {
rankdir="LR"
splines=spline
d -> f
l -> n
d -> e
p -> w
q -> w
b -> f
p -> v
q -> v
f -> l
c -> g
s -> v
l -> m
j -> m
a -> f
i -> m
r -> v
j -> n
r -> u
i -> n
s -> u
r -> w
t -> u
e -> i
c -> e
c -> f
k -> m
b -> g
o -> p
e -> j
o -> q
f -> j
o -> t
f -> h
e -> h
h -> n
e -> l
f -> k
t -> w
o -> r
m -> o
p -> u
o -> s
d -> g
n -> o
g -> h
g -> j
k -> n
s -> w
f -> i
t -> v
e -> k
a -> e
b -> e
h -> m
}

```

2nd

```auto
strict digraph {
rankdir="LR"
splines=spline
d -> f
r -> o
l -> n
u -> t
d -> e
b -> f
w -> t
f -> l
c -> g
l -> m
w -> r
j -> m
a -> f
v -> r
i -> m
v -> p
j -> n
w -> s
i -> n
w -> q
w -> p
e -> i
c -> e
c -> f
k -> m
b -> g
v -> s
e -> j
u -> r
p -> o
f -> j
f -> h
e -> h
h -> n
u -> s
q -> o
e -> l
o -> n
t -> o
f -> k
u -> p
d -> g
o -> m
g -> j
k -> n
g -> h
s -> o
f -> i
e -> k
v -> t
a -> e
b -> e
v -> q
h -> m
}

```

---

<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:** [February 5, 2022, 4:33pm UTC](https://forum.graphviz.org/t/keep-rankdir-above-direction/1019/4 "2022-02-05T16:33:27Z")

</div>

Unfortunately, you got caught by how ranks work. Your ranks flow left-to-right, so **dot** positioned your node accordingly. And only one **rankdir** per graph. (Yes, it would be nice apply **rankdir** at a cluster-level - [[Dot] Allow different rankdir for subgraph cluster (#887) · Issues · graphviz / graphviz · GitLab](https://gitlab.com/graphviz/graphviz/-/issues/887)).  
So, a fix is to swap head & tail of the “mirrored” edges and then add dir=back attribute ([dir | Graphviz](https://graphviz.org/docs/attrs/dir/)) to those edges to make it appear as you want.  
Here is the gawk script used to modify the input:  
`gawk '$1~/^[o-z]$/{t=$1;$1=$3;$3=t " [dir=back]"}{print}'`  
Here is the result:

```auto
strict digraph {
rankdir="LR"
splines=spline
d -> f
o -> r [dir=back]
l -> n
t -> u [dir=back]
d -> e
b -> f
t -> w [dir=back]
f -> l
c -> g
l -> m
r -> w [dir=back]
j -> m
a -> f
r -> v [dir=back]
i -> m
p -> v [dir=back]
j -> n
s -> w [dir=back]
i -> n
q -> w [dir=back]
p -> w [dir=back]
e -> i
c -> e
c -> f
k -> m
b -> g
s -> v [dir=back]
e -> j
r -> u [dir=back]
o -> p [dir=back]
f -> j
f -> h
e -> h
h -> n
s -> u [dir=back]
o -> q [dir=back]
e -> l
n -> o [dir=back]
o -> t [dir=back]
f -> k
p -> u [dir=back]
d -> g
m -> o [dir=back]
g -> j
k -> n
g -> h
o -> s [dir=back]
f -> i
e -> k
t -> v [dir=back]
a -> e
b -> e
q -> v [dir=back]
h -> m
}

```

Giving:

 ![rankdirAndArrowDirB1](https://global.discourse-cdn.com/graphviz/original/1X/506961842f93dfd4bb033102119957ee8f3e957a.png)

---

<div class="post-metadata">

**Author:** ![agunicat](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@agunicat](https://forum.graphviz.org/u/agunicat)\
**Post date:** [February 7, 2022, 6:28pm UTC](https://forum.graphviz.org/t/keep-rankdir-above-direction/1019/5 "2022-02-07T18:28:06Z")

</div>

omg!! that was awesome!!  
thank you so much for your help!
