-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowchart.Rmd
533 lines (429 loc) · 17.6 KB
/
flowchart.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
---
title: "flowchart"
output:
rmarkdown::html_vignette:
toc: true
toc_depth: 5
number_sections: true
package: "`flowchart`"
vignette: >
%\VignetteIndexEntry{flowchart}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: inline
---
```{r message=FALSE, warning=FALSE, include=FALSE}
library(flowchart)
library(dplyr)
library(tidyr)
library(purrr)
library(stringr)
library(tibble)
```
# Overview
`flowchart` is a package for drawing participant flow diagrams directly from a dataframe using tidyverse. It provides a set of functions that can be combined with `|>` to create all kinds of flowcharts from a dataframe in an easy way:
- `as_fc()` transforms a dataframe into a `fc` object that can be manipulated by the package.
- `fc_split()` splits a flowchart according to the different values of a column in the dataframe.
- `fc_filter()` creates a filtered box from the flowchart, based on the evaluation of an expression in the dataframe.
- `fc_merge()` combines horizontally two different flowcharts.
- `fc_stack()` combines vertically two different flowcharts.
- `fc_modify()` allows to modify the parameters of the flowchart which are stored in `.$fc`.
- `fc_draw()` draws the flowchart created by the previous functions.
- `fc_export()` allows to export the flowchart drawn to the desired format.
# Installation
We can install the stable version in CRAN:
```{r eval=FALSE}
install.packages("flowchart")
```
Or the development version from GitHub:
```{r eval=FALSE}
# install.packages("remotes")
remotes::install_github('bruigtp/flowchart')
```
# safo dataset
We will use the built-in dataset `safo`, which is a randomly generated dataset from the SAFO trial[^1]. SAFO is an open-label, multicentre, phase III–IV superiority randomised clinical trial designed to assess whether cloxacillin plus fosfomycin administered during the first 7 days of therapy achieves better treatment outcomes than cloxacillin alone in hospitalised patients with meticillin-sensitive Staphylococcus aureus bacteraemia.
[^1]: Grillo, S., Pujol, M., Miró, J.M. et al. Cloxacillin plus fosfomycin versus cloxacillin alone for methicillin-susceptible Staphylococcus aureus bacteremia: a randomized trial. Nat Med 29, 2518–2525 (2023). https://doi.org/10.1038/s41591-023-02569-0
```{r}
library(flowchart)
data(safo)
head(safo)
```
# Basic operations
The first step is to initialise the flowchart with `as_fc`. The last step, if we want to visualise the created flowchart, is to draw the flowchart with `fc_draw`. In between we can combine the functions `fc_split`., `fc_filter`, `fc_merge`, `fc_stack` with the operator pipe (`|>` or `%>$`) to create complex flowchart structures.
## Initialize
To initialize a flowchart from a dataset we have to use the `as_fc()` function:
```{r}
safo_fc <- safo |>
as_fc()
str(safo_fc, max.level = 1)
```
The `safo_fc` object created is a `fc` object, which consists of a list containing the tibble of the dataframe associated with the flowchart and the tibble that stores the flowchart parameters. In this example, `safo_fc$data` corresponds to the `safo` dataset while `safo_fc$fc` contains the parameters of the initial flowchart:
```{r}
safo_fc$fc
```
Alternatively, if a dataframe is not available, we can initialize a flowchart using the `N =` argument manually specifying the number of rows:
```{r include=FALSE}
as_fc(N = 230)
```
## Draw
The function `fc_draw()` allows to draw the flowchart associated to any `fc` object. Following the last example, we can draw the initial flowchart that has been previously created:
```{r}
safo_fc |>
fc_draw()
```
## Filter
We can filter the flowchart using `fc_filter()` specifying the logic in which the filter is to be applied. For example, we can show the number of patients that were randomized in the study:
```{r fig.width = 6, fig.height = 5}
safo |>
as_fc(label = "Patients assessed for eligibility") |>
fc_filter(!is.na(group), label = "Randomized", show_exc = TRUE) |>
fc_draw()
```
Percentages are calculated from the box in the previous level. See 'Modify function arguments' for more information on the `label=` and `show_exc=` arguments.
Alternatively, if the column to filter is not available, we can use the `N =` argument to manually specify the number of rows of the resulting filter:
```{r fig.width = 6, fig.height = 5}
safo |>
as_fc(label = "Patients assessed for eligibility") |>
fc_filter(N = 215, label = "Randomized", show_exc = TRUE) |>
fc_draw()
```
## Split
We can split the flowchart into groups using `fc_split()` specifying the grouping variable. The function will split the flowchart into as many categories as the specified variable has. For example, we can split the previous flowchart showing the patients allocated in the two study treatments:
```{r fig.width = 6, fig.height = 5}
safo |>
dplyr::filter(!is.na(group)) |>
as_fc(label = "Randomized patients") |>
fc_split(group) |>
fc_draw()
```
Percentages are calculated from the box in the previous level.
Alternatively, if the column to split is not available, we can use the `N =` argument to manually specify the number of rows in each group of the resulting split:
```{r fig.width = 6, fig.height = 5}
safo |>
dplyr::filter(!is.na(group)) |>
as_fc(label = "Randomized patients") |>
fc_split(N = c(105, 110), label = c("cloxacillin plus fosfomycin", "cloxacillin alone")) |>
fc_draw()
```
# Customize output
We can customize the flowchart either with the arguments provided by each function in the process of creating it, or directly in the final output using the function `modify_fc`.
## Modify function arguments
Arguments common to `as_fc()`, `fc_filter()` and `fc_split()`, to customise the appearance of the boxes created at each step:
<table>
<thead>
<tr>
<th style="text-align:left;"> Argument </th>
<th style="text-align:left;"> Description </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> `N=` </td>
<td style="text-align:left;"> manually specify the numbers to display in the boxes.</td>
</tr>
<tr>
<td style="text-align:left;"> `label=` </td>
<td style="text-align:left;"> modify the label. </td>
</tr>
<tr>
<td style="text-align:left;"> `text_pattern=` </td>
<td style="text-align:left;"> modify the pattern of the text. </td>
</tr>
<tr>
<td style="text-align:left;"> `just=` </td>
<td style="text-align:left;"> modify the justification for the text. </td>
</tr>
<tr>
<td style="text-align:left;"> `text_color=` </td>
<td style="text-align:left;"> modify the color of the text. </td>
</tr>
<tr>
<td style="text-align:left;"> `text_fs=` </td>
<td style="text-align:left;"> modify the font size of the text. </td>
</tr>
<tr>
<td style="text-align:left;"> `bg_fill=` </td>
<td style="text-align:left;"> modify the background color of the box. </td>
</tr>
<tr>
<td style="text-align:left;"> `border_color=` </td>
<td style="text-align:left;"> modify the border color of the box. </td>
</tr>
</tbody>
</table>
`as_fc()` arguments:
<table>
<thead>
<tr>
<th style="text-align:left;"> Argument </th>
<th style="text-align:left;"> Description </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> `hide=` </td>
<td style="text-align:left;"> hide the first initial box created by this function. </td>
</tr>
</tbody>
</table>
`fc_filter()` arguments:
<table>
<thead>
<tr>
<th style="text-align:left;"> Argument </th>
<th style="text-align:left;"> Description </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> `sel_group=` </td>
<td style="text-align:left;"> apply the filter only in the specified groups (if data is grouped). </td>
</tr>
<tr>
<td style="text-align:left;"> `round_digits=` </td>
<td style="text-align:left;"> modify the number of digits to round percentages. </td>
</tr>
<tr>
<td style="text-align:left;"> `show_exc=` </td>
<td style="text-align:left;"> show the box with the excluded rows that do not match the filter. </td>
</tr>
<tr>
<td style="text-align:left;"> `direction_exc=` </td>
<td style="text-align:left;"> change the direction of the exclusion box (left or right). </td>
</tr>
<tr>
<td style="text-align:left;"> `label_exc=` </td>
<td style="text-align:left;"> modify the label of the exclusion box. </td>
</tr>
<tr>
<td style="text-align:left;"> `text_pattern_exc=` </td>
<td style="text-align:left;"> modify the pattern of the exclusion box. </td>
</tr>
<tr>
<td style="text-align:left;"> `just_exc=` </td>
<td style="text-align:left;"> modify the justification for the text of the exclusion box. </td>
</tr>
<tr>
<td style="text-align:left;"> `text_color_exc=` </td>
<td style="text-align:left;"> modify the color of the text in the exclusion box. </td>
</tr>
<tr>
<td style="text-align:left;"> `text_fs_exc=` </td>
<td style="text-align:left;"> modify the font size of the text in the exclusion box. </td>
</tr>
<tr>
<td style="text-align:left;"> `bg_fill_exc=` </td>
<td style="text-align:left;"> modify the background color of the exclusion box. </td>
</tr>
<tr>
<td style="text-align:left;"> `border_color_exc=` </td>
<td style="text-align:left;"> modify the border color of the exclusion box. </td>
</tr>
</tbody>
</table>
`fc_split()` arguments:
<table>
<thead>
<tr>
<th style="text-align:left;"> Argument </th>
<th style="text-align:left;"> Description </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> `sel_group=` </td>
<td style="text-align:left;"> split the flowchart only in the specified groups (if data is grouped). </td>
</tr>
<tr>
<td style="text-align:left;"> `na.rm=` </td>
<td style="text-align:left;"> omit the missing values in the grouping variable. </td>
</tr>
<tr>
<td style="text-align:left;"> `show_zero=` </td>
<td style="text-align:left;"> omit the levels of the grouping variable that don't have data. </td>
<tr>
<td style="text-align:left;"> `round_digits=` </td>
<td style="text-align:left;"> modify the number of digits to round percentages. </td>
</tr>
</tr>
</tbody>
</table>
`fc_draw()` arguments are heredited from `arrow`:
<table>
<thead>
<tr>
<th style="text-align:left;"> Argument </th>
<th style="text-align:left;"> Description </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> `arrow_angle=` </td>
<td style="text-align:left;"> angle of the arrow head in degrees. </td>
</tr>
<tr>
<td style="text-align:left;"> `arrow_length=` </td>
<td style="text-align:left;"> unit specifying the length of the arrow head. </td>
</tr>
<tr>
<td style="text-align:left;"> `arrow_ends=` </td>
<td style="text-align:left;"> specify the ends of the line to draw the arrow head (last/first/both). </td>
</tr>
<tr>
<td style="text-align:left;"> `arrow_type=` </td>
<td style="text-align:left;"> whether the arrow head should be a closed triangle. </td>
</tr>
</tbody>
</table>
## Function to customize the flowchart
The function `modify_fc` allows the user to customise the created flowchart by modifying its parameters, which are stored in `.$fc`.
For example, we could fully customise the text in the exclusion box if we wanted to specify the different reasons for exclusion:
```{r fig.width = 7, fig.height = 5}
safo_fc <- safo |>
as_fc(label = "Patients assessed for eligibility") |>
fc_filter(!is.na(group), label = "Randomized", show_exc = TRUE) |>
fc_modify(
~ . |>
mutate(
text = ifelse(id == 3, str_glue("- {sum(safo$inclusion_crit == 'Yes')} not met the inclusion criteria\n- {sum(safo$exclusion_crit == 'Yes')} met the exclusion criteria"), text)
)
)
safo_fc |>
fc_draw()
```
We could also use `fc_modify()` to change the default _x_ and the _y_ coordinates:
```{r fig.width = 6, fig.height = 5}
safo_fc |>
fc_modify(
~ . |>
mutate(
x = case_when(
id == 3 ~ 0.75,
TRUE ~ x
),
y = case_when(
id == 1 ~ 0.8,
id == 2 ~ 0.2,
TRUE ~ y
)
)
) |>
fc_draw()
```
# Combine
`fc_merge()` and `fc_stack()` allow you to combine different flowcharts horizontally or vertically. This is very useful when you need to combine flowcharts generated from different dataframes, as shown here.
## Merge
We can combine different flowcharts horizontally using `fc_merge()`. For example, we might want to represent the flow of patients included in the ITT population with the flow of patients included in the PP population.
```{r fig.width = 8}
# Create first flowchart for ITT
fc1 <- safo |>
as_fc(label = "Patients assessed for eligibility") |>
fc_filter(itt == "Yes", label = "Intention to treat (ITT)")
fc_draw(fc1)
# Create second flowchart for visits
fc2 <- safo |>
as_fc(label = "Patients assessed for eligibility") |>
fc_filter(pp == "Yes", label = "Per protocol (PP)")
fc_draw(fc2)
list(fc1, fc2) |>
fc_merge() |>
fc_draw()
```
## Stack
We can combine different flowcharts vertically using `fc_stack()`. For example, we can combine the same two flowcharts vertically instead of horizontally.
```{r warning = FALSE, fig.width = 6, fig.height = 5}
list(fc1, fc2) |>
fc_stack() |>
fc_draw()
```
# Export
Once the flowchart has been drawn we can export it to the most popular image formats (png, jpeg, tiff, bmp) using `fc_export()`:
```{r eval = FALSE}
safo |>
as_fc(label = "Patients assessed for eligibility") |>
fc_filter(!is.na(group), label = "Randomized", show_exc = TRUE) |>
fc_draw() |>
fc_export("flowchart.png")
```
We can change the size and resolution of the stored image.
```{r eval = FALSE}
safo |>
as_fc(label = "Patients assessed for eligibility") |>
fc_filter(!is.na(group), label = "Randomized", show_exc = TRUE) |>
fc_draw() |>
fc_export("flowchart.png", width = 2500, height = 2000, res = 700)
```
# Examples
## Example 1
In this example, we will try to create a flowchart for the complete flow of patients in the SAFO study:
```{r warning = FALSE, fig.width = 7, fig.height = 7}
safo |>
as_fc(label = "Patients assessed for eligibility") |>
fc_filter(!is.na(group), label = "Randomized", show_exc = TRUE) |>
fc_split(group) |>
fc_filter(itt == "Yes", label = "Included in ITT") |>
fc_filter(pp == "Yes", label = "Included in PP") |>
fc_draw()
```
## Example 2
In this example, we will try to exactly reproduce the original flowchart of the SAFO study published in Nature Medicine: [SAFO flowchart](https://www.nature.com/articles/s41591-023-02569-0/figures/1).
First, we need to do some pre-processing to reproduce the text in the larger boxes:
```{r warning=FALSE, fig.width = 12, fig.height = 8}
# Create labels for exclusion box:
label_exc <- paste(
c(str_glue("{sum(safo$inclusion_crit == 'Yes' | safo$exclusion_crit == 'Yes' | safo$decline_part == 'Yes', na.rm = T)} excluded:"),
map_chr(c("inclusion_crit", "decline_part", "exclusion_crit"), ~str_glue("{sum(safo[[.x]] == 'Yes', na.rm = TRUE)} {attr(safo[[.x]], 'label')}")),
map_chr(4:15, ~str_glue(" - {sum(safo[[.x]] == 'Yes')} {attr(safo[[.x]], 'label')}"))),
collapse = "\n")
label_exc <- gsub("exclusion criteria", "exclusion criteria:", label_exc)
safo1 <- safo |>
filter(group == "cloxacillin alone", !is.na(reason_pp)) |>
mutate(reason_pp = droplevels(reason_pp))
label_exc1 <- paste(
c(str_glue("{nrow(safo1)} excluded:"),
map_chr(levels(safo1$reason_pp), ~str_glue(" - {sum(safo1$reason_pp == .x)} {.x}"))),
collapse = "\n")
label_exc1 <- str_replace_all(label_exc1, c("resistant" = "resistant\n", "blood" = "blood\n"))
safo2 <- safo |>
filter(group == "cloxacillin plus fosfomycin", !is.na(reason_pp)) |>
mutate(reason_pp = droplevels(reason_pp))
label_exc2 <- paste(
c(str_glue("{nrow(safo2)} excluded:"),
map_chr(levels(safo2$reason_pp), ~str_glue(" - {sum(safo2$reason_pp == .x)} {.x}"))),
collapse = "\n")
label_exc2 <- str_replace_all(label_exc2, c("nosocomial" = "nosocomial\n", "treatment" = "treatment\n"))
```
Second, let's create and customise the flowchart using the functions in the package:
```{r warning=FALSE, fig.width = 12, fig.height = 9}
safo |>
as_fc(label = "patients assessed for eligibility", text_pattern = "{n} {label}") |>
fc_filter(!is.na(group), label = "randomized", text_pattern = "{n} {label}", show_exc = TRUE,
just_exc = "left", text_pattern_exc = "{label}", label_exc = label_exc, text_fs_exc = 7) |>
fc_split(group, text_pattern = "{n} asssigned\n {label}") |>
fc_filter(itt == "Yes", label = "included in intention-to-treat\n population", show_exc = TRUE,
text_pattern = "{n} {label}",
label_exc = "patient did not receive allocated\n treatment (withdrew consent)",
text_pattern_exc = "{n} {label}", text_fs_exc = 7) |>
fc_filter(pp == "Yes", label = "included in per-protocol\n population", show_exc = TRUE,
just_exc = "left", text_pattern = "{n} {label}", text_fs_exc = 7) |>
fc_modify(
~.x |>
filter(n != 0) |>
mutate(
text = case_when(id == 11 ~ label_exc1, id == 13 ~ label_exc2, TRUE ~ text),
x = case_when(id == 3 ~ x + 0.15, id %in% c(11, 13) ~ x + 0.01, TRUE ~ x),
y = case_when(id %in% c(1, 3) ~ y + 0.05, id >= 2 ~ y - 0.05, TRUE ~ y)
)
) |>
fc_draw()
```
## Example 3
In this example, we will create a flowchart without any dataframe, using the `N =` argument to manually specify the numbers to display in the boxes:
```{r warning=FALSE, fig.width = 7, fig.height = 4.5}
as_fc(N = 300) |>
fc_filter(N = 240, label = "Randomized patients", show_exc = TRUE) |>
fc_split(N = c(100, 80, 60), label = c("Group A", "Group B", "Group C")) |>
fc_filter(N = c(80, 75, 50), label = "Finished the study") |>
fc_draw()
```