---
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`. 

## Change 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;"> `text_fface=` </td>
   <td style="text-align:left;"> modify the font face of the text. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `text_ffamily=` </td>
   <td style="text-align:left;"> modify the font family of the text. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `text_padding=` </td>
   <td style="text-align:left;"> modify the padding of the text inside the box. </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;"> `perc_total=` </td>
   <td style="text-align:left;"> calculate percentages using the total number of rows at the beginning of the flowchart. </td>
  </tr>
  <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;"> `text_fface_exc=` </td>
   <td style="text-align:left;"> modify the font face of the text in the exclusion box. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `text_ffamily_exc=` </td>
   <td style="text-align:left;"> modify the font family of the text in the exclusion box. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `text_padding_exc=` </td>
   <td style="text-align:left;"> modify the padding of the text inside 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>
  <tr>
   <td style="text-align:left;"> `offset_exc=` </td>
   <td style="text-align:left;"> add space to the distance between the box and the excluded box (in the x-coordinate). </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;"> `perc_total=` </td>
   <td style="text-align:left;"> calculate percentages using the total number of rows at the beginning of the flowchart. </td>
  </tr>
  <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>
   <td style="text-align:left;"> `title=` </td>
   <td style="text-align:left;"> add a title box to the split. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `text_color_title=` </td>
   <td style="text-align:left;"> modify the color of the text in the title box. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `text_fs_title=` </td>
   <td style="text-align:left;"> modify the font size of the text in the title box. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `text_fface_title=` </td>
   <td style="text-align:left;"> modify the font face of the text in the title box. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `text_ffamily_title=` </td>
   <td style="text-align:left;"> modify the font family of the text in the title box. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `text_padding_title=` </td>
   <td style="text-align:left;"> modify the padding of the text in the title box. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `bg_fill_title=` </td>
   <td style="text-align:left;"> modify the background color of the title box. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `border_color_title=` </td>
   <td style="text-align:left;"> modify the border color of the title box. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `offset=` </td>
   <td style="text-align:left;"> add space to the distance between the boxes (in the x-coordinate). </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>
  <tr>
   <td style="text-align:left;"> `title=` </td>
   <td style="text-align:left;"> title of the flowchart. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `title_x=` </td>
   <td style="text-align:left;"> x coordinate of the title. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `title_y=` </td>
   <td style="text-align:left;"> y coordinate of the title. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `title_color=` </td>
   <td style="text-align:left;"> color of the title. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `title_fs=` </td>
   <td style="text-align:left;"> font size of the title. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `title_fface=` </td>
   <td style="text-align:left;"> font face of the title. </td>
  </tr>
  <tr>
   <td style="text-align:left;"> `title_ffamily=` </td>
   <td style="text-align:left;"> font family of the title. </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, let's customize the following flowchart:

```{r fig.width = 6, fig.height = 4}
 safo_fc <- safo |> 
  as_fc(label = "Patients assessed for eligibility") |>
  fc_filter(!is.na(group), label = "Randomized", show_exc = TRUE) 

safo_fc |> 
  fc_draw()
```

Previous to modifying it, we can use the function `fc_view()` to inspect the element `$fc` that we want to change:

```{r}
safo_fc |> 
  fc_view("fc")
```

Let's customise the text in the exclusion box (`id = 3`) to specify different reasons for exclusion, and change the _x_ and _y_ coordinate:
 
```{r fig.width = 7, fig.height = 5}
safo_fc |> 
  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),
        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()
```

## Example 4

In this example, we will explore some of the arguments to customize the following flowchart:

```{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_split(group) |> 
  fc_draw()
```

### Add a title to the flowchart

We can add a title to the flowchart using the argument `title=` in the `fc_draw()` function:

```{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_split(group) |> 
  fc_draw(title = "SAFO flowchart")
```

### Add a title to the split

We can also add a title to a split in the flowchart, using the argument `title` in the `fc_split()` function:

```{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, perc_total = TRUE) |> 
  fc_split(group, perc_total = TRUE, title = "Treatment", bg_fill_title = "skyblue") |> 
  fc_draw()
```

### Percentage with respect to the total rows

We can change the calculation of all percentages in a flowchart. By default, percentages are calculated with respect to the box in the previous level. With the argument `perc_total=` we can change it, to calculate it with respect to the initial box with the total number of rows:

```{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, perc_total = TRUE) |> 
  fc_split(group, perc_total = TRUE) |> 
  fc_draw()
```

### Offset

We can add/remove space to the distance between boxes in a split using the argument `offset`:

```{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, perc_total = TRUE) |> 
  fc_split(group, offset = 0.1) |> 
  fc_draw()

safo |> 
  as_fc(label = "Patients assessed for eligibility") |>
  fc_filter(!is.na(group), label = "Randomized", show_exc = TRUE, perc_total = TRUE) |> 
  fc_split(group, offset = -0.1) |> 
  fc_draw()
```

We can also add/remove space to the distance between the excluded box in a filter using the argument `offset_exc`:

```{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, offset_exc = 0.1) |> 
  fc_split(group) |> 
  fc_draw()
```
