<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/forest-plot-with-horizontal-bands/"></div></div><div><a data-url="http://www.r-bloggers.com/forest-plot-with-horizontal-bands/" data-counturl="http://www.r-bloggers.com/forest-plot-with-horizontal-bands/" data-count="horizontal" data-text="Forest Plot (with Horizontal Bands)" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://designdatadecisions.wordpress.com/2016/07/02/forest-plot-with-horizontal-bands/"> R – Design Data Decisions</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Forest plots are often used in clinical trial reports to show differences in the estimated treatment effect(s) across various patient subgroups. See, for example a <a target="_blank" href="http://trialsjournal.biomedcentral.com/articles/10.1186/1745-6215-8-36">review</a>. The page on <a target="_blank" href="https://www.ctspedia.org/do/view/CTSpedia/StatGraphHome">Clinical Trials Safety Graphics</a> includes a SAS code for a forest plot that depicts the hazard ratios for various patient subgroups (this web page has links to other interesting clinical trial graphics, as well as links to notes on best practices for graphs, and is worth checking out).</p>
<p>The purpose of this blog post is to create the same forest plot using R.</p>
<p>It should be possible to create such a graphic from first principles, using either base R graphics or using the <em>ggplot2</em> package such as <a target="_blank" href="https://mcfromnz.wordpress.com/2012/11/06/forest-plots-in-r-ggplot-with-side-table/">posted here</a>. However, there is a contributed package <em><a target="_blank" href="https://cran.r-project.org/web/packages/forestplot/index.html">forestplot</a></em> that makes it very easy to make forest plots interspersed with tables – we just need to supply the right arguments to the <em>forestplot</em> function in the package. Also, one question that arose was how easy it would be to get the horizontal grey bands for alternate rows in the forest plot. This too was not very difficult. Following is a short explanation of the entire process, as well as the relevant R code:</p>
<p>First, we store the data for the plot, <a target="_blank" href="https://designdatadecisions.files.wordpress.com/2016/07/forestplotdata.xlsx">ForestPlotData</a> in any convenient file format and then read it into a dataframe in R:</p>
<pre>
workdir &lt;- &quot;&quot;C:\\Path\\To\\Relevant\\Directory&quot;&quot;
datafile &lt;- file.path(workdir,&quot;ForestPlotData.csv&quot;)
data &lt;- read.csv(datafile, stringsAsFactors=FALSE)
</pre>
<p>Then format the data a bit so that the column labels and columns match the required graphical output:</p>
<pre>
## Labels defining subgroups are a little indented!
subgps &lt;- c(4,5,8,9,12,13,16,17,20,21,24,25,28,29,32,33)
data$Variable[subgps] &lt;- paste(&quot;  &quot;,data$Variable[subgps]) 
 
## Combine the count and percent column
np &lt;- ifelse(!is.na(data$Count), paste(data$Count,&quot; (&quot;,data$Percent,&quot;)&quot;,sep=&quot;&quot;), NA)
 
## The rest of the columns in the table. 
tabletext &lt;- cbind(c(&quot;Subgroup&quot;,&quot;\n&quot;,data$Variable), 
                    c(&quot;No. of Patients (%)&quot;,&quot;\n&quot;,np), 
                    c(&quot;4-Yr Cum. Event Rate\n PCI&quot;,&quot;\n&quot;,data$PCI.Group), 
                    c(&quot;4-Yr Cum. Event Rate\n Medical Therapy&quot;,&quot;\n&quot;,data$Medical.Therapy.Group), 
                    c(&quot;P Value&quot;,&quot;\n&quot;,data$P.Value))
</pre>
<p>Finally, include the <em>forestplot</em> R package and call the <em>forestplot</em> function with appropriate arguments.</p>
<p>The way I got around to creating the horizontal band at every alternate row was by using settings for a very thick transparent line in the <em>hrzl_lines</em> argument! See below. The <em>col=”#99999922″</em> option gives the light grey color to the line as well as sets it to be transparent.</p>
<p>A graphics device (here, a png file) with appropriate dimensions is first opened and the forest plot is saved to the device.</p>
<pre>
library(forestplot)
png(file.path(workdir,&quot;Figures\\Forestplot.png&quot;),width=960, height=640)
forestplot(labeltext=tabletext, graph.pos=3, 
           mean=c(NA,NA,data$Point.Estimate), 
           lower=c(NA,NA,data$Low), upper=c(NA,NA,data$High),
           title=&quot;Hazard Ratio&quot;,
           xlab=&quot;     &lt;---PCI Better---    ---Medical Therapy Better--->&quot;,
           hrzl_lines=list(&quot;3&quot; = gpar(lwd=1, col=&quot;#99999922&quot;), 
                          &quot;7&quot; = gpar(lwd=60, lineend=&quot;butt&quot;, columns=c(2:6), col=&quot;#99999922&quot;),
                          &quot;15&quot; = gpar(lwd=60, lineend=&quot;butt&quot;, columns=c(2:6), col=&quot;#99999922&quot;),
                          &quot;23&quot; = gpar(lwd=60, lineend=&quot;butt&quot;, columns=c(2:6), col=&quot;#99999922&quot;),
                          &quot;31&quot; = gpar(lwd=60, lineend=&quot;butt&quot;, columns=c(2:6), col=&quot;#99999922&quot;)),
           txt_gp=fpTxtGp(label=gpar(cex=1.25),
                              ticks=gpar(cex=1.1),
                              xlab=gpar(cex = 1.2),
                              title=gpar(cex = 1.2)),
           col=fpColors(box=&quot;black&quot;, lines=&quot;black&quot;, zero = &quot;gray50&quot;),
           zero=1, cex=0.9, lineheight = &quot;auto&quot;, boxsize=0.5, colgap=unit(6,&quot;mm&quot;),
           lwd.ci=2, ci.vertices=TRUE, ci.vertices.height = 0.4)
dev.off()
</pre>
<p>Here is the resulting forest plot:</p>
<p><img alt="Forestplot" class="wp-image-256" src="http://i1.wp.com/designdatadecisions.files.wordpress.com/2016/07/forestplot1.png?resize=456%2C304&ssl=1" data-recalc-dims="1"></p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/designdatadecisions.wordpress.com/250/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/designdatadecisions.wordpress.com/250/" border="0"></a> <img alt="" src="http://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/forest-plot-with-horizontal-bands/"></div></div><div><a data-url="http://www.r-bloggers.com/forest-plot-with-horizontal-bands/" data-counturl="http://www.r-bloggers.com/forest-plot-with-horizontal-bands/" data-count="vertical" data-text="Forest Plot (with Horizontal Bands)" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://designdatadecisions.wordpress.com/2016/07/02/forest-plot-with-horizontal-bands/"> R – Design Data Decisions</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/_GBlGI682VM"></a></div>