graphvizdot

How to place a set of nodes over another set of nodes in dot graph


I am trying to go through the dot language for drawing directed graphs. While trying to get a grip on the ways to correctly lay the nodes in the graphs, I am trying to place a set of nodes and arrange them relative to each other. Somewhat akin to creating building blocks (placing one brick over two staggered ones).

I am trying to this as:

   digraph {
   ranksep=.05; 
   rankdir=RL;
   B -> A [style=invis];
   C -> B [style=invis];
   E -> W [style="invis"];
   O -> P [style=invis];
   R -> E [style=invis];

   A[shape=star,color=yellowgreen,style=filled,label="S"];    
   B[shape=star,color=yellowgreen,style=filled,label="U"];
   C[shape=star,color=yellowgreen,style=filled,label="N"];
    
   P,O,W,E,R [shape=house,color=brown,style=filled];

   {rank=same; A; O};
   {rank=same; B; W};
   {rank=same; C; E};
   }

I am trying to draw the graph in a way so that the nodes A, B and C should come first (i.e. above the rest of the nodes P,O,W,E,R.

Can somebody guide me the correct way to do this?

The result I am currently getting is:

enter image description here


Solution

  • I see two ways of doing this:

    Method 1

       digraph {
       ranksep=.15; 
       rankdir=RL;
       U -> S [style=invis];
       N -> U [style=invis];
       S -> O [style=invis];
       U -> W [style=invis];
       N -> E [style=invis];
       O -> P [style=invis];
       R -> E [style=invis];
    
       S[shape=star,color=lightgoldenrod,style=filled];
       U[shape=star,color=lightgoldenrod,style=filled];
       N[shape=star,color=lightgoldenrod,style=filled];
    
       P,O,W,E,R [shape=house,color=brown,style=filled];
    
       {rank=same; S; O};
       {rank=same; U; W};
       {rank=same; N; E};
       }
    

    Method 1

    Another way to do that would be:

       digraph G {
       rankdir=TB;
         subgraph cluster_1 { N; U; S; penwidth=0 }
            
         subgraph cluster_2 { R; E; W; O; P; penwidth=0 }
    
         N -> E [style=invis];
         U -> W [style=invis];
         S -> O [style=invis];
    
         S, U, N [shape=star,color=yellowgreen,style=filled];
         P, O, W, E, R [shape=house,color=brown,style=filled];
    
       }
    

    Method 2

    The second method is more compact but the result is somewhat different.