I do love create figures with R, I found quite satisfying to make them very
personalized, yet reproducible! However I sometime wish that combining two
plots was easier (as it often requires to build larges matrices and crazy
layout()
calls). I was looking for a simple way to do basic combinations of
customized R figures, and I stumbled on this discussion on
showing how easy it is to do so with
ImageMagick. Let me exemplify with
a reproducible example based on two silhouettes by Timothey
Bartley available on
PhyloPic (note that I use 2 images here, but it works
the same way with more):
- lake trout (Salvelinus namaycush);
- yellow perch (Perca flavescens).
Let’s first create a folder assets
and download the silhouettes with wget:
|
|
Here there are:
perca_flavescens.png

salvelinus_namaycush.png

To combine them, you just need to use convert
with the option +append
to
combine them horizontally or -append
to combine them vertically. Let’s start
with +append
:
|
|

💥 Neat, right? There is something about the default behavior you need to know, though. Indeed, by default, the images are top-aligned and so, if one image is shorter, a patch is added to make the image of the same size and this patch has a white background by default. You may not have noticed this here, but it’d be obvious with a black background:
|
|

By showing you this problem, I’ve almost solved it! So if you want to get rid of the white background, add a transparent one!
|
|

And now let’s combine the image vertically with a transparent background for the patch:
|
|

Love this trick! Two short remarks to end this note:
- there are many programming languages that interface ImageMagick;
- the R 📦 patchwork is making this kind of operation really easy with ggplot2.