Search code examples
graphviz

Align nodes in a Graphviz directed graph


I have the following Graphviz code:

digraph {
  Technique [shape = box];
  Path [shape = box];
  KnowledgeObservation [shape = box, label = "Knowledge\nObservation"];
  ManagementIntervention [shape = box, label = "Management\nIntervention"];
  ResultsModification [shape = box, label = "Results\nModification"];
  SharedCode [label = "Shared Code"];
  MediatedRelationship [label = "Mediated Relationship"];
  Art -> Technique;
  Therapy -> Path;
  {Technique Path} -> KnowledgeObservation -> ManagementIntervention -> ResultsModification;
  {MediatedRelationship SharedCode} -> {KnowledgeObservation ResultsModification}

  subgraph {
    rank = same
    Technique -> Path [dir = none]
  }

  subgraph {
    rank = same
    SharedCode
    ManagementIntervention
    MediatedRelationship     
  }
}

It currently produces the following output:

Graphviz

How can I vertically align "Management Intervention" with both "Knowledge Observation" and "Results Modification"?

"Shared Code" should be moved to the left of "Management Intervention".

"Mediated Relationship" should be moved to the right of "Management Intervention".

"Shared Code", "Management Intervention" and "Mediated Relationship" should stay horizontally aligned.

How can I accomplish this?


Solution

  • This can be achieved without subgraphs; the most important modification is the line

    { rank = same; SharedCode -> ManagementIntervention -> MediatedRelationship[ style = invis ] }
    

    which keeps the three nodes not only on the right level, but also within the desired order.

    Altogether, this code here

    digraph 
    {
      // node definition
      Art Therapy;
      Technique[ shape = box ];
      Path[ shape = box ];
      KnowledgeObservation[ shape = box, label = "Knowledge\nObservation" ];
      ManagementIntervention[ shape = box, label = "Management\nIntervention" ];
      ResultsModification[ shape = box, label = "Results\nModification" ];
      SharedCode[ label = "Shared Code" ];
      MediatedRelationship[ label = "Mediated Relationship" ];
    
      // edges
      Art -> Technique;
      Therapy -> Path;
      { rank = same; Technique -> Path [dir = none] }
      { Technique Path} -> KnowledgeObservation -> ManagementIntervention -> ResultsModification;
      { rank = same; SharedCode -> ManagementIntervention -> MediatedRelationship[ style = invis ] } 
    
      { MediatedRelationship SharedCode } -> { KnowledgeObservation ResultsModification }
    }
    

    gives you

    enter image description here

    which is, in my understanding, what you are looking for.

    Still I would recommend to replace the last line of code with these three

    KnowledgeObservation -> { SharedCode MediatedRelationship }[ dir = back ];
    SharedCode -> ResultsModification;
    MediatedRelationship -> ResultsModification;
    

    Reason is that once your graph gets more complicated, graphviz will recognize and maintain the hierarchical relationships, rather than interpreting ambiguous instructions in surprising ways.