rvisualizationdiagrammer

Controlling Position of Boxes in DiagrammeR


I wondered if anyone could help with positioning of boxes in a Diagrammer flowchart, please?

In particular, on the third layer of the flowchart, I would like horizontal splits from 4-5, and 4-6. And also horizontal splits between 7-8, and 7-9.

Therefore, the growth out of nodes 2 and 3 is a two t-shapes. Would appreciate if anyone could help with this.

Thanks

library(DiagrammeR)

grViz("digraph flowchart {
      node [fontname = Arial, shape = circular, fontsize=11]       
      tab1 [label = '@@1']
      tab2 [label = '@@2']
      tab3 [label = '@@3']
      tab4 [label = '@@4']
      tab5 [label = '@@5']
      tab6 [label = '@@6']
      tab7 [label = '@@7']
      tab8 [label = '@@8']
      tab9 [label = '@@9']

      # edge definitions with the node IDs
      tab1 -> tab2 
      tab1 -> tab3 
      tab2 -> tab4
      tab4 -> tab5
      tab4 -> tab6
      tab3 -> tab7
      tab7 -> tab8
      tab7 -> tab9
      
      ;
      }

      [1]: '1'
      [2]: '2'
      [3]: '3'
      [4]: '4'
      [5]: '5'
      [6]: '6'
      [7]: '7'
      [8]: '8'
      [9]: '9'
      ")

enter image description here

Edit for @benson23

enter image description here


Solution

  • Use rank = same to set nodes to same level. dir = back to reverse arrow direction and group = left to align nodes vertically.

    I've cleaned up your code a bit.

    library(DiagrammeR)
    
    grViz("digraph flowchart {
          node [fontname = Arial, shape = circular, fontsize=11]       
          tab1 [label = '1']
          tab2 [label = '2'; group = left]
          tab3 [label = '3'; group = right]
          tab4 [label = '4'; group = left]
          tab7 [label = '7'; group = right]
          tab5 [label = '5']
          tab6 [label = '6']
          tab8 [label = '8']
          tab9 [label = '9']
          tab10 [label = '10']
          tab1 -> tab10
          tab2 -> tab4
          tab3 -> tab7
          subgraph {
            rank = same; tab2; tab3; tab10;
            tab2 -> tab10 [dir = back]
            tab10 -> tab3 
          }
          subgraph {
            rank = same; tab4; tab5; tab6; tab7; tab8; tab9;
            tab5 -> tab4 [dir = back]
            tab4 -> tab6
            tab6 -> tab8 [color = white; arrowsize = 0]
            tab8 -> tab7 [dir = back]
            tab7 -> tab9
      }
    }")
    

    flowchart