Laying out images and fonts in ggplot with colour extraction too.
@ChrisWoodsSays, 2.3.2021
To create a complete data visualisation using ggplot, i.e. without the need to add any finishing touches with a package such as Illustrator. To include specific fonts, cropped images and a repeating pattern where each item has similar constituent parts.
To make it a bit more interesting, I wanted to use R to extract colours from the included images.
I used the following R packages:
Package | For |
---|---|
tidyverse | All kinds of stuff including ggplot used for making the final plot |
colorfindr | Extracting the colour palettes |
magick | Cropping and resizing the bird images |
ggforce | Plotting palette circles in specific places |
ggtext | Adding rich text (with fonts) to ggplot |
ggtextures | Places images without distortion based on the device aspect ratio or scales |
here | Great for easily getting hold of files in your project structure without hardcoding underlying folders |
tools | Removing the extension from a file name so that I can add .png |
janitor | Cleaning up column names in bird file |
kable | Displaying tables like this one |
I chose eight birds that I either see in my garden or in the case of my favorite, the Kingfisher, nearby. I used images from Wikipedia. The birds and accreditations are listed below.
Name | Scientific Name | Conservation Status | What they eat | Image Credit |
---|---|---|---|---|
Kingfisher | Alcedo atthis | Amber | Fish and aquatic insects | Frank-2.0, CC0, via Wikimedia Commons |
Robin | Erithacus rubecula | Green | Worms, seeds, fruits, insects and other invertebrates | © Francis C. Franklin / CC-BY-SA-3.0, CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0, via Wikimedia Commons |
Goldfinch | Carduelis carduelis | Green | Seeds and insects in summer. | © Francis C. Franklin / CC-BY-SA-3.0, CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0, via Wikimedia Commons |
Nuthatch | Sitta europaea | Green | Insects, hazel nuts, acorns, beechmast and other nuts and seed | Paweł Kuźniar, CC BY-SA 3.0 http://creativecommons.org/licenses/by-sa/3.0/, via Wikimedia Commons |
Blue tit | Cyanistes caeruleus | Green | Insects, caterpillars, seeds and nuts. | © Francis C. Franklin / CC-BY-SA-3.0, CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0, via Wikimedia Commons |
Blackbird | Turdus merula | Green | Blackbird food consists of a variety of insects and worms, but they also eat berries and fruit when in season | Andreas Trepte, CC BY-SA 2.5 https://creativecommons.org/licenses/by-sa/2.5, via Wikimedia Commons |
Bullfinch | Pyrrhula pyrrhula | Amber | Seeds, buds and insects (for young). | © Francis C. Franklin / CC-BY-SA-3.0, CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0, via Wikimedia Commons |
Greater-spotted Woodpecker |
Dendrocopos major | Green | Insects, seeds and nuts | Hangsna, CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0, via Wikimedia Commons |
The colorfindr package includes a get_colors function that does just that.
The bird images include backgrounds, branches, leaves, tree bark etc and I didn’t want to include those colours in my palettes. So I created rough crops of each image and placed them in the data/images/handcropped folder (not the output folder as these are not programatically created).
I selected the top 6 colours as in the example below.
palette <- colorfindr::get_colors(here::here(dataDir, "images", "handCropped", "Eisvogel_kingfisher.png"),
exclude_col = "#FFFFFF", exclude_rad = 10
) %>%
make_palette(n = 6)
Rectangular images don’t look good in this context, so I wanted to apply a circular crop. The images were also of varying sizes and in some cases, such as our Kingfisher, the hero was not in the middle.
I added specific adjustments for zoom, x and y offsets to the bird table which I could then apply.
## Warning: Quick-TRANSfer stage steps exceeded maximum (= 11857600)