Search code examples
rgraphvizdiagrammer

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')

Solution

  • 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.