Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing nodes into columns using graphviz

I'm trying to replicate this figure using Graphviz (the figure was generated in LaTeX): enter image description here

Doing various searches and reading, I've managed to get this far:

enter image description here

I'm not interested in getting the super and subscripting. I'm prety sure I can figure that much out if I really want to. What I would like to do is make sure that the nodes are all in the 3 x 3 grid, and nicely aligned. As you can see, my production is not aligned. My code is below. (The figure was made in R using the DiagrammeR package.

So far, I've tried using [pos='1,1!',pin=true], and incrementing the position indices over the three by three grid, but it hasn't changed the result at all.

Any hints?

library(DiagrammeR)

grViz(
  "
digraph {

  graph [overlap = true, fontsize = 10]
    node [shape=circle] 
    Q11 [pos='1,1',pin=true]
    Q21 [pos='2,1',pin=true]
    Y1  [fillcolor=lightgray,style=filled,pos='3,1',pin=true]

    Q11 -> Q21
    Q11 -> Y1
    Q21 -> Y1

    node [shape = circle]
    Q12
    Q22
    Y2 [fillcolor=lightgray,style=filled]

    Q12 -> Q22
    Q12 -> Y2
    Q22 -> Y2

    node [shape = circle]
    Q13
    Q23
    Y3 [fillcolor=lightgray,style=filled]

    Q13 -> Q23
    Q13 -> Y3
    Q23 -> Y3


  {rank = same; Q11; Q12; Q13}
  Q11 -> Q12
  Q12 -> Q13

  {rank = same; Q21; Q22; Q23}
  Q21 -> Q22
  Q22 -> Q23
}
  ",
engine = 'neato')
like image 916
Benjamin Avatar asked Nov 25 '25 09:11

Benjamin


2 Answers

You should disable few constraints on the edges by adding constraint=false attribute.

digraph {
    graph [fontsize=10]
    node [shape=circle] 
    Q21
    Q22
    Q23
    Q12
    Q11
    Q13

    Q21 -> Q22 [constraint=false]
    Q22 -> Q23 [constraint=false]

    Q11 -> Q21
    Q11 -> Y1 [constraint=false]
    Q21 -> Y1

    Q12 -> Q22
    Q12 -> Y2 [constraint=false]
    Q22 -> Y2


    Q13 -> Q23
    Q13 -> Y3 [constraint=false]
    Q23 -> Y3

    {rank = same; Q11; Q12; Q13;}
    Q11 -> Q12 [constraint=false]
    Q12 -> Q13 [constraint=false]

    {rank = same; Q21; Q22; Q23}
    Y3 [fillcolor=lightgray,style=filled]
    Y2 [fillcolor=lightgray,style=filled]
    Y1 [fillcolor=lightgray,style=filled]
}

This code will generate below graph.

FIxed graph

Please check http://graphviz.it/#/LXfbjEui for working demo.

like image 159
Marcin Stefaniuk Avatar answered Nov 28 '25 00:11

Marcin Stefaniuk


I realize it's many years later, but for people coming across this, the following code does essentially what @Marcin's solution does.. but is reduced, and perhaps illustrates the technique employed, a bit better.

digraph {
    node [shape=circle]

    Q11 -> Q21 -> Y1
    Q12 -> Q22 -> Y2
    Q13 -> Q23 -> Y3

    edge [constraint=false]
    Q11 -> Q12 -> Q13
    Q21 -> Q22 -> Q23
    Q11 -> Y1
    Q12 -> Y2
    Q13 -> Y3
    Y1, Y2, Y3 [fillcolor=lightgray,style=filled]
}
like image 30
RoyM Avatar answered Nov 27 '25 22:11

RoyM



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!