<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/survival-plots-have-never-been-so-informative/"></div></div><div><a data-url="http://www.r-bloggers.com/survival-plots-have-never-been-so-informative/" data-counturl="http://www.r-bloggers.com/survival-plots-have-never-been-so-informative/" data-count="horizontal" data-text="Survival plots have never been so informative" 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="http://r-addict.com/2016/05/23/Informative-Survival-Plots.html"> http://r-addict.com</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img src="http://i0.wp.com/r-addict.com/images/fulls/survminer_orly.png?w=456" data-recalc-dims="1"><a target="_blank" href="http://www.r-bloggers.com/ggplot-2-0-0/">Hadley Wickham’s ggplot2 version 2.0 revolution</a>, at the end of 2015, triggered many crashes in dependent R packages, that finally led to deletions of few packages from <a target="_blank" href="https://cran.r-project.org/">The Comprehensive R Archive Network</a>. It occured that <code>survMisc</code> package was removed from CRAN on 27th of January 2016 and R world remained helpless in the struggle with the elegant visualizations of survival analysis. Then a new tool – <a target="_blank" href="https://cran.rstudio.com/web/packages/survminer/index.html">survminer</a> package, created by <a target="_blank" href="https://github.com/kassambara">Alboukadel Kassambara</a> – appeared on the R survival scene to fill the gap in visualizing the Kaplan-Meier estimates of survival curves in elegant grammar of graphics like way. This blog presents main features of core <code>ggsurvplot()</code> function from survminer package, <em>which creates the most informative, elegant and flexible survival plots that I have seen!</em></p>
<p>During the development of <a target="_blank" href="http://rtcga.github.io/RTCGA/">RTCGA</a> package (about which I wrote <a target="_blank" href="http://r-addict.com/2016/05/04/RTCGA-Quick-Guide.html">here</a>) <a target="_blank" href="https://github.com/orgs/RTCGA/people">we</a> encountered a need to provide the simplest possible interface to estimates of survival curves for biotechnologists and <a target="_blank" href="http://www.sthda.com/english/wiki/survminer-r-package-survival-data-analysis-and-visualization">the discovery of <code>ggsurvplot()</code></a> was a bull’s-eye! <a target="_blank" href="https://github.com/hadley/ggplot2/issues/1080">Many have tried</a> to provide a package or function for ggplot2-like plots that would present the basic tool of survival analysis: Kaplan-Meier estimates of survival curves, but none of earlier attempts have provided such a rich structure of features and flexibility as survminer.<br>
On basis of estimates of survival curves one can infere on differences in survival times between compared groups, so survival plots are very useful and can be seen in almost every publication in the field of survival analysis and time to event models.</p>
<h1>Example</h1>
<p>After regular installation</p>
<figure>
<pre><code data-lang="r"><span>install.packages</span><span>(</span><span>'survminer'</span><span>)</span>
<span>source</span><span>(</span><span>&quot;https://bioconductor.org/biocLite.R&quot;</span><span>)</span>
<span>biocLite</span><span>(</span><span>&quot;RTCGA.clinical&quot;</span><span>)</span> <span>#</span> <span>data</span> <span>for</span> <span>examples</span></code></pre>
</figure>
<p>we can create simple estimates of survival curves just after we put <code>survfit</code> (survival package) object into <code>ggsurvplot()</code> function. Let’s have a look at differences in survival times between patients suffering from Ovarian Cancer (<em>Ovarian serous cystadenocarcinoma</em>) and patients suffering from Breast Cancer (<em>Breast invasive carcinoma</em>), where data were collected by <a target="_blank" href="http://cancergenome.nih.gov/">The Cancer Genome Atlas Project</a>.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>RTCGA.clinical</span><span>)</span>
<span>survivalTCGA</span><span>(</span><span>BRCA.clinical</span><span>,</span> <span>OV.clinical</span><span>,</span>
             <span>extract.cols</span> <span>=</span> <span>&quot;admin.disease_code&quot;</span><span>)</span> <span>-></span> <span>BRCAOV.survInfo</span>
<span>library</span><span>(</span><span>survival</span><span>)</span>
<span>fit</span> <span>&lt;-</span> <span>survfit</span><span>(</span><span>Surv</span><span>(</span><span>times</span><span>,</span> <span>patient.vital_status</span><span>)</span> <span>~</span> <span>admin.disease_code</span><span>,</span>
               <span>data</span> <span>=</span> <span>BRCAOV.survInfo</span><span>)</span>
<span>library</span><span>(</span><span>survminer</span><span>)</span>
<span>ggsurvplot</span><span>(</span><span>fit</span><span>,</span> <span>risk.table</span> <span>=</span> <span>TRUE</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-3" src="http://i0.wp.com/r-addict.com/figure/source/2016-05-23-Informative-Survival-Plots/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p>This simple plot presents, in an elegant way, estimates of survival probability depending on days from cancer diagnostics grouped by cancer types and an informative risk set table, which shows the number of patients that were under observation in the specific period of time. Survival analysis is a specific field of data analysis because of the censored time to event data, so risk set size is a must in visual inference.</p>
<p>After few improvements (<a target="_blank" href="https://github.com/kassambara/survminer/issues/1">#1</a>, <a target="_blank" href="https://github.com/kassambara/survminer/issues/2">#2</a>, <a target="_blank" href="https://github.com/kassambara/survminer/issues/3">#3</a>, <a target="_blank" href="https://github.com/kassambara/survminer/issues/4">#4</a>, <a target="_blank" href="https://github.com/kassambara/survminer/issues/7">#7</a>, <a target="_blank" href="https://github.com/kassambara/survminer/issues/8">#8</a>, <a target="_blank" href="https://github.com/kassambara/survminer/issues/12">#12</a>,<br>
<a target="_blank" href="https://github.com/kassambara/survminer/issues/28">#28</a>), made by Alboukadel in version 0.2.0, one can create a powerful, informative survival plot with such specification of parameters</p>
<figure>
<pre><code data-lang="r"><span>ggsurvplot</span><span>(</span>
   <span>fit</span><span>,</span>                     <span># survfit object with calculated statistics.
</span>   <span>risk.table</span> <span>=</span> <span>TRUE</span><span>,</span>       <span># show risk table.
</span>   <span>pval</span> <span>=</span> <span>TRUE</span><span>,</span>             <span># show p-value of log-rank test.
</span>   <span>conf.int</span> <span>=</span> <span>TRUE</span><span>,</span>         <span># show confidence intervals for </span>                            <span># point estimaes of survival curves.
</span>   <span>xlim</span> <span>=</span> <span>c</span><span>(</span><span>0</span><span>,</span><span>2000</span><span>),</span>        <span># present narrower X axis, but not affect
</span>                            <span># survival estimates.
</span>   <span>break.time.by</span> <span>=</span> <span>500</span><span>,</span>     <span># break X axis in time intervals by 500.
</span>   <span>ggtheme</span> <span>=</span> <span>theme_RTCGA</span><span>(),</span> <span># customize plot and risk table with a theme.
</span> <span>risk.table.y.text.col</span> <span>=</span> <span>T</span><span>,</span> <span># colour risk table text annotations.
</span>  <span>risk.table.y.text</span> <span>=</span> <span>FALSE</span> <span># show bars instead of names in text annotations
</span>                            <span># in legend of risk table
</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-4" src="http://i0.wp.com/r-addict.com/figure/source/2016-05-23-Informative-Survival-Plots/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p>Each parameter is described in the correspoding comment, but I would like to emphasize the <code>xlim</code> parameter which controls limits of the X axis but does not affect the survival curves, that are taking into account all possible times. Another brilliant parameter is <code>break.time.by</code> that affects survival plots and the risk set table – this would not be so easy to create it by yourself. Also a <code>ggtheme</code> parameter is beneficial for simple plot customization. Finally, <code>risk.table.y.text</code> and <code>risk.table.y.text.col</code> (for which I have provided a user requests) are useful parameters that change text (often too long and redundand) from risk table legend into narrow, coloured bar. This saves a lot of space in the final plot.</p>
<h1>Comparisons</h1>
<p>Even though <a target="_blank" href="https://cran.rstudio.com/web/packages/survMisc/index.html"><code>survMisc</code></a> have returned on CRAN, I’ve appreciated survminer so much that I would not look anymore for other solutions. Check why: at the end I present survival curves that can be obtained with the usage of <code>base</code> package and <code>survMisc</code> package.</p>
<h3>base package</h3>
<figure>
<pre><code data-lang="r"><span>plot</span><span>(</span><span>fit</span><span>)</span> <span>#</span> <span>base</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-5" src="http://i1.wp.com/r-addict.com/figure/source/2016-05-23-Informative-Survival-Plots/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<p>It looks pretty… base…</p>
<figure>
<pre><code data-lang="r"><span>plot</span><span>(</span><span>fit</span><span>,</span> <span>col</span><span>=</span><span>c</span><span>(</span><span>&quot;orange&quot;</span><span>,</span><span>&quot;purple&quot;</span><span>),</span> <span>lty</span><span>=</span><span>c</span><span>(</span><span>1</span><span>:</span><span>2</span><span>),</span> <span>lwd</span><span>=</span><span>3</span><span>,</span> <span># base with some customization
</span>     <span>conf.int</span> <span>=</span> <span>TRUE</span><span>,</span> <span>xmax</span> <span>=</span> <span>2000</span><span>)</span>
<span># add a legend
</span><span>legend</span><span>(</span><span>100</span><span>,</span> <span>.2</span><span>,</span> <span>c</span><span>(</span><span>&quot;Ovarian Cancer&quot;</span><span>,</span> <span>&quot;Breast Cancer&quot;</span><span>),</span> 
       <span>lty</span> <span>=</span> <span>c</span><span>(</span><span>1</span><span>:</span><span>2</span><span>),</span> <span>col</span><span>=</span><span>c</span><span>(</span><span>&quot;orange&quot;</span><span>,</span><span>&quot;purple&quot;</span><span>))</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-6" src="http://i0.wp.com/r-addict.com/figure/source/2016-05-23-Informative-Survival-Plots/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"></p>
<p>I haven’t seen examples with risk table and adding legend isn’t so quick as in survminer. Moreover, there are no minor axis breaks lines.</p>
<h3>survMisc package</h3>
<figure>
<pre><code data-lang="r"><span># install.packages('survMisc')
</span><span>library</span><span>(</span><span>survMisc</span><span>)</span>
<span>survMisc</span><span>:::</span><span>autoplot.survfit</span><span>(</span><span>fit</span><span>)</span> <span>#</span> <span>no</span> <span>customization</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-7" src="http://i0.wp.com/r-addict.com/figure/source/2016-05-23-Informative-Survival-Plots/unnamed-chunk-7-1.png?w=456" data-recalc-dims="1"></p>
<p>Why colours are not asigned to any group? Where is the legend? Why there is so much white space to the right?</p>
<figure>
<pre><code data-lang="r"><span>survMisc</span><span>:::</span><span>autoplot.survfit</span><span>(</span> <span># with some hard customization
</span>   <span>fit</span><span>,</span>
   <span>type</span> <span>=</span> <span>&quot;fill&quot;</span><span>,</span>
   <span>pVal</span><span>=</span><span>TRUE</span>
<span>)</span> <span>-></span> <span>fit.survMisc</span> 
<span>fit.survMisc</span><span>$</span><span>table</span> <span>&lt;-</span> <span>fit.survMisc</span><span>$</span><span>table</span> <span>+</span>
   <span>theme_RTCGA</span><span>()</span> <span>+</span> <span># theme(legend.position = &quot;top&quot;)
</span>   <span>coord_cartesian</span><span>(</span><span>xlim</span> <span>=</span> <span>c</span><span>(</span><span>0</span><span>,</span><span>2000</span><span>))</span>
<span>fit.survMisc</span><span>$</span><span>plot</span> <span>&lt;-</span> <span>fit.survMisc</span><span>$</span><span>plot</span> <span>+</span>
   <span>theme_RTCGA</span><span>()</span> <span>+</span> 
   <span>coord_cartesian</span><span>(</span><span>xlim</span> <span>=</span> <span>c</span><span>(</span><span>0</span><span>,</span><span>2000</span><span>))</span>
<span>survMisc</span><span>:::</span><span>print.tableAndPlot</span><span>(</span><span>fit.survMisc</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-8" src="http://i0.wp.com/r-addict.com/figure/source/2016-05-23-Informative-Survival-Plots/unnamed-chunk-8-1.png?w=456" data-recalc-dims="1"></p>
<p>Where is risk table? Why I can’t pass <code>break.by.time</code> to have informative minor X axis breaks? Why the plot gets narrower when the legend in risk table gets wider and why I can’t do anything to workaround this?</p>
<blockquote>
<p>Never mind -> <code>install.packages('survminer')</code></p>
</blockquote>
<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/survival-plots-have-never-been-so-informative/"></div></div><div><a data-url="http://www.r-bloggers.com/survival-plots-have-never-been-so-informative/" data-counturl="http://www.r-bloggers.com/survival-plots-have-never-been-so-informative/" data-count="vertical" data-text="Survival plots have never been so informative" 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="http://r-addict.com/2016/05/23/Informative-Survival-Plots.html"> http://r-addict.com</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/UeSzBIoyDJs"></a></div>