Search code examples
graphvizsubgraph

How to make a subgraph visible?


In the graph below the two subgraphs are not visible, but instead all nodes seem to be placed randomly. How can I create the visible subgraphs, like one box with my PlayerChars inside, and the other with the NonPlayerChars inside?

digraph "All Characters" { 
  subgraph PlayerChars {
    label = "Player Characters";
    node [style=filled,color=yellow];
    Char1 -> Char2 [ label = "is sister of" ];
    Char1 -> Char2 [ label = "is brother of" ];
    label = "PCs";
  }
  subgraph NonPlayerChars {
    label = "Non-Player Characters";
    Person1 -> Char2 [label="hates"];
    Char2 -> Person1 [label="is indifferent"];
    Person2 -> Char2 [label="stole from"];
    Person1 -> Person2 [label="is father of"];
    Person2 -> Person1 [label="is daughter of"];
    Char1 -> Person2 [label="is in love with"];
    Person2 -> Char1 [label="is annoyed by"];
  }
}

this is NOT how it should look like...


Solution

  • What you are looking for is called a "cluster" in Graphviz. Use the special subgraph names cluster_ to draw the subgraphs in rectangular regions. E.g. from http://www.graphviz.org/Gallery/directed/cluster.html

    digraph G {
    
        subgraph cluster_0 {
            style=filled;
            color=lightgrey;
            node [style=filled,color=white];
            a0 -> a1 -> a2 -> a3;
            label = "process #1";
        }
    
        subgraph cluster_1 {
            node [style=filled];
            b0 -> b1 -> b2 -> b3;
            label = "process #2";
            color=blue
        }
        start -> a0;
        start -> b0;
        a1 -> b3;
        b2 -> a3;
        a3 -> a0;
        a3 -> end;
        b3 -> end;
    
        start [shape=Mdiamond];
        end [shape=Msquare];
    }
    

    enter image description here