Scransom
Scransom

Reputation: 3335

Ordering flowchart in graphviz

The graph I'd like to produce is a something like this a left-to-right flowchart with a main process at the top, and a series of groupings of stuff below that feed in and out at various points like this: (though this is a dummy example and I want lots of stuff coming in and out from the top code box, which is why a horizontal layout works better than the default enter image description here

The problem is that this is made in powerpoint...

I can get something close with this:

digraph example {
  
  graph [
    rankdir = LR
  ]
  
  subgraph cluster_code {
    label = "code";
    A;
    B;
    C;
    D;
  }

  subgraph cluster_data {
    label = "data";
    data_1;
    data_2;
  }
  
  subgraph cluster_source {
    label = "source"
    source_1;
    source_2
  }
  
   A -> B
   B -> C
   C -> D
  
  data_1 -> A
  data_2 -> B
  A -> output_1
  output_1 -> C
  
  source_1 -> data_1
  source_2 -> data_2
  
  #{rank = same; source_2; data_2; A}
  
}

enter image description here

But if I try to bring the source and data clusters underneath the code cluster using {rank = same; source_2; data_2; A} (this is hashed out above, and I don't repeat the whole code for brevity of the post), I then A, data_2 and source_2 drop out of the box. I think this is something do do with rank and clusters not playing nicely together.

enter image description here

Any hints on getting something like the first graph above?

Am running graphviz via R/Rstudio and DiagrammeR.

Upvotes: 0

Views: 684

Answers (1)

sroush
sroush

Reputation: 6773

It seems that you want to change "rankdir" in the middle of the graph. Quite reasonable, but Graphviz doesn't support it. Here is your graph, using default rankdir and the not-that-well-documented ability to effectively change rankdir by using rank=same in a subgraph. It also reverses edge arrowhead direction - a kludge, but it works.

digraph example {
  node [width=1.5]

  subgraph cluster_code {
    label = "code";
    {rank=same 
    A -> B -> C -> D    
    }
  }

  subgraph cluster_data {
    label = "data";
    data_1;
    data_2;
  }
  
  subgraph cluster_source {
    label = "source"
    source_1;
    source_2
  }
  
  A -> output_1
  output_1 -> C
  
  edge[dir=back minlen=2]  // minlen makes (rank) space
  A -> data_1  // -> A
  A -> data_2  // -> A
  
  edge[dir=back minlen=1]
  data_1 -> source_1 // -> data_1
  data_2 -> source_2 // -> data_2
}

Giving this: enter image description here

Upvotes: 1

Related Questions