0

I am using the DiagrammeR package to create a flowchart like this:

grViz("
    digraph boxes_and_circles {

        # left 'node' statements
        node [shape = box,
        fontname = Helvetica,
        width = 2, 
        penwidth = 2,
        color = lightblue]
        A1; P1; P2

        # right node statements
        node [shape = box,
        fontname = Helvetica,
        width = 2,
        penwidth = 2,
        color = steelblue]
        A2; A3; P3; P4; P5; AP

        # edge statements
        P1->P2 P2->P3 P3->P4 P4->P5 P5->AP
        A1->A2->A3
        A2->P4
        A3->AP

        # define ranks
        subgraph {
            rank = same; A2; P4
        }

        subgraph {
            rank = same; A1; P2
        }
    }
")

This creates a graph like this:

enter image description here

However, instead of manually filling in the values in the boxes, I want to fill in values from a dataframe where the column names correspond to each of the boxes:

d <- read.table(text = "P1  P2  P3  A1  P4  P5  A2  AP  A3
23  46  38  101 378 344 33  222 45", header = T) 

I have been able to go only so far with this:

node.df <- create_node_df(n = 9, shape = 'box', color = 'aqua', 
                          type = c(rep('box',9)), 
                          label = c('P1','P2','P3','P4','P5','AP','A1','A2','A3'), 
                          tooltip = c('P1','P2','P3','P4','P5','AP','A1','A2','A3'),
                          penwidth = 2,
                          fontsize= 35,
                          face = 'bold',
                          width = 5)
edge.df <- create_edge_df(from = c(1,2,3,4,5,7,8), 
                          to = c(2,3,4,5,6,8,9), 
                          rel = 'leading_from')
render_graph(create_graph(nodes_df = node.df,
             edges_df = edge.df))

However, it creates the following flowchart that is not even close to what I was initially creating:

enter image description here

Can someone suggest how do I set the ranking and order of each box?

Thanks!

Komal Rathi
  • 4,164
  • 13
  • 60
  • 98

1 Answers1

3

Use Graphviz Substitution described here.

d <- read.table(text = "P1  P2  P3  A1  P4  P5  A2  AP  A3
23  46  38  101 378 344 33  222 45", header = T) 

library(DiagrammeR)
grViz("
    digraph boxes_and_circles {

        # left 'node' statements
        node [shape = box,
        fontname = Helvetica,
        width = 2, 
        penwidth = 2,
        color = lightblue]
        A1; P1; P2

        # right node statements
        node [shape = box,
        fontname = Helvetica,
        width = 2,
        penwidth = 2,
        color = steelblue]
        A2; A3; P3; P4; P5; AP

        # edge statements
        P1->P2 P2->P3 P3->P4 P4->P5 P5->AP
        A1->A2->A3
        A2->P4
        A3->AP

        # define ranks
        subgraph {
            rank = same; A2; P4
        }

        subgraph {
            rank = same; A1; P2
        }

        # define labels using Graphviz substitution
        A1 [label = '@@1-4']
        A2 [label = '@@1-7']
        A3 [label = '@@1-9']
        P1 [label = '@@1-1']
        P2 [label = '@@1-2']
        P3 [label = '@@1-3']
        P4 [label = '@@1-5']
        P5 [label = '@@1-6']
        AP [label = '@@1-8']

    }
    [1]: d

")

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58