绘制多重/并行中介模型的图表在R中

huangapple go评论104阅读模式
英文:

Drawing diagram of a multiple / parallel mediation model in R

问题

I want to draw a publication-ready diagram of a parallel mediation model in R.

我想在R中绘制一张适合出版的并行中介模型的图表。

I adapted the Diagrammer + Graphviz Code provided here https://stackoverflow.com/questions/46465752/drawing-simple-mediation-diagram-in-r, but I do not seem to get the expected output. I think I am doing something wrong with the rank argument.

我使用了这里提供的Diagrammer + Graphviz代码https://stackoverflow.com/questions/46465752/drawing-simple-mediation-diagram-in-r,但我似乎没有得到预期的输出。我认为我在rank参数上做错了什么。

My goal is to have the predictor and outcome at the bottom and all three mediators in one vertical line above.

我的目标是将预测变量和结果放在底部,将三个中介变量放在上面的一条垂直线上。

This following code almost gets it, but puts two mediators below instead on top. This would also work, if the arrow from xx to yy would at least land in the middle of the box.

以下的代码几乎可以实现这个目标,但将两个中介变量放在下面而不是上面。如果从xx到yy的箭头至少能够落在盒子的中间,也可以工作。

med_data <-
  data.frame(
    lab_x   = "XXXXXX",
    lab_m1   = "MMMMMM 1",
    lab_m2   = "MMMMMM 2",
    lab_m3   = "MMMMMM 3",
    lab_y   = "YYYYYYY",
    coef_xm1 = "0.11*",
    coef_m1y = "0.11*",
    coef_xm2 = "0.22*",
    coef_m2y = "0.22*",
    coef_xm3 = "0.33*",
    coef_m3y = "0.33*",
    coef_xy = "0.66*"
  )

Output, Mediation Model 1:
绘制多重/并行中介模型的图表在R中

A second version I tried, led to all mediators being in a horizontal instead of a vertical line:

我尝试的第二个版本导致所有中介变量都水平排列,而不是垂直排列。

med_data <-
  data.frame(
    lab_x   = "XXXXXX",
    lab_m1   = "MMMMMM 1",
    lab_m2   = "MMMMMM 2",
    lab_m3   = "MMMMMM 3",
    lab_y   = "YYYYYYY",
    coef_xm1 = "0.11*",
    coef_m1y = "0.11*",
    coef_xm2 = "0.22*",
    coef_m2y = "0.22*",
    coef_xm3 = "0.33*",
    coef_m3y = "0.33*",
    coef_xy = "0.66* (.16)"
  )

Output, Mediation Model 2:
绘制多重/并行中介模型的图表在R中

Help and a little input on the rank part would be amazing or an alternative package for solving this issue.

关于rank部分的帮助和一点输入将是很棒的,或者有一个解决这个问题的替代包。

Update:
Here is an example picture:
Example Model

更新:
这里有一个示例图片:
Example Model

英文:

I want to draw a publication-ready diagram of a parallel mediation model in R.

I adapted the Diagrammer + Graphviz Code provided here https://stackoverflow.com/questions/46465752/drawing-simple-mediation-diagram-in-r, but I do not seem to get the expected output. I think I am doing something wrong with the rank argument.

My goal is to have the predictor and outcome at the bottom and all three mediators in one vertical line above.

This following code almost gets it, but puts two mediatiors below instead on top. This would also work, if the arrow from xx to yy would at least land in the middle of the box.

med_data <-
  data.frame(
    lab_x   = "XXXXXX",
    lab_m1   = "MMMMMM 1",
    lab_m2   = "MMMMMM 2",
    lab_m3   = "MMMMMM 3",
    lab_y   = "YYYYYYY",
    coef_xm1 = "0.11*",
    coef_m1y = "0.11*",
    coef_xm2 = "0.22*",
    coef_m2y = "0.22*",
    coef_xm3 = "0.33*",
    coef_m3y = "0.33*",
    coef_xy = "0.66*"
  )


med_diagram <- function(data, height = .75, width = 2, 
                        graph_label = NA, node_text_size = 12, 
                        edge_text_size = 12, color = "black", 
                        ranksep = .2, minlen = 3){
  
  require(glue)
  require(DiagrammeR)
  
  data$height  <- height   # node height
  data$width   <- width    # node width
  data$color   <- color    # node + edge border color
  data$ranksep <- ranksep  # separation btwn mediator row and x->y row
  data$minlen  <- minlen   # minimum edge length
  
  data$node_text_size  <- node_text_size
  data$edge_text_size  <- edge_text_size
  
  data$graph_label <- ifelse(is.na(graph_label), "", 
                             paste0("label = '", 
                                    graph_label, "'"))
  
  diagram_out <- glue::glue_data(data,
                                 "digraph flowchart {
      fontname = Helvetica
      <<graph_label>>
      graph [ranksep = <<ranksep>>]

      # node definitions with substituted label text
      node [fontname = Helvetica, shape = rectangle, fixedsize = TRUE, 
      width = <<width>>, height = <<height>>, fontsize = <<node_text_size>>, 
      color = <<color>>]        
        mm1 [label = '<<lab_m1>>']
        xx [label = '<<lab_x>>']
        yy [label = '<<lab_y>>']
        mm2 [label = '<<lab_m2>>']
        mm3 [label = '<<lab_m3>>']

      # edge definitions with the node IDs
      edge [minlen = <<minlen>>, fontname = Helvetica, 
      fontsize = <<edge_text_size>>, color = <<color>>]
        xx -> yy [label = '<<coef_xy>>'];
        mm1 -> yy [label = '<<coef_m1y>>'];
        xx -> mm1 [label = '<<coef_xm1>>'];
        mm2 -> yy [label = '<<coef_m2y>>'];
        xx -> mm2 [label = '<<coef_xm2>>'];
        mm3 -> yy [label = '<<coef_m3y>>'];
        xx -> mm3 [label = '<<coef_xm3>>'];
        
      rankdir = LR;
      { rank = same; mm1; mm2; mm3 }
      
      }

      ", .open = "<<", .close = ">>")  
  
  
  DiagrammeR::grViz(diagram_out)  
}

med_diagram(med_data)

Output, Mediation Model 1:
绘制多重/并行中介模型的图表在R中

A second version I tried, led to all mediators being in a horizontal instead of a vertical line:

med_data <-
  data.frame(
    lab_x   = "XXXXXX",
    lab_m1   = "MMMMMM 1",
    lab_m2   = "MMMMMM 2",
    lab_m3   = "MMMMMM 3",
    lab_y   = "YYYYYYY",
    coef_xm1 = "0.11*",
    coef_m1y = "0.11*",
    coef_xm2 = "0.22*",
    coef_m2y = "0.22*",
    coef_xm3 = "0.33*",
    coef_m3y = "0.33*",
    coef_xy = "0.66* (.16)"
  )


med_diagram <- function(data, height = .75, width = 2, 
                        graph_label = NA, node_text_size = 12, 
                        edge_text_size = 12, color = "black", 
                        ranksep = .2, minlen = 3){
  
  require(glue)
  require(DiagrammeR)
  
  data$height  <- height   # node height
  data$width   <- width    # node width
  data$color   <- color    # node + edge border color
  data$ranksep <- ranksep  # separation btwn mediator row and x->y row
  data$minlen  <- minlen   # minimum edge length
  
  data$node_text_size  <- node_text_size
  data$edge_text_size  <- edge_text_size
  
  data$graph_label <- ifelse(is.na(graph_label), "", 
                             paste0("label = '", 
                                    graph_label, "'"))
  
  diagram_out <- glue::glue_data(data,
                                 "digraph flowchart {
      fontname = Helvetica
      <<graph_label>>
      graph [ranksep = <<ranksep>>]

      # node definitions with substituted label text
      node [fontname = Helvetica, shape = rectangle, fixedsize = TRUE, 
      width = <<width>>, height = <<height>>, fontsize = <<node_text_size>>, 
      color = <<color>>]        
        mm1 [label = '<<lab_m1>>']
        xx [label = '<<lab_x>>']
        yy [label = '<<lab_y>>']
        mm2 [label = '<<lab_m2>>']
        mm3 [label = '<<lab_m3>>']

      # edge definitions with the node IDs
      edge [minlen = <<minlen>>, fontname = Helvetica, 
      fontsize = <<edge_text_size>>, color = <<color>>]
        xx -> yy [label = '<<coef_xy>>'];
        mm1 -> yy [label = '<<coef_m1y>>'];
        xx -> mm1 [label = '<<coef_xm1>>'];
        mm2 -> yy [label = '<<coef_m2y>>'];
        xx -> mm2 [label = '<<coef_xm2>>'];
        mm3 -> yy [label = '<<coef_m3y>>'];
        xx -> mm3 [label = '<<coef_xm3>>'];
        

      { rank = max; xx; yy}
      }

      ", .open = "<<", .close = ">>")  
  
  
  DiagrammeR::grViz(diagram_out)  
}

med_diagram(med_data)

Output, Mediation Model 2:
绘制多重/并行中介模型的图表在R中

Help and a little input on the rank part would be amazing or an alternative package for solving this issue.

Update:
Here is an example picture:
Example Model

答案1

得分: 2

最佳参考资料排名:https://www.graphviz.org/pdf/dotguide.pdf(尤其是第7页和第15页)以及https://www.graphviz.org/docs/attrs/rank/
总的来说,这并不是Graphviz的强项。我不使用Diagrammer,但下面的Graphviz代码可能会相对容易转化。

digraph N{
splines=false // 直线边
rankdir=TB    // 与默认相同
nodesep=1.2
ranksep=.2
node [shape=rect]
// 设置这些节点一个在每一层
mm1 -> mm2 [style=invis]
mm2 -> mm3 [style=invis]
xxxx -> mm1:w
xxxx -> mm2:w
xxxx -> mm3:w
mm1:e -> yyyy
mm2:e -> yyyy
mm3:e -> yyyy
{
rank=sink  // 如果你想要这些节点在顶部,使用rank=source
// 需要虚构节点以保持左右对称,不确定为什么
bogus [style=invis shape=plain]
xxxx -> yyyy
xxxx -> bogus [style=invis]
bogus -> yyyy [style=invis]
}
}

结果:
绘制多重/并行中介模型的图表在R中

英文:

Best references for ranking: https://www.graphviz.org/pdf/dotguide.pdf (esp. p. 7 & 15) and https://www.graphviz.org/docs/attrs/rank/
All-in-all not a Graphviz strength. I do not use Diagrammer, but the Graphviz code below will probably translate pretty easily.

digraph N{
splines=false // straight line edges
rankdir=TB    // same as default
nodesep=1.2
ranksep=.2
node [shape=rect]
// set these nodes one per rank
mm1 -> mm2 [style=invis]
mm2 -> mm3 [style=invis]
xxxx -> mm1:w
xxxx -> mm2:w
xxxx -> mm3:w
mm1:e -> yyyy
mm2:e -> yyyy
mm3:e -> yyyy
{
rank=sink  //  rank=source   if you want these nodes on top
// bogus node is needed to keep left-right symmetry, not sure why
bogus [style=invis shape=plain]
xxxx -> yyyy
xxxx -> bogus [style=invis]
bogus -> yyyy [style=invis]
}
}

Giving:
绘制多重/并行中介模型的图表在R中

huangapple
  • 本文由 发表于 2023年3月4日 01:39:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定