r - DiagrammeR - 箭头问题

标签 r

我正在尝试使用这个优秀的教程创建一个流程图:https://dannyjnwong.github.io/STROBE-CONSORT-Diagrams-in-R/

library(DiagrammeR)
library(DiagrammeRsvg) #Needed if you want to export the image
library(rsvg) #Needed if you want to export the image

#Set the values which will go into each label.
a1 <- 'Total available patients\n(n = x)'
b1 <- ''
c1 <- ''
d1 <- 'Included for analysis\n(n = x - y - z)'
e1 <- 'Data linked with\nexternal dataset'
a2 <- ''
b2 <- 'Excluded because of\nexclusion criteria (n = y)'
c2 <- 'Excluded because of\nmissing values (n = z)'
d2 <- ''
e2 <- ''

#Create a node dataframe
ndf <- create_node_df(
  n = 10,
  label = c(a1, b1, c1, d1, e1, #Column 1
            a2, b2, c2, d2, e2), #Column 2
  style = c('solid', 'invis', 'invis', 'solid', 'solid', #Column 1
            'invis', 'solid', 'solid', 'invis', 'invis'), #Column 2
  shape = c('box', 'point', 'point', 'box', 'box', #Column 1 
            'plaintext', 'box', 'box', 'point', 'point'), #Column 2
  width = c(3, 0.001, 0.001, 3, 3, #Column 1
            2, 2.5, 2.5, 0.001, 0.001), #Column 2
  height = c(1, 0.001, 0.001, 1, 1, #Column 1
             1, 1, 1, 0.001, 0.001), #Column 2
  fontsize = c(rep(14, 10)),
  fontname = c(rep('Helvetica', 10)),
  penwidth = 1.5,
  fixedsize = 'true')

#Create an edge dataframe
edf <- create_edge_df(
  from = c(1, 2, 3, 4, #Column 1
           6, 7, 8, 9, #Column 2
           2, 3 #Horizontals
           ),
  to = c(2, 3, 4, 5, #Column 1
         7, 8, 9, 10, #Column 2
         7, 8 #Horizontals
         ),
  arrowhead = c('none', 'none', 'normal', 'normal', #Column 1
                'none', 'none', 'none', 'none', #Column 2
                'normal', 'normal' #Horizontals
                ),
  color = c('black', 'black', 'black', 'black', #Column 1
            '#00000000', '#00000000', '#00000000', '#00000000', #Column 2
            'black', 'black' #Horizontals
            ),
  constraint = c(rep('true', 8), #Columns
                 rep('false', 2) #Horizontals
                 )
)

g <- create_graph(ndf,
                  edf,
                  attr_theme = NULL)

render_graph(g)

结果应如下所示:
enter image description here

但是当我在计算机上运行代码时,它看起来像这样

enter image description here

有热心帮忙的吗?

此致,
H

最佳答案

也许有点晚,但总比没有好。
您必须使用 width = 0 创建两个节点和 height = 0 ,箭头可以从那里水平起源。它们需要定义为子图才能位于水平轴上。

DiagrammeR::grViz("digraph prisma{
    node [shape=box, fontsize = 12, fontname = 'Helvetica', width = 2];
    a; b; c; d; e
    
    # create 2 nodes without box around 
    node [shape = point, width = 0, height = 0]
    x1; x2
    graph [splines=ortho, nodesep=1, dpi = 72]
    
    # Labels
    a [label = 'Total available patients\n(n = x)'];
    b [label = 'Excluded because of\nexclusion criteria (n = y)'];
    c [label = 'Excluded because of\nmissing values (n = z)'];
    d [label = 'Included for analysis\n(n = x - y - z)']
    e [label = 'Data linked with\nexternal dataset']
    
    
    # Edge definitions
    a -> x1  [arrowhead='none']
    x1 -> x2 [arrowhead='none']
    x1 -> b 
    x2 -> c
    x2 -> d
    d -> e
    
    # Make subgraph definition so arrow is horzontal
    subgraph {
      rank = same; b; x1;
    }
    subgraph {
      rank = same; c; x2;
    }
  }
}
")

创建于 2021-08-06 由 reprex package (v2.0.1)

session 信息

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.3 (2020-10-10)
#>  os       macOS Catalina 10.15.7      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  de_CH.UTF-8                 
#>  ctype    de_CH.UTF-8                 
#>  tz       Europe/Zurich               
#>  date     2021-08-06                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version date       lib source        
#>  backports      1.2.1   2020-12-09 [1] CRAN (R 4.0.2)
#>  callr          3.7.0   2021-04-20 [1] CRAN (R 4.0.2)
#>  cli            3.0.0   2021-06-30 [1] CRAN (R 4.0.2)
#>  crayon         1.4.1   2021-02-08 [1] CRAN (R 4.0.2)
#>  curl           4.3.2   2021-06-23 [1] CRAN (R 4.0.2)
#>  DiagrammeR     1.0.6.1 2020-05-08 [1] CRAN (R 4.0.2)
#>  digest         0.6.27  2020-10-24 [1] CRAN (R 4.0.2)
#>  ellipsis       0.3.2   2021-04-29 [1] CRAN (R 4.0.2)
#>  evaluate       0.14    2019-05-28 [1] CRAN (R 4.0.0)
#>  fansi          0.5.0   2021-05-25 [1] CRAN (R 4.0.2)
#>  fs             1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
#>  glue           1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
#>  highr          0.9     2021-04-16 [1] CRAN (R 4.0.2)
#>  htmltools      0.5.1.1 2021-01-22 [1] CRAN (R 4.0.2)
#>  htmlwidgets    1.5.2   2020-10-03 [1] CRAN (R 4.0.2)
#>  httr           1.4.2   2020-07-20 [1] CRAN (R 4.0.2)
#>  jsonlite       1.7.2   2020-12-09 [1] CRAN (R 4.0.2)
#>  knitr          1.33    2021-04-24 [1] CRAN (R 4.0.2)
#>  lifecycle      1.0.0   2021-02-15 [1] CRAN (R 4.0.2)
#>  magrittr       2.0.1   2020-11-17 [1] CRAN (R 4.0.2)
#>  mime           0.11    2021-06-23 [1] CRAN (R 4.0.2)
#>  pillar         1.6.1   2021-05-16 [1] CRAN (R 4.0.2)
#>  pkgconfig      2.0.3   2019-09-22 [1] CRAN (R 4.0.0)
#>  processx       3.5.2   2021-04-30 [1] CRAN (R 4.0.2)
#>  ps             1.6.0   2021-02-28 [1] CRAN (R 4.0.2)
#>  purrr          0.3.4   2020-04-17 [1] CRAN (R 4.0.0)
#>  R6             2.5.0   2020-10-28 [1] CRAN (R 4.0.2)
#>  RColorBrewer   1.1-2   2014-12-07 [1] CRAN (R 4.0.0)
#>  reprex         2.0.1   2021-08-05 [1] CRAN (R 4.0.3)
#>  rlang          0.4.11  2021-04-30 [1] CRAN (R 4.0.2)
#>  rmarkdown      2.9     2021-06-15 [1] CRAN (R 4.0.2)
#>  rstudioapi     0.13    2020-11-12 [1] CRAN (R 4.0.2)
#>  sessioninfo    1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
#>  stringi        1.7.3   2021-07-16 [1] CRAN (R 4.0.2)
#>  stringr        1.4.0   2019-02-10 [1] CRAN (R 4.0.2)
#>  styler         1.3.2   2020-02-23 [1] CRAN (R 4.0.2)
#>  tibble         3.1.2   2021-05-16 [1] CRAN (R 4.0.2)
#>  utf8           1.2.1   2021-03-12 [1] CRAN (R 4.0.2)
#>  vctrs          0.3.8   2021-04-29 [1] CRAN (R 4.0.2)
#>  visNetwork     2.0.9   2019-12-06 [1] CRAN (R 4.0.2)
#>  webshot        0.5.2   2019-11-22 [1] CRAN (R 4.0.0)
#>  withr          2.4.2   2021-04-18 [1] CRAN (R 4.0.2)
#>  xfun           0.24    2021-06-15 [1] CRAN (R 4.0.2)
#>  xml2           1.3.2   2020-04-23 [1] CRAN (R 4.0.0)
#>  yaml           2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

关于r - DiagrammeR - 箭头问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61745574/

相关文章:

r - 计算变量的唯一级别数

sql - 在 R 中使用正则表达式删除 SQL 注释,同时保留特殊标记

r - 如果缺少,请使用另一数据框的值更新现有的 data.frame

r - 如何删除heatmap.2输出中的左侧空白

r - 订购 list ,命令不起作用

r - R 中重复测量的意大利面条图

regex - dplyr内的正则表达式匹配

r - 在 Rstudio Server 中为每个用户使用不同版本的 R

regex - R中的正则表达式

r - 为什么 summary 和 class 方法在 R 中的 data.frame 类型上不一致?