2

使用RDiagrammeR,我想创建回归模型树的层次表示。

我有四个拆分的原始数据(蓝色椭圆)和五个终端节点(红色方块)。从下图中可以看出,模型 2、7 和 8 是终端节点,但它们与模型 4 和 5 出现在同一级别(相对于它们的垂直对齐)。我该如何实现呢?

library("DiagrammeR")

grViz(" 
  digraph CFA {
    # Multiple level nodes
    node [shape = ellipse, color=CornflowerBlue]
    a [label = '@@1']; 
    b [label = '@@2']; 
    c [label = '@@3']; 
    d [label = '@@4'];

    # Terminal branch nodes
    node [shape = box, color = Crimson] 
    e [label = 'Model 2'];
    f [label = 'Model 4'];
    g [label = 'Model 5'];
    h [label = 'Model 7'];
    i [label = 'Model 8'];

     # Connect nodes with edges and labels
    a -> b [label = 'Condition 1a']
    a -> d [label = 'Condition 1b'] 
    b -> e [label = 'Condition 2a'] 
    b -> c [label = 'Condition 2b']
    c -> f [label = 'Condition 3a']
    c -> g [label = 'Condition 3b']
    d -> h [label = 'Condition 4a'] 
    d -> i [label = 'Condition 4b'] 
  }

[1]: 'Split 1' 
[2]: paste0('Model 1\\n Split 2') 
[3]: paste0('Model 3\\n Split 3') 
[4]: paste0('Model 6\\n Split 4') 
")

模型树


编辑

我发现通过对未按预期放置的终端节点进行分组并定义minlength参数来手动调整边缘的长度(见下文)。但是,我更喜欢一种不需要摆弄参数的方法。

grViz("
  digraph CFA {
    # latent variables
    node [shape = ellipse, color=CornflowerBlue]
    a [label = '@@1'];
    b [label = '@@2'];
    c [label = '@@3'];
    d [label = '@@4'];

    node [shape = box, color = Crimson]
    e [label = 'Model 2'];
    f [label = 'Model 4'];
    g [label = 'Model 5'];
    h [label = 'Model 7'];
    i [label = 'Model 8'];

    # Define arrow length for first group
    edge [color = grey, minlen = 1]
    a -> b [label = 'Condition 1a']
    a -> d [label = 'Condition 1b']
    b -> c [label = 'Condition 2b']
    c -> f [label = 'Condition 3a']
    c -> g [label = 'Condition 3b']

    # Define edge length for models 2, 7 and 8
    edge [color = grey, minlen = 2]
    b -> e [label = 'Condition 2a']
    d -> h [label = 'Condition 4a']
    d -> i [label = 'Condition 4b']
  } 

[1]: 'Split 1'
[2]: paste0('Model 1\\n Split 2')
[3]: paste0('Model 3\\n Split 3')
[4]: paste0('Model 6\\n Split 4')
")
4

1 回答 1

2

例如

library("DiagrammeR")

grViz(" 
  digraph CFA {
    # Multiple level nodes
    node [shape = ellipse, color=CornflowerBlue]
    a [label = '@@1']; 
    b [label = '@@2']; 
    c [label = '@@3']; 
    d [label = '@@4'];
    {rank = same; b; d}

    # Terminal branch nodes
    node [shape = box, color = Crimson] 
    e [label = 'Model 2'];
    f [label = 'Model 4'];
    g [label = 'Model 5'];
    h [label = 'Model 7'];
    i [label = 'Model 8'];
    {rank = same; e; f; g; h; i}

    # Connect nodes with edges and labels
    a -> b [label = 'Condition 1a']
    a -> d [label = 'Condition 1b'] 
    b -> e [label = 'Condition 2a'] 
    b -> c [label = 'Condition 2b']
    c -> f [label = 'Condition 3a']
    c -> g [label = 'Condition 3b']
    d -> h [label = 'Conddition 4a'] 
    d -> i [label = 'Condition 4b'] 
  }

[1]: 'Split 1' 
[2]: paste0('Model 1\\n Split 2') 
[3]: paste0('Model 3\\n Split 3') 
[4]: paste0('Model 6\\n Split 4') 
")

给你

在此处输入图像描述

于 2016-09-12T13:30:27.797 回答