Search code examples
rgraphvizdiagrammer

graphviz / DiagrammeR - subgraphs top to bottom, then left to right


I'm trying to recreate a diagram where I have 5 different subgraphs or "clusters." I want to have two "columns" of subgraphs, with the first column having three subgraphs and the second having second. This is the code I've written so far, but the output isn't what I want:

grViz("
      digraph G {
      rankdir = LR
      edge[style = invis]
      node[fontname = 'helvetica', width = 1.5, height = 0.5, fontsize = 20]
      graph[fontname = 'helvetica', fontsize = 20]

      subgraph cluster_one {

      node[shape = box, style = filled, fillcolor = lightblue];
      label = '30 days post-procedure';
      style = dashed;
      fontname = 'helvetica-bold';
      rank = same;

      node[shape = box, style = filled, color = black];
      tab6 [label = 'FU within window (n=@@1-1)']
      tab7 [label = 'FU outside window (n=@@1-2)']
      tab8 [label = 'Missed visit (n=@@1-3)']
      tab9 [label = 'Exit study prior to visit (n=@@1-4)']
      tab10 [label = 'Visit pending (n=@@1-5)']
      tab11 [label = 'Unknown (n=@@1-6)']

      tab6 -> tab8
      tab7 -> tab11

      }

      subgraph cluster_two {

      node[shape = box, style = filled, fillcolor = lightblue];
      label = '1 year post-procedure';
      style = dashed;
      fontname = 'helvetica-bold';
      rank = same;

      node[shape = box, style = filled, color = black];
      tab12 [label = 'FU within window (n=@@2-1)']
      tab13 [label = 'FU outside window (n=@@2-2)']
      tab14 [label = 'Missed visit (n=@@2-3)']
      tab15 [label = 'Exit study prior to visit (n=@@2-4)']
      tab16 [label = 'Visit pending (n=@@2-5)']
      tab17 [label = 'Unknown (n=@@2-6)']

      tab12 -> tab14
      tab13 -> tab17

      }

      subgraph cluster_three {

      node[shape = box, style = filled, fillcolor = lightblue];
      label = '2 years post-procedure';
      style = dashed;
      fontname = 'helvetica-bold';
      rank = same;

      node[shape = box, style = filled, color = black];
      tab18 [label = 'FU within window (n=@@3-1)']
      tab19 [label = 'FU outside window (n=@@3-2)']
      tab20 [label = 'Missed visit (n=@@3-3)']
      tab21 [label = 'Exit study prior to visit (n=@@3-4)']
      tab22 [label = 'Visit pending (n=@@3-5)']
      tab23 [label = 'Unknown (n=@@3-6)']

      tab18 -> tab20
      tab19 -> tab23

      }

      subgraph cluster_four {

      node[shape = box, style = filled, fillcolor = lightblue];
      label = '1 year post-randomization';
      style = dashed;
      fontname = 'helvetica-bold';
      rank = same;

      node[shape = box, style = filled, color = black];
      tab24 [label = 'FU within window (n=@@4-1)']
      tab25 [label = 'FU outside window (n=@@4-2)']
      tab26 [label = 'Missed visit (n=@@4-3)']
      tab27 [label = 'Exit study prior to visit (n=@@4-4)']
      tab28 [label = 'Visit pending (n=@@4-5)']
      tab29 [label = 'Unknown (n=@@4-6)']

      tab24 -> tab26
      tab25 -> tab29

      }

      subgraph cluster_five {

      node[shape = box, style = filled, fillcolor = lightblue];
      label = '2 years post-randomization';
      style = dashed;
      fontname = 'helvetica-bold';
      rank = same;

      node[shape = box, style = filled, color = black];
      tab30 [label = 'FU within window (n=@@5-1)']
      tab31 [label = 'FU outside window (n=@@5-2)']
      tab32 [label = 'Missed visit (n=@@5-3)']
      tab33 [label = 'Exit study prior to visit (n=@@5-4)']
      tab34 [label = 'Visit pending (n=@@5-5)']
      tab35 [label = 'Unknown (n=@@5-6)']

      tab30 -> tab32
      tab31 -> tab35

      }

      tab8 -> tab12 -> tab14 -> tab18 -> tab20;
      tab8 -> tab26 -> tab30 -> tab32;

      }

      [1]: c(1, 2, 3, 4, 5, 6)
      [2]: c(1, 2, 3, 4, 5, 6)
      [3]: c(1, 2, 3, 4, 5, 6)
      [4]: c(1, 2, 3, 4, 5, 6)
      [5]: c(1, 2, 3, 4, 5, 6)
      ")

Output: enter image description here

Essentially, I want the subgraphs for "30 days post-procedure," "1 year post-procedure," and "2 years post-procedure" to be on the left side (or the first "column"), with 30 days being at the top, 1 year in the middle, and 2 years at the bottom. On the right side (or the second "column"), I want 1 year post-randomization to be at the top and 2 years to be at the bottom. This is a rough sketch of what I'm talking about:

enter image description here

Any ideas or suggestions would be deeply appreciated. Thank you!


Solution

  • embed the 5 clusters within 2 clusters and add an invisible edge:

    digraph G {
          rankdir = LR
          edge[style = invis]
          node[fontname = "helvetica", width = 1.5, height = 0.5, fontsize = 20]
          graph[fontname = "helvetica", fontsize = 20]
    
      subgraph clusterLeft{
          peripheries=0
    
          subgraph cluster_one {
          peripheries=1
    
          node[shape = box, style = filled, fillcolor = lightblue];
          label = "30 days post-procedure";
          style = dashed;
          fontname = "helvetica-bold";
          rank = same;
    
          node[shape = box, style = filled, color = black];
          tab6 [label = "FU within window (n=@@1-1)"]
          tab7 [label = "FU outside window (n=@@1-2)"]
          tab8 [label = "Missed visit (n=@@1-3)"]
          tab9 [label = "Exit study prior to visit (n=@@1-4)"]
          tab10 [label = "Visit pending (n=@@1-5)"]
          tab11 [label = "Unknown (n=@@1-6)"]
    
          tab6 -> tab8
          tab7 -> tab11
    
          }
    
          subgraph cluster_two {
          peripheries=1
    
          node[shape = box, style = filled, fillcolor = lightblue];
          label = "1 year post-procedure";
          style = dashed;
          fontname = "helvetica-bold";
          rank = same;
    
          node[shape = box, style = filled, color = black];
          tab12 [label = "FU within window (n=@@2-1)"]
          tab13 [label = "FU outside window (n=@@2-2)"]
          tab14 [label = "Missed visit (n=@@2-3)"]
          tab15 [label = "Exit study prior to visit (n=@@2-4)"]
          tab16 [label = "Visit pending (n=@@2-5)"]
          tab17 [label = "Unknown (n=@@2-6)"]
    
          tab12 -> tab14
          tab13 -> tab17
    
          }
    
          subgraph cluster_three {
          peripheries=1
    
          node[shape = box, style = filled, fillcolor = lightblue];
          label = "2 years post-procedure";
          style = dashed;
          fontname = "helvetica-bold";
          rank = same;
    
          node[shape = box, style = filled, color = black];
          tab18 [label = "FU within window (n=@@3-1)"]
          tab19 [label = "FU outside window (n=@@3-2)"]
          tab20 [label = "Missed visit (n=@@3-3)"]
          tab21 [label = "Exit study prior to visit (n=@@3-4)"]
          tab22 [label = "Visit pending (n=@@3-5)"]
          tab23 [label = "Unknown (n=@@3-6)"]
    
          tab18 -> tab20
          tab19 -> tab23
    
          }
      }
      subgraph clusterRight {
          peripheries=0
    
          subgraph cluster_four {
          peripheries=1
    
          node[shape = box, style = filled, fillcolor = lightblue];
          label = "1 year post-randomization";
          style = dashed;
          fontname = "helvetica-bold";
          rank = same;
    
          node[shape = box, style = filled, color = black];
          tab24 [label = "FU within window (n=@@4-1)"]
          tab25 [label = "FU outside window (n=@@4-2)"]
          tab26 [label = "Missed visit (n=@@4-3)"]
          tab27 [label = "Exit study prior to visit (n=@@4-4)"]
          tab28 [label = "Visit pending (n=@@4-5)"]
          tab29 [label = "Unknown (n=@@4-6)"]
    
          tab24 -> tab26
          tab25 -> tab29
    
          }
    
          subgraph cluster_five {
          peripheries=1
    
          node[shape = box, style = filled, fillcolor = lightblue];
          label = "2 years post-randomization";
          style = dashed;
          fontname = "helvetica-bold";
          rank = same;
    
          node[shape = box, style = filled, color = black];
          tab30 [label = "FU within window (n=@@5-1)"]
          tab31 [label = "FU outside window (n=@@5-2)"]
          tab32 [label = "Missed visit (n=@@5-3)"]
          tab33 [label = "Exit study prior to visit (n=@@5-4)"]
          tab34 [label = "Visit pending (n=@@5-5)"]
          tab35 [label = "Unknown (n=@@5-6)"]
    
          tab30 -> tab32
          tab31 -> tab35
    
          }
      }
    
      tab17 ->  tab28
    }
    

    enter image description here