0

我正在使用 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
        }
    }
")

这将创建一个像这样的图表:

在此处输入图像描述

但是,我不想手动填写框中的值,而是想从列名对应于每个框的数据框中填写值:

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

我只能做到这一点:

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))

但是,它创建了以下流程图,甚至与我最初创建的流程图都不相近:

在此处输入图像描述

有人可以建议我如何设置每个盒子的排名和顺序吗?

谢谢!

4

1 回答 1

3

使用此处描述的 Graphviz 替换。

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

")

在此处输入图像描述

于 2017-12-14T21:15:20.357 回答