7

I am trying to create a flowchart using this excellent tutorial: 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)

The result should look like this: enter image description here

But instead it looks likes this when i run the code on my computer

enter image description here

Anyone eager to help?

Best regards, H

1 Answer 1

4

Maybe a bit late, but better than never. You have to create two nodes with width = 0 and height = 0, from where the arrows can origin horizontally. They need to be defined as subgraphs in order to be on a horizontal axis.

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;
    }
  }
}
")

Created on 2021-08-06 by the reprex package (v2.0.1)

Session info
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.