4
votes

I have several related sub-graphs that I want to draw together in GraphViz. When I draw simple nodes it looks quite pretty:

enter image description here

Source:

digraph {
  rankdir=LR;

  A1 -> A21;
  A1 -> A22;
  A1 -> A23;
  A1 -> A24;

  B1 -> B21;
  B1 -> B22;
  B1 -> B23;
  B1 -> B24;

  A21 -> A31;
  A22 -> A31;
  A23 -> A31;

  A23 -> A32;

  B21 -> B31;

  B21 -> B32;
  B22 -> B32;

  B21 -> B33;
  B23 -> B33;
}

As the nodes in the same level across the several sub-graphs are related, I want to group them and give it a label. I tried to do it using clusters, but it "squeezes" the nodes:

enter image description here

Source:

digraph {
  rankdir=LR;

  subgraph cluster_level1 {
    label = "Level 1";
    style=filled;
    color=lightgrey;

    A1;
    B1;
  }

  subgraph cluster_level2 {
    label = "Level 2";
    style=filled;
    color=lightgrey;

    A21;
    A22;
    A23;
    A24;

    B21;
    B22;
    B23;
    B24;
  }

  subgraph cluster_level3 {
    label = "Level 3";
    style=filled;
    color=lightgrey;

    A31;
    A32;

    B31;
    B32;
    B33;
  }

  A1 -> A21;
  A1 -> A22;
  A1 -> A23;
  A1 -> A24;

  B1 -> B21;
  B1 -> B22;
  B1 -> B23;
  B1 -> B24;

  A21 -> A31;
  A22 -> A31;
  A23 -> A31;

  A23 -> A32;

  B21 -> B31;

  B21 -> B32;
  B22 -> B32;

  B21 -> B33;
  B23 -> B33;
}

With just two sub-graphs, it's bad, but still not horrible. However, if I add more sub-graphs, it becomes uglier and uglier.

Is there a way to group nodes with some shadowing and labels, while keeping the original nodes layout using GraphViz?

1
Whenever I have had problems with dot, I have found the best approach is to post here; graphviz.org/forum - Ian A. Mason

1 Answers

3
votes

This is probably not a good answer since it implies a lot of trial and error but at least you get what you want (I guess) with invisible nodes:

digraph {
  rankdir=LR;

  subgraph cluster_level1 {
    label = "Level 1";
    style=filled;
    color=lightgrey;
    A01[ style = invis ];
    A1;
    A02[ style = invis ];
    A03[ style = invis ];
    A06[ style = invis ];
    A05[ style = invis ];
    B1;
    A04[ style = invis ];
  }

  subgraph cluster_level2 {
    label = "Level 2";
    style=filled;
    color=lightgrey;

    A21;
    A22;
    A23;
    A24;

    B21;
    B22;
    B23;
    B24;
  }

  subgraph cluster_level3 {
    label = "Level 3";
    style=filled;
    color=lightgrey;

    A07[ style = invis ];
    A31;
    A32;
    A08[ style = invis ];            

    B31;
    B32;
    B33;
    A01[ style = invis ];
    A09[ style = invis ];
  }

  A1 -> A21;
  A1 -> A22;
  A1 -> A23;
  A1 -> A24;

  B1 -> B21;
  B1 -> B22;
  B1 -> B23;
  B1 -> B24;

  A21 -> A31;
  A22 -> A31;
  A23 -> A31;

  A23 -> A32;

  B21 -> B31;

  B21 -> B32;
  B22 -> B32;

  B21 -> B33;
  B23 -> B33;
}

yields

enter image description here