<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/saving-high-resolution-ggplots-how-to-preserve-semi-transparency/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.sthda.com/english/wiki/saving-high-resolution-ggplots-how-to-preserve-semi-transparency"> Easy Guides</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p></p>
<p> </p>
<div>
<p></p>
<p>This article describes solutions for preserving semi-<strong>transparency</strong> when <strong>saving</strong> a <strong>ggplot2</strong>-based graphs into a high quality <strong>postscript</strong> (.eps) file format.</p>
<p><img alt="Saving High-Reslution ggplots" src="https://i0.wp.com/www.sthda.com/sthda/RDoc/images/saving-high-resolution-ggplots.png?w=456" data-recalc-dims="1"></p>
<p><strong>Contents:</strong></p>
<div>
<ul>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/saving-high-resolution-ggplots-how-to-preserve-semi-transparency#create-a-ggplot-with-semi-transparent-color" target="_blank">Create a ggplot with semi-transparent color</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/saving-high-resolution-ggplots-how-to-preserve-semi-transparency#save-ggplots-with-semi-transparent-colors" target="_blank">Save ggplots with semi-transparent colors</a>
<ul>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/saving-high-resolution-ggplots-how-to-preserve-semi-transparency#use-cairo-based-postscript-graphics-devices" target="_blank">Use cairo-based postscript graphics devices</a></li>
<li><a rel="nofollow" href="http://www.sthda.com/english/wiki/saving-high-resolution-ggplots-how-to-preserve-semi-transparency#export-to-powerpoint" target="_blank">Export to powerpoint</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h2>Create a ggplot with semi-transparent color</h2>
<p>To illustrate this, we start by creating ggplot2-based survival curves using the function <a rel="nofollow" href="http://www.sthda.com/english/rpkgs/survminer/reference/ggsurvplot.html" target="_blank">ggsurvplot()</a> in the <em>survminer</em> package. The <em>ggsurvplot</em>() function creates survival curves with the 95% confidence bands in a semi-transparent color.</p>
<p>First install (if needed) survminer as follow:</p>
<pre><code>install.packages(&quot;survminer&quot;)</code></pre>
<p>Then type, this:</p>
<pre><code># Fit survival curves
require(&quot;survival&quot;)
fit&lt;- survfit(Surv(time, status) ~ sex, data = lung)

# Visualize
library(&quot;survminer&quot;)
p &lt;- ggsurvplot(fit, data = lung,
         surv.median.line = &quot;hv&quot;, # Add medians survival
         pval = TRUE,             # Add p-value and tervals
        
         conf.int = TRUE,        # Add the 95% confidence band
         risk.table = TRUE,      # Add risk table
         tables.height = 0.2,
         tables.theme = theme_cleantable(),
         palette = &quot;jco&quot;,
         ggtheme = theme_bw()
    )
print(p)</code></pre>
<p><img src="https://i0.wp.com/www.sthda.com/sthda/RDoc/figure/sthda/preserve-semi-transparency-ggplot2-and-eps-survival-curves-1.png?w=450" data-recalc-dims="1"></p>
<div>
<p>
In the plot above, the confidence band is semi-transparent. It can be saved to a PDF file without loosing the semi-transparent color.
</p>
</div>
<p>If you try to export the picture as vector file (EPS or SVG, …), the 95% confidence interval will disappear and the saved plot looks as follow:</p>
<p><img alt="ggsurvplot in .eps format: confidence bands disapear" src="https://i2.wp.com/www.sthda.com/sthda/RDoc/images/ggsurvplot-eps-confidence-bands-disapear.png?w=456" data-recalc-dims="1"></p>
<p>The problem is that EPS in R does not support transparency.</p>
<div>
<p>
In the following sections, we’ll describe convenient solutions to save high-quality ggplots by preserving semi-transparency.
</p>
</div>
</div>
<div>
<h2>Save ggplots with semi-transparent colors</h2>
<div>
<h3>Use cairo-based postscript graphics devices</h3>
<p>You can use the <strong>ggsave</strong>() function in [ggplot2] as follow:</p>
<pre><code>ggsave(filename = &quot;survival-curves.eps&quot;,
       plot = print(p),
       device = cairo_eps)</code></pre>
<p>Or use this:</p>
<pre><code>cairo_ps(filename = &quot;survival-curves.eps&quot;,
         width = 7, height = 7, pointsize = 12,
         fallback_resolution = 300)

print(p)

dev.off()</code></pre>
<div>
<p>
Note that, the argument <em>fallback_resolution</em> is used to control the resolution in dpi at which semi-transparent areas are rasterized (the rest stays as vector format).
</p>
</div>
</div>
<div>
<h3>Export to powerpoint</h3>
<p>You can export the plot to Powerpoint using the <strong>ReporteRs</strong> package. ReporteRs will give you a fully editable vector format with full support for transparency as well.</p>
<p>We previously described how to <a rel="nofollow" href="http://www.sthda.com/english/wiki/create-and-format-powerpoint-documents-from-r-software" target="_blank">Create and format PowerPoint documents from R software using the ReporteRs package</a>. We also described how to <a rel="nofollow" href="http://www.sthda.com/english/wiki/create-an-editable-graph-from-r-software" target="_blank">export an editable ggplot from R software to powerpoint</a>.</p>
<p>Briefly, to export our survival curves from R to powerpoint, the script looks like this</p>
<pre><code>library('ReporteRs')

# Create a new powerpoint document
doc &lt;- pptx()

# Add a new slide into the ppt document 
doc &lt;- addSlide(doc, slide.layout = &quot;Two Content&quot;  )

# Add a slide title
doc &lt;- addTitle(doc, &quot;Survival Curves: Editable Vector Graphics&quot; )

# Print the survival curves in the powerpoint
doc &lt;- addPlot(doc, function() print(p, newpage = FALSE), 
               vector.graphic = TRUE  # Make it editable
               )

# write the document to a file
writeDoc(doc, file = &quot;editable-survival-curves.pptx&quot;)</code></pre>
<p>The output looks like this:</p>
<p><img alt="Editable survival curves" src="https://i1.wp.com/www.sthda.com/sthda/RDoc/images/editable-survival-curves.png?w=456" data-recalc-dims="1"></p>
<p>Edit the plot in powerpoint. See the video below: <a rel="nofollow" href="https://www.youtube.com/edit?o=U&video_id=Yprmq8AUf8g" target="_blank">Editing ggplots Exported with ReporteRs into PWPT</a></p>
<p><iframe allowfullscreen src="https://youtube.com/embed/Yprmq8AUf8g" width="450" type="text/html"><br>
</iframe>
</div>
</div>
<div>
<h2>Infos</h2>
</div>
<p></p>

</div>
<p><br>
</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="http://www.sthda.com/english/wiki/saving-high-resolution-ggplots-how-to-preserve-semi-transparency"> Easy Guides</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/GoEFOuka41A" height="1"></a></div>