<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/flow-charts-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://aghaynes.wordpress.com/2018/05/09/flow-charts-in-r/"> R – Insights of a PhD</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Flow charts are an important part of a clinical trial report. Making them can be a pain though. One good way to do it seems to be with the grid and Gmisc packages in R. X and Y coordinates can be designated based on the center of the boxes in normalized device coordinates (proportions of the device space – 0.5 is this middle) which saves a lot of messing around with corners of boxes and arrows.</p>
<p>A very basic flow chart, based very roughly on the <a rel="nofollow" href="http://www.consort-statement.org/consort-statement/flow-diagram" target="_blank">CONSORT version</a>, can be generated as follows…</p>
<pre>library(grid)
library(Gmisc)

grid.newpage()
# set some parameters to use repeatedly
leftx &lt;- .25
midx &lt;- .5
rightx &lt;- .75
width &lt;- .4
gp &lt;- gpar(fill = &quot;lightgrey&quot;)
# create boxes
(total &lt;- boxGrob(&quot;Total\n N = NNN&quot;, 
 x=midx, y=.9, box_gp = gp, width = width))

(rando &lt;- boxGrob(&quot;Randomized\n N = NNN&quot;, 
 x=midx, y=.75, box_gp = gp, width = width))
# connect boxes like this
connectGrob(total, rando, &quot;v&quot;)

(inel &lt;- boxGrob(&quot;Ineligible\n N = NNN&quot;, 
 x=rightx, y=.825, box_gp = gp, width = .25, height = .05))

connectGrob(total, inel, &quot;-&quot;)

(g1 &lt;- boxGrob(&quot;Allocated to Group 1\n N = NNN&quot;, 
 x=leftx, y=.5, box_gp = gp, width = width))
(g2 &lt;- boxGrob(&quot;Allocated to Group 2\n N = NNN&quot;, 
 x=rightx, y=.5, box_gp = gp, width = width))

connectGrob(rando, g1, &quot;N&quot;)
connectGrob(rando, g2, &quot;N&quot;)

(g11 &lt;- boxGrob(&quot;Followed up\n N = NNN&quot;, 
 x=leftx, y=.3, box_gp = gp, width = width))
(g21 &lt;- boxGrob(&quot;Followed up\n N = NNN&quot;, 
 x=rightx, y=.3, box_gp = gp, width = width))

connectGrob(g1, g11, &quot;N&quot;)
connectGrob(g2, g21, &quot;N&quot;)

(g12 &lt;- boxGrob(&quot;Completed\n N = NNN&quot;, 
 x=leftx, y=.1, box_gp = gp, width = width))
(g22 &lt;- boxGrob(&quot;Completed\n N = NNN&quot;, 
 x=rightx, y=.1, box_gp = gp, width = width))

connectGrob(g11, g12, &quot;N&quot;)
connectGrob(g21, g22, &quot;N&quot;)</pre>
<p>Sections of code to make the boxes are wrapped in brackets to print them immediately. The code creates something like the following figure:</p>
<p><img data-orig-file="https://aghaynes.files.wordpress.com/2018/05/flowtemp.png?w=456" data-orig-size="778,818" data-medium-file="https://aghaynes.files.wordpress.com/2018/05/flowtemp.png?w=456?w=285" data-attachment-id="347" data-image-meta="{" data-image-description="" src="https://aghaynes.files.wordpress.com/2018/05/flowtemp.png?w=456" data-permalink="https://aghaynes.wordpress.com/2018/05/09/flow-charts-in-r/flowtemp/" data-large-file="https://aghaynes.files.wordpress.com/2018/05/flowtemp.png?w=456?w=497" alt="flowtemp" data-image-title="flowtemp" sizes="(max-width: 497px) 100vw, 497px" data-comments-opened="1" class="wp-image-347" data-recalc-dims="1"></p>
<p>For detailed info, see the <a rel="nofollow" href="https://cran.r-project.org/web/packages/Gmisc/vignettes/Grid-based_flowcharts.html" target="_blank">Gmisc vignette</a>. This code is also on <a rel="nofollow" href="https://github.com/aghaynes/misc/blob/master/flow_template.R" target="_blank">github</a>.</p>
<p> </p>
<p> </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://aghaynes.wordpress.com/2018/05/09/flow-charts-in-r/"> R – Insights of a PhD</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/pdn9WxWv8os" height="1"></a></div>