Motivation

$$ \begin{aligned} \textrm{Susceptible Individuals} \qquad \dot S & = - b S I \\ \textrm{Infected Individuals} \qquad \dot I & = b S I - g I \\ \textrm{Recovered Individuals} \qquad \dot R & = g I \end{aligned} $$ wzxhzdk:0

Motivation

flowdiagramr basics

Specify a model:

library(flowdiagramr)
varlabels = c("S","I","R")
flows     = list(S_flows = c("-b*S*I"), 
                 I_flows = c("b*S*I","-g*I"), 
                 R_flows = c("g*I"))
sirmodel = list(varlabels = varlabels, flows = flows)

flowdiagramr basics

Let flowdiagramr turn it into a nice diagram (in 2 steps):

# prepare diagram
sir_diagram_list <- prepare_diagram(sirmodel)
# make diagram
sir_diagram <- make_diagram(sir_diagram_list)
plot(sir_diagram)

Customize your diagram

Specify settings to be passed to function make_diagram(diagram_list, diagram_settings)

sir_diagram_settings <- list(
    var_outline_color = "black",
    var_fill_color = c("#6aa4c8", "#eb5600", "#1a9988"),
    var_label_color = c("black","white","black"),
    var_label_size = 12,
    main_flow_color = "blue",
    main_flow_size = 1.5,
    interaction_flow_label_size = 6,
    interaction_flow_color = "red",
    interaction_flow_size = 1.2)

Customize your diagram

Create diagram with customized settings

sir_diagram <- make_diagram(sir_diagram_list, sir_diagram_settings)
plot(sir_diagram)

More customizations

Specify settings to be passed to function prepare_diagram(model_list, model_settings)

model_settings = 
  list(varnames  = c("Susceptible","Infected","Recovered"),
       use_varnames = TRUE,
       var_label_size = 4,
       varlocations = matrix(data = c("S", "", "R",
                                       "", "I", "" ),
                      nrow = 2, ncol = 3, byrow = TRUE)
      )

More customizations

sir_diagram_list2 <- prepare_diagram(sirmodel, model_settings)
sir_diagram2 <- make_diagram(sir_diagram_list2)
plot(sir_diagram2)

More customizations

You can combine settings for prepare_diagram and make_diagram

sir_diagram_settings$var_label_size = 4 #reduce to make text fit
sir_diagram3 <- make_diagram(sir_diagram_list2, 
                             sir_diagram_settings)
plot(sir_diagram3)

Even more customization

prepare_diagram returns a list of data frames that specify variable and flow location and styling.

:::{ .smallcode}

print(sir_diagram_list2$variables)

:::

Even more customization

You can edit the object produced by prepare_diagram before calling make_diagram.

#increase the I box a bit both on left and right 
sir_diagram_list2$variables$xmin[2] = 2 #was 2.5
sir_diagram_list2$variables$xmax[2] = 4 #was 3.5
#modify inflow and outflow arrows to align with new box size 
sir_diagram_list2$flows$xend[1] = 2 #was 2.5
sir_diagram_list2$flows$xstart[2] = 4 #was 3.5
#move the b*S*I arrow end a bit
sir_diagram_list2$flows$xend[3] = 1.25 #was 1.5
#move the b*S*I label 
sir_diagram_list2$flows$labelx[3] = 2 #was 2.35
sir_diagram_list2$flows$labely[3] = -1.2 #was -0.75

Even more customization

With those modifications, the resulting diagram looks like this

sir_diagram4 <- make_diagram(sir_diagram_list2, sir_diagram_settings)
plot(sir_diagram4)

Get the full code

write_diagram(sirmodel, filename ="sirmodel_diagram_code.R")

Further Resources

The flowdiagramr website provides a lot of additional information (https://andreashandel.github.io/flowdiagramr/):

Feedback is appreciated:



andreashandel/modeldiagram documentation built on July 31, 2023, 10:05 a.m.