<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/creat-meme-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://guangchuangyu.github.io/2017/10/creat-meme-in-r/"> R on Guangchuang YU</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I developed a tiny toy package, <code>meme</code>, which is now on CRAN. As it’s name indicated, it was designed to create memes, which are captioned photos that are intended to be funny, riduculous.</p>
<h2><code>meme()</code></h2>
<p>The package is quite simple. You can use <code>meme()</code> function to add meme captions, and this is all the package supposed to do:</p>
<pre><code>library(meme)
u &lt;- &quot;http://www.happyfamilyneeds.com/wp-content/uploads/2017/08/angry8.jpg&quot;
meme(u, &quot;code&quot;, &quot;all the things!&quot;)
</code></pre>
<p><img alt="" src="https://i1.wp.com/guangchuangyu.github.io/blog_images/R/meme/Figs/unnamed-chunk-2-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>the grammar <img alt="🙈" src="https://s.w.org/images/core/emoji/2.3/72x72/1f648.png" class="wp-smiley"></h2>
<p>The <code>meme</code> package was implemented using <code>grid</code> graphic system. Since <code>grid</code> is the most flexible graphic system in R, I try to mimic <code>ggplot2</code> (although very superficial) for practice.</p>
<p>User can use <code>mmplot()</code> to read and plot the input image and then using <code>+ mm_caption()</code> to add meme captions.</p>
<pre><code>mmplot(u) + mm_caption(&quot;calm down&quot;, &quot;and RTFM&quot;, color=&quot;purple&quot;)
</code></pre>
<p><img alt="" src="https://i2.wp.com/guangchuangyu.github.io/blog_images/R/meme/Figs/unnamed-chunk-3-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2><code>meme_save()</code></h2>
<p>The <code>meme</code> output can be saved as an object, and can be exported to file using <code>ggsave()</code>. Since we would like to keep the original figure aspection ratio for output meme figure, I provide a helper function, <code>meme_save()</code>, which takes care of the figure aspection ratio and then called <code>ggsave()</code> to export the figure.</p>
<pre><code>u2 &lt;- &quot;http://i0.kym-cdn.com/entries/icons/mobile/000/000/745/success.jpg&quot;
x &lt;- meme(u2, &quot;please&quot;, &quot;tell me more&quot;)
meme_save(x, file=&quot;</code><code>docs/Figs/meme.png</code><code>&quot;) </code></pre>
<p><img src="https://i2.wp.com/guangchuangyu.github.io/blog_images/R/meme/Figs/meme.png?w=450&ssl=1" data-recalc-dims="1"></p>
<h2>the <code>plot</code> method</h2>
<p>Users can <code>plot</code> the <code>meme()</code> output and change the caption or other parameters in real time.</p>
<pre><code>plot(x, size = 2, &quot;happy friday!&quot;, &quot;wait, sorry, it's monday&quot;, color = &quot;firebrick&quot;, font = &quot;Courier&quot;)
</code></pre>
<p><img alt="" src="https://i1.wp.com/guangchuangyu.github.io/blog_images/R/meme/Figs/unnamed-chunk-5-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>the <code>+</code> method</h2>
<p>Instead of using parameters in <code>plot()</code> explictely, Users can use <code>+ aes()</code> to set the plot parameters:</p>
<pre><code>x + aes(upper = &quot;#barbarplots&quot;,
        lower = &quot;friends don't let friends make bar plots&quot;,
        color = firebrick, font = Courier, size=1.5)
</code></pre>
<p><img alt="" src="https://i0.wp.com/guangchuangyu.github.io/blog_images/R/meme/Figs/unnamed-chunk-6-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>or using <code>+ list()</code>. The following command will also generate the figure displayed above.</p>
<pre><code>x + list(upper = &quot;#barbarplots&quot;,
        lower = &quot;friends don't let friends make bar plots&quot;,
        color = &quot;firebrick&quot;, font = &quot;Courier&quot;, size=1.5)
</code></pre>
<h2>multi-language support</h2>
<p>I didn’t do anything about it. Multi-language was supported internally. Just simply select a font for your language.</p>
<pre><code>y &lt;- meme(u, &quot;卧槽&quot;, &quot;听说你想用中文&quot;, font=&quot;STHeiti&quot;)
y
</code></pre>
<p><img alt="" src="https://i2.wp.com/guangchuangyu.github.io/blog_images/R/meme/Figs/unnamed-chunk-8-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<hr>
<p>As the <code>meme</code> package was developed using <code>grid</code>, It would be better to provide function to convert the output object to <code>grob</code>. Similar to <code>ggplotGrob()</code> for <code>ggplot</code> object, I provide <code>memeGrob()</code> for the <code>meme</code> object and this making it possible to edit the details of the graph and compatible with the <code>grid</code> ecosystem.</p>
<p>Here are the examples of using <code>meme</code> in <code>grid</code>, <code>ggplot2</code> and <code>cowplot</code>.</p>
<h2>grid support</h2>
<pre><code>library(grid)
mm &lt;- meme(u, &quot;code&quot;, &quot;all the things!&quot;, size=.3, color='firebrick')

grid.newpage()
pushViewport(viewport(width=.9, height=.9))
grid.rect(gp = gpar(lty=&quot;dashed&quot;))

xx &lt;- seq(0, 2*pi , length.out=10)
yy &lt;- sin(xx)

for (i in seq_along(xx)) {
    vp &lt;- viewport(x = xx[i]/(2*pi), y = (yy[i]-min(yy))/2, width=.05, height=.05)
    print(mm, vp = vp)
}
</code></pre>
<p><img alt="" src="https://i1.wp.com/guangchuangyu.github.io/blog_images/R/meme/Figs/unnamed-chunk-9-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>ggplot2 support</h2>
<pre><code>library(ggplot2)
library(ggimage)

d &lt;- data.frame(x = xx, y = yy)
ggplot(d, aes(x, y)) + geom_line() +
    geom_subview(mm, x = xx, y = yy, width=.3, height=.15)
</code></pre>
<p><img alt="" src="https://i1.wp.com/guangchuangyu.github.io/blog_images/R/meme/Figs/unnamed-chunk-10-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre><code>ggplot(d, aes(x, y)) +
    geom_subview(mm+aes(size=3), x=0, y=0, width=Inf, height=Inf) +
    geom_point() + geom_line()
</code></pre>
<p><img alt="" src="https://i2.wp.com/guangchuangyu.github.io/blog_images/R/meme/Figs/unnamed-chunk-10-2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>cowplot support</h2>
<pre><code>cowplot::plot_grid(x, y, ncol=1, labels = c(&quot;A&quot;, &quot;B&quot;))
</code></pre>
<p><img alt="" src="https://i1.wp.com/guangchuangyu.github.io/blog_images/R/meme/Figs/unnamed-chunk-11-1.png?w=456&ssl=1" data-recalc-dims="1"></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://guangchuangyu.github.io/2017/10/creat-meme-in-r/"> 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/qMv4MU43K5U" height="1"></a></div>