Padding between cluster boundaries and nodes when using Graphviz and neato
Asked Answered
M

2

6

I want to generate the following graph in Graphviz:

Desired layout

For reasons explained here, this:

digraph
{
   layout=dot;
   rankdir="LR";
   overlap = true;
   node[shape=record, height="0.4", width="0.4"];
   edge[dir=none];

   A; B; C; D; E; F; G; H; I;

   A -> B -> C;
   D -> E -> F;
   G -> H -> I;
   edge[constraint=false];
   A -> D -> G;

   subgraph clusterX
   {
      label="Cluster 1";
      A; B;
   }

   subgraph clusterY
   {
      label="Cluster 2";
      E; F; H; I;
   }

}

produces this:

Not desired layout

Following some careful tweaking of the order of appearance of nodes:

   F; E; I; H; D; G; A; B; C;

I get the correct result.

While this works, I would like more direct control over the placement of nodes, so I tried switching to neato so that I can force the node locations using pos:

graph g
{
   layout=neato;
   node[shape=record, height="0.4", width="0.4"];
   edge[dir=none];

   A [pos="1,3!"];
   B [pos="2,3!"];
   C [pos="3,3!"];
   D [pos="1,2!"];
   E [pos="2,2!"];
   F [pos="3,2!"];
   G [pos="1,1!"];
   H [pos="2,1!"];
   I [pos="3,1!"];

   A -- B -- C;
   D -- E -- F;
   G -- H -- I;
   A -- D -- G;

   subgraph clusterX
   {
      label="Cluster 1";
      A;
      B;
   }

   subgraph clusterY
   {
      label="Cluster 2";
      E; F; H; I;
   }
}

This gives the following result:

enter image description here

How can I get neato to add padding between the cluster boundaries and the nodes within the cluster (in the same way that dot does)?

Mcentire answered 26/3, 2012 at 11:12 Comment(0)
G
4

I hate to be a party pooper, but I don't think the neato-with-fixed-positions-and-clusters approach will be successful.

Cluster support depends on the layout engine - not all engines support it to the same degree:

Note that, for good and bad, cluster subgraphs are not part of the DOT language, but solely a syntactic convention adhered to by certain of the layout engines.

Neato does not seem to be a part of the engines supporting clusters, and while fdp does support neato like layout, it does not support fixed positions.

In the above linked forum entries, ERG proposes at some point to use a gvpr script to achieve this - probably not the solution you had in mind.

By the way, the graph should not be a directed graph, I get warnings and replaced all -> with --.

Galangal answered 27/3, 2012 at 19:28 Comment(1)
Thanks @marapet. It looks like I'm going to have to change my approach here. I appreciate the information and links that you provided. I have updated the question to remove the directed edges.Mcentire
C
2

Add a surrounding cluster around each of your clusters, set its style to "invis" and set its margin to the additional space you want between clusters. your dot file would be as follows :

digraph
{
   layout=dot;
   rankdir="LR";
   overlap = true;
   node[shape=record, height="0.4", width="0.4"];
   edge[dir=none];

   A; B; C; D; E; F; G; H; I;

   A -> B -> C;
   D -> E -> F;
   G -> H -> I;
   edge[constraint=false];
   A -> D -> G;

   subgraph clusterX_margin
   {
     style=invis
     margin=20.0
     subgraph clusterX
     {
        label="Cluster 1";
        A; B;
     }
   }

   subgraph clusterY_margin
   {
     style=invis
     margin=20.0
     subgraph clusterY
     {
        label="Cluster 2";
        E; F; H; I;
     }
   }
}
Chassepot answered 23/7, 2021 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.