<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/combine-your-hex-stickers-with-magick/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.masalmon.eu/2018/02/22/hexcombine/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Hex stickers remind me of <a rel="nofollow" href="https://en.wikipedia.org/wiki/Milk_caps_(game)" target="_blank">Pogs</a>, except they’re cooler because you can combine them together! Some people do that very smartly.</p>
<blockquote class="twitter-tweet" data-lang="ca">
<p lang="en" dir="ltr">Now when I forget how to do an <a rel="nofollow" href="https://twitter.com/hashtag/rstats?src=hash&ref_src=twsrc%5Etfw" target="_blank">#rstats</a> analysis I can just check the back of my laptop <a rel="nofollow" href="https://twitter.com/hashtag/runconf16?src=hash&ref_src=twsrc%5Etfw" target="_blank">#runconf16</a> <a rel="nofollow" href="https://t.co/DY7UgeBzYV" target="_blank">pic.twitter.com/DY7UgeBzYV</a></p>
<p>— David Robinson (@drob) <a rel="nofollow" href="https://twitter.com/drob/status/715694466707750913?ref_src=twsrc%5Etfw" target="_blank">1 d’abril de 2016</a></p></blockquote>
<p>I’ve got a pretty random hex stickers combination on my laptop, but after all it could be worse…</p>
<blockquote class="twitter-tweet" data-lang="ca">
<p lang="en" dir="ltr">My boyfriend decided he is post-hex / post-<a rel="nofollow" href="https://twitter.com/hashtag/tidyverse?src=hash&ref_src=twsrc%5Etfw" target="_blank">#tidyverse</a> <a rel="nofollow" href="https://t.co/ADtyORMxHL" target="_blank">pic.twitter.com/ADtyORMxHL</a></p>
<p>— Hilary Parker (@hspter) <a rel="nofollow" href="https://twitter.com/hspter/status/908102841323188225?ref_src=twsrc%5Etfw" target="_blank">13 de setembre de 2017</a></p></blockquote>
<p>Now since I’m a <code>magick</code>/collage fan, you can bet I’ve wondered how to use R in order to combine stickers automatically! Say I have a bunch of sticker PNGs, how could I produce a map to design my laptop style? Read to find out more…</p>
<p><span></span></p>
<h1>Getting some hex stickers to play with</h1>
<p>I do agree that in real life you’ll most often not have PNGs of the stickers but hey I can dream… especially since I’m sure that one will soon be able to identify stickers on a conference haul pic in the blink of an eye with the <a rel="nofollow" href="https://github.com/swarm-lab/Rvision" target="_blank"><code>Rvision</code> package</a>. I got stickers from <a rel="nofollow" href="http://hexb.in/" target="_blank">hex.bin</a> whose existence I learnt from this tweet:</p>
<blockquote class="twitter-tweet" data-lang="ca">
<p lang="en" dir="ltr">A plea to my favorite <a rel="nofollow" href="https://twitter.com/hashtag/rstats?src=hash&ref_src=twsrc%5Etfw" target="_blank">#rstats</a> <img alt="📦" src="https://s.w.org/images/core/emoji/2.3/72x72/1f4e6.png" class="wp-smiley"> developers: please submit your BIG hex sticker image files (with vector versions <img alt="🙏" src="https://s.w.org/images/core/emoji/2.3/72x72/1f64f.png" class="wp-smiley">) to <a rel="nofollow" href="https://t.co/7KWg3d2fZA" target="_blank">https://t.co/7KWg3d2fZA</a>! <a rel="nofollow" href="https://twitter.com/old_man_chester?ref_src=twsrc%5Etfw" target="_blank">@old_man_chester</a> <a rel="nofollow" href="https://twitter.com/nj_tierney?ref_src=twsrc%5Etfw" target="_blank">@nj_tierney</a> <a rel="nofollow" href="https://twitter.com/samfirke?ref_src=twsrc%5Etfw" target="_blank">@samfirke</a> they will be right at home <img alt="👇" src="https://s.w.org/images/core/emoji/2.3/72x72/1f447.png" class="wp-smiley"> <a rel="nofollow" href="https://t.co/AIqgnBIx1X" target="_blank">pic.twitter.com/AIqgnBIx1X</a></p>
<p>— Alison Hill (@apreshill) <a rel="nofollow" href="https://twitter.com/apreshill/status/966397975328038912?ref_src=twsrc%5Etfw" target="_blank">21 de febrer de 2018</a></p></blockquote>
<p>And well if more R packages are added to it my <code>Rvision</code> dream could come true.</p>
<p>Note: When I say I got them from hex.bin, I mean I lazily downloaded the “hexagons/” folder of <a rel="nofollow" href="https://github.com/maxogden/hexbin" target="_blank">this GitHub repository</a> by using <a rel="nofollow" href="https://stackoverflow.com/a/38879691" target="_blank">a link as explained in this SO answer</a>.</p>
<h1>Combining a few of them at random</h1>
<p>I’ll sample <a rel="nofollow" href="https://en.wikipedia.org/wiki/Phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy#Answer_to_the_Ultimate_Question_of_Life,_the_Universe,_and_Everything_(42)" target="_blank">42</a> stickers from my nice little internet haul.</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;magrittr&quot;</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>42</span><span>)</span><span>
</span><span>ultimate_sample</span><span> </span><span>&lt;-</span><span> </span><span>fs</span><span>::</span><span>dir_ls</span><span>(</span><span>&quot;hexagons&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>sample</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>42</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>After that I’ll create rows which is pretty straightforward.</p>
<div>
<div>
<pre><code><span>no_row</span><span> </span><span>&lt;-</span><span> </span><span>6</span><span>
</span><span>no_col</span><span> </span><span>&lt;-</span><span> </span><span>7</span><span>
</span><span>row_paths</span><span> </span><span>&lt;-</span><span> </span><span>split</span><span>(</span><span>ultimate_sample</span><span>,</span><span> </span><span>rep</span><span>(</span><span>1</span><span>:</span><span>no_col</span><span>,</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>no_row</span><span>))</span><span>
</span><span># let's be crazy</span><span>
</span><span>read_append</span><span> </span><span>&lt;-</span><span> </span><span>.</span><span> </span><span>%>%</span><span>
</span><span>magick</span><span>::</span><span>image_read</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>magick</span><span>::</span><span>image_append</span><span>()</span><span>
</span><span>rows</span><span> </span><span>&lt;-</span><span> </span><span>purrr</span><span>::</span><span>map</span><span>(</span><span>row_paths</span><span>,</span><span> </span><span>read_append</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p>Now rows have to be combined together, not just stacked, with a small shift. This will depend on the size of pics. They seem to be the same size which is great. Note that the thing that gets displayed below is what <code>magick::image_info</code> gives you about any <code>magick</code> object, so I’ll use that.</p>
<div>
<div>
<pre><code><span>magick</span><span>::</span><span>image_read</span><span>(</span><span>ultimate_sample</span><span>[</span><span>1</span><span>:</span><span>10</span><span>])</span><span>
</span><span>info</span><span> </span><span>&lt;-</span><span> </span><span>magick</span><span>::</span><span>image_read</span><span>(</span><span>ultimate_sample</span><span>[</span><span>1</span><span>])</span><span> </span><span>%>%</span><span>
</span><span>magick</span><span>::</span><span>image_info</span><span>()</span><span>
</span><span>height</span><span> </span><span>&lt;-</span><span> </span><span>info</span><span>$</span><span>height</span><span>
</span><span>width</span><span> </span><span>&lt;-</span><span> </span><span>info</span><span>$</span><span>width</span><span>
</span></code></pre>
</div>
</div>
<p>The <code>col</code> parameter below is my laptop background <em>hex code</em>. I needed it before pasting the <em>hex stickers</em>.</p>
<div>
<div>
<pre><code><span>my_laptop</span><span> </span><span>&lt;-</span><span> </span><span>magick</span><span>::</span><span>image_blank</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>width</span><span> </span><span>*</span><span> </span><span>no_col</span><span>,</span><span>
</span><span>height</span><span> </span><span>=</span><span> </span><span>height</span><span> </span><span>*</span><span> </span><span>no_row</span><span> </span><span>*</span><span> </span><span>0.9</span><span>,</span><span>
</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;#FF6987&quot;</span><span>)</span><span>
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>no_row</span><span>){</span><span>
</span><span>if</span><span>(</span><span>i</span><span>/</span><span>2</span><span> </span><span>==</span><span> </span><span>floor</span><span>(</span><span>i</span><span>/</span><span>2</span><span>)){</span><span>
</span><span>offset1</span><span> </span><span>&lt;-</span><span> </span><span>0</span><span>
</span><span>}</span><span>else</span><span>{</span><span>
</span><span>offset1</span><span> </span><span>&lt;-</span><span> </span><span>(</span><span>width</span><span>/</span><span>2</span><span>)</span><span> </span><span>}</span><span>
</span><span>offset2</span><span> </span><span>&lt;-</span><span> </span><span>(</span><span>i</span><span>-1</span><span>)</span><span>*</span><span>(</span><span>height</span><span>*</span><span>0.75</span><span>)</span><span>
</span><span>my_laptop</span><span> </span><span>&lt;-</span><span> </span><span>magick</span><span>::</span><span>image_composite</span><span>(</span><span>my_laptop</span><span>,</span><span> </span><span>rows</span><span>[[</span><span>i</span><span>]],</span><span>
</span><span>offset</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;+&quot;</span><span>,</span><span> </span><span>offset1</span><span>,</span><span>
</span><span>&quot;+&quot;</span><span>,</span><span> </span><span>offset2</span><span>))</span><span>
</span><span>}</span><span>
</span><span>magick</span><span>::</span><span>image_write</span><span>(</span><span>my_laptop</span><span>,</span><span> </span><span>&quot;data/my_laptop.png&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="" src="https://i2.wp.com/raw.githubusercontent.com/maelle/maelle.github.io/master/_source/data/my_laptop.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>This looks fine, the only reason why it’s not perfect is probably the stickers not having exactly the same dimensions. Moreover, one of them doesn’t have transparent borders! That’s a mean design. <a rel="nofollow" href="https://github.com/jeroen" target="_blank">Jeroen Ooms</a> told me I could correct it by smartly using <code>image_fill</code> with color = “transparent” and some fuzz and <code>image_trim</code> before that to remove margins but I was lazy.</p>
<p>Now, what could one do? One could order hex stickers by color using the code in my <a rel="nofollow" href="http://www.masalmon.eu/2018/01/07/rainbowing/" target="_blank">blog post about rainbowing a set of pictures</a>. Or one could just combine all hex stickers together, then make them gray and use the resulting image as a place to collect stickers. #goals</p>
<div>
<div>
<pre><code><span>magick</span><span>::</span><span>image_convert</span><span>(</span><span>my_laptop</span><span>,</span><span> </span><span>colorspace</span><span> </span><span>=</span><span> </span><span>&quot;Gray&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>magick</span><span>::</span><span>image_write</span><span>(</span><span>&quot;data/my_laptop_goals.png&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="" src="https://i2.wp.com/raw.githubusercontent.com/maelle/maelle.github.io/master/_source/data/my_laptop_goals.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="http://www.masalmon.eu/2018/02/22/hexcombine/"> Maëlle</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/ya8XX2FjICM" height="1"></a></div>