<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/setting-ggplot2-background-with-ggbackground/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://guangchuangyu.github.io/2018/04/setting-ggplot2-background-with-ggbackground/"> R on Guangchuang YU</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://cran.r-project.org/package=ggimage" target="_blank">ggimage</a> 0.1.4 is available on CRAN.</p>
<p>This release introduces a new function called <code>ggbackground</code> for setting image background as <code>ggplot</code> canvas.</p>
<pre><code>require(ggplot2)
p &lt;- ggplot(iris) + aes(x = Sepal.Length, y = Sepal.Width, color=Species) + 
    geom_point(size=5) + theme_classic()</code></pre>
<p>Suppose we have the above <code>ggplot</code> object, <code>p</code>, the only thing we need to do is passing the <code>p</code> with an image file name (local or remote) to <code>ggbackground</code>, as demonstrated below:</p>
<p><span></span></p>
<pre><code>require(ggimage) 
img = &quot;https://assets.bakker.com/ProductPics/560x676/10028-00-BAKI_20170109094316.jpg&quot;
ggbackground(p, img)</code></pre>
<p><img src="https://i0.wp.com/guangchuangyu.github.io/post/R/ggimage/ggbackground_files/figure-html/unnamed-chunk-3-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>This image file is too colorful and it is hard to see the patterns of the data points. We can manipulate the image using the functions provided by <code>magick</code> package. For example, the following function emulate a classic high-pass filter from photoshop:</p>
<pre><code>require(magick)
ggbackground(p, img, 
    image_fun = function(x) image_negate(image_convolve(x, 'DoG:0,0,2')))</code></pre>
<p><img src="https://i0.wp.com/guangchuangyu.github.io/post/R/ggimage/ggbackground_files/figure-html/unnamed-chunk-4-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>Here are examples of setting transparency and color:</p>
<pre><code>img = &quot;http://phylopic.org/assets/images/submissions/bf5fe2c5-1247-4ed9-93e2-d5af255ec462.512.png&quot;
p1 = ggbackground(p, img) + ggtitle(&quot;ggbackground(p, img)&quot;)
p2 = ggbackground(p, img, alpha=.3) + ggtitle(&quot;ggbackground(p, img, alpha=.3)&quot;)
p3 = ggbackground(p, img, alpha=.3, color=&quot;steelblue&quot;) + ggtitle('ggbackground(p, img, alpha=.3, color=&quot;steelblue&quot;)')
cowplot::plot_grid(p1, p2, p3, ncol=3)</code></pre>
<p><img src="https://i2.wp.com/guangchuangyu.github.io/post/R/ggimage/ggbackground_files/figure-html/unnamed-chunk-5-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<div>
<h2>Links</h2>
<ul>
<li>ggimage package: <a rel="nofollow" href="https://cran.r-project.org/package=ggimage" target="_blank">CRAN</a>, <a rel="nofollow" href="https://github.com/GuangchuangYu/ggimage" target="_blank">GitHub</a></li>
<li><a rel="nofollow" href="https://cran.r-project.org/web/packages/ggimage/vignettes/ggimage.html" target="_blank">ggimage introduction</a></li>
<li><a rel="nofollow" href="https://guangchuangyu.github.io/software/ggtree/vignettes/ggtree-ggimage.html" target="_blank">Annotating phylogenetic tree with images using ggtree and ggimage</a></li>
</ul>
</div>
	<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://guangchuangyu.github.io/2018/04/setting-ggplot2-background-with-ggbackground/"> R on Guangchuang YU</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/HonPXdKBfAc" height="1"></a></div>