Search code examples
rdiagrammer

DiagrammeR: Merging lines in flowchart


I am new to R and trying replicate work from research I come across.

I am looking to replicate this flowchart:

enter image description here

This is the code I have so far:

library(DiagrammeR)

grViz(" digraph sample_selection {   graph [layout = dot, rankdir = TB, splines = ortho, overlap = true]

  node [shape = box, style = filled, fillcolor = white]

  # Nodes for each year   '2010 Survey\\nParticipants\\n(n = 32,846)' [group = years]   '2011 Survey\\nParticipants\\n(n = 35,313)' [group = years]   '2012 Survey\\nParticipants\\n(n = 38,974)' [group = years]   '2013 Survey\\nParticipants\\n(n = 36,940)' [group = years]

  # Combined node   '2010-2013\\n(n = 144,073)'

  # Exclusion nodes   'Excluded participants\\n<18 years at time of\\nsurvey\\n(n = 41,277)'   'Excluded participants\\nwithout medical visit\\nin the last 12 months\\n(45,843)' [style = 'filled,rounded', penwidth = 2, fillcolor = white, color = black]   'Excluded participants\\nwithout a usual\\nsource of care\\n(n = 8,984)'

  # Final sample node   'Final Sample\\npopulation (n = 47,969)\\nrepresenting 130\\nmillion individuals'

  # Intermediate node   'n = 102,796'   'n = 56,953'

  # Edges   {rank = same; '2010 Survey\\nParticipants\\n(n = 32,846)'; '2011 Survey\\nParticipants\\n(n = 35,313)'; '2012 Survey\\nParticipants\\n(n = 38,974)'; '2013 Survey\\nParticipants\\n(n = 36,940)'}   {rank = same; '2010-2013\\n(n
= 144,073)'}   '2010 Survey\\nParticipants\\n(n = 32,846)' -> '2010-2013\\n(n = 144,073)'   '2011 Survey\\nParticipants\\n(n = 35,313)' -> '2010-2013\\n(n = 144,073)'   '2012 Survey\\nParticipants\\n(n = 38,974)' -> '2010-2013\\n(n = 144,073)'   '2013 Survey\\nParticipants\\n(n = 36,940)' -> '2010-2013\\n(n = 144,073)'

  '2010-2013\\n(n = 144,073)' -> 'Excluded participants\\n<18 years at time of\\nsurvey\\n(n = 41,277)'   '2010-2013\\n(n = 144,073)' -> 'n = 102,796'

  'n = 102,796' -> 'Excluded participants\\nwithout medical visit\\nin the last 12 months\\n(45,843)'   'n = 102,796' -> 'n = 56,953'

  'n = 56,953' -> 'Excluded participants\\nwithout a usual\\nsource of care\\n(n = 8,984)'   'n = 56,953' -> 'Final Sample\\npopulation (n = 47,969)\\nrepresenting 130\\nmillion individuals' } ")

Which produces this: enter image description here

Guidance on how to get those lines merged together?


Solution

  • Not a DiagrammR solution, but the flowchart package does this reasonably well.

    library(flowchart)
    
    fc1 <- as_fc(N=144073, label="2010-2013 Surveys", text_pattern = "{label}") |>
      fc_split(N=c(32846, 35313, 38974, 36940),
               label=c("2010 Survey", "2011 Survey", "2012 Survey", "2013 Survey"),
               text_pattern = "{label}\n(n={n})")
    

    fc2 <- as_fc(N=144073, text_pattern = "(n={n})") |>
      fc_filter(N=102796,
                text_pattern = "(n={n})",
                show_exc = TRUE, 
                label_exc = "Excluded participants\n
                             <18 years at time\nof survey",
                text_pattern_exc = "{label}\n(n={n})", 
                text_fs_exc = 8, 
                offset_exc=0.2) |>
      fc_filter(N=56953, 
                text_pattern = "(n={n})",
                show_exc = TRUE, 
                label_exc = "Excluded participants\n
                             without medical visit\n
                             in the last 12 months",
                text_pattern_exc = "{label}\n(n={n})", 
                text_fs_exc = 8, 
                offset_exc=0.2) |>
      fc_filter(N=47969,
                label = "Final sample\n
                         population representing\n
                         130 million individuals", 
                text_pattern = "{label}\n(n={n})",
                show_exc = TRUE, 
                label_exc = "Excluded participants\n
                             without a usual\n
                             source of care",
                text_pattern_exc = "{label}\n(n={n})", 
                text_fs_exc = 8, 
                offset_exc=0.2)
    

    list(fc1, fc2) |>
      fc_stack(unite=TRUE) |> 
      fc_draw(arrow_angle=0)
    

    enter image description here