DiagrammeR icon indicating copy to clipboard operation
DiagrammeR copied to clipboard

Variable values in DiagrammeR

Open komalsrathi opened this issue 7 years ago • 1 comments

Hi,

I am trying 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 generates a graph like this:

rplot

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 gone as far as doing 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))

But it creates something like this which is not even close to what I had before:

rplot01

Can you please show me an example on how to reproduce the first diagram with variable values?

Any help would be much appreciated.

Thanks!

komalsrathi avatar Dec 14 '17 19:12 komalsrathi

Do you have any tips on how to recreate the flowchart that OP wants with the functional approach? I am trying to create a similar graph as well. For my purpose, I miss the rank attribute from graphviz that enables horizontal alignment.

marton-balazs-kovacs avatar Dec 24 '19 20:12 marton-balazs-kovacs