Search code examples
graphvizdot

Order of nodes in graphviz


I am working on this graph:

digraph "example.gsn.yaml" {

  ## Elements
  "G1" [shape="box", label=<<B>G1</B><BR align="left"/>Goal 1>];
  "G2" [shape="box", label=<<B>G2</B><BR align="left"/>Goal 2>];
  "G4" [shape="box", label=<<B>G4</B><BR align="left"/>Goal 4>];
  "G3" [shape="box", label=<<B>G3</B><BR align="left"/>Goal 3>];
  "C1" [shape="box", style="rounded", label=<<B>C1</B><BR align="left"/>Context 1>];
  "S1" [shape="parallelogram", label=<<B>S1</B><BR align="left"/>Strategy 1>];
  "A1" [shape="oval", label=<<B>A1</B><BR align="left"/>Argument 1>];
  "A1":e -> "A1":e [headlabel=< <B>A</B> >, labeldistance=2.5, penwidth=0, arrowhead=none];
  "Sn1" [shape="circle", label=<<B>Sn1</B><BR align="left"/>Solution 1>];
  "Sn2" [shape="circle", label=<<B>Sn2</B><BR align="left"/>Solution 2>];
  "J1" [shape="oval", label=<<B>J1</B><BR align="left"/>Justification 1>];
  "J1":e -> "J1":e [headlabel=< <B>J</B> >, labeldistance=2.5, penwidth=0, arrowhead=none];

  ## Relations
  "G1" -> "G2";
  "G1" -> "G3";
  "G1" -> "G4";
  "G2" -> "S1";
  "G4" -> "Sn2";
  "G3" -> "Sn1";
  "G3" -> "C1" [arrowhead=empty];
  {rank = same; "G3"; "C1"; }
  "S1" -> "Sn1";
  "S1" -> "J1" [arrowhead=empty];
  "S1" -> "A1" [arrowhead=empty];
  {rank = same; "A1"; "S1"; }
  {rank = same; "S1"; "J1"; }
}


This is my current graph. I would like to minimize crossings. My preferred layout would be that A1, S1 and J1 are on the same rank in a left-to-right order. However I cannot find out how to do this.

Can anybody help, please? Thanks in advance.


Solution

  • Many "obvious" solutions did not work (ordering, weight, invisible edges). Putting A1, J1, and S1 inside a cluster does the trick:

    digraph "example.gsn.yaml" {
      ## Elements
      "G1" [shape="box", label=<<B>G1</B><BR align="left"/>Goal 1>];
      "G2" [shape="box", label=<<B>G2</B><BR align="left"/>Goal 2>];
      "G4" [shape="box", label=<<B>G4</B><BR align="left"/>Goal 4>];
      "G3" [shape="box", label=<<B>G3</B><BR align="left"/>Goal 3>];
      "C1" [shape="box", style="rounded", label=<<B>C1</B><BR align="left"/>Context 1>];
      "S1" [shape="parallelogram", label=<<B>S1</B><BR align="left"/>Strategy 1>];
      "A1" [shape="oval", label=<<B>A1</B><BR align="left"/>Argument 1>];
      "A1":e -> "A1":e [headlabel=< <B>A</B> >, labeldistance=2.5, penwidth=0, arrowhead=none];
      "Sn1" [shape="circle", label=<<B>Sn1</B><BR align="left"/>Solution 1>];
      "Sn2" [shape="circle", label=<<B>Sn2</B><BR align="left"/>Solution 2>];
      "J1" [shape="oval", label=<<B>J1</B><BR align="left"/>Justification 1>];
      "J1":e -> "J1":e [headlabel=< <B>J</B> >, labeldistance=2.5, penwidth=0, arrowhead=none];
    
      subgraph clusterg1{
        graph[peripheries=0]  // no box around the cluster
        {rank = same; node[group=g1] edge[style=invis]  "A1" -> "S1" ->  "J1"}
      }
    
      ## Relations
      "G1" -> "G2";
      "G1" -> "G3";
      "G1" -> "G4";
      "G2" -> "S1";
      "G4" -> "Sn2";
      "G3" -> "Sn1";
      "G3" -> "C1" [arrowhead=empty];
      {rank = same; "G3"; "C1"; }
      "S1" -> "Sn1";
      "S1" -> "J1" [arrowhead=empty];
      "A1" -> "S1" [arrowhead=empty dir=back];
    }
    

    enter image description here

    p.s. is the A1->S1 arrow correct?