Issues: Beautiful maps with R
On 27 Jan. 2022 my package rasterpic was accepted on CRAN (Hooray!). This package geotags images, using a spatial object (from sf or terra) as a geographic reference.
I tweeted about that, and it seems to have received good feedback from the #rspatial community:
#rspatial Do we need a package for using our own pictures as base maps? I don’t know, but anyway we have it! {**rasterpic**} 📦 is now on CRAN, and we can convert our pngs to spatial rasters like this https://t.co/fpoollARoN pic.twitter.com/l9o7rQwdgX
— dieghernan ن (@dhernangomez) January 27, 2022
I also received an interesting reply to this from Hefin Ioan Rhys @HRJ21:
Ooh I think a map with countries filled with their own flag would be poppin'.
— Hefin Ioan Rhys (@HRJ21) January 28, 2022
That reminds me of a previous post that I wrote when I added some new functions to the cartography package, now replaced by the mapsf package.
With rasterpic, we now have an alternative tool for creating maps using images, and this quick post shows you how to do it.
I will recreate the map of Africa from the earlier post using
giscoR and ggspatial.
Use ggspatial 1.1.6 or later
to plot SpatRaster objects in ggplot2.
The flags come from country-flags.
We download the 1000-pixel-wide PNGs from version 1.2.10 of
svg-country-flags via jsDelivr.
# Requires ggspatial 1.1.6 or later.
library(ggspatial)
library(ggplot2)
library(giscoR)
library(dplyr)
library(rasterpic)
# For country names
library(countrycode)
background <- "#FEFDFD"
map_title <- "inter-tight"
map_regular <- "inter"
sysfonts::font_add_google("Inter Tight", family = map_title)
sysfonts::font_add_google("Inter", family = map_regular)
showtext::showtext_opts(dpi = knitr::opts_chunk$get("dpi"))
showtext::showtext_auto()
world <- gisco_get_countries(epsg = 3857)
africa <- gisco_get_countries(region = "Africa", epsg = 3857)
# Base map of Africa
plot <- ggplot(world) +
geom_sf(fill = "grey90") +
theme_minimal(base_family = map_regular, paper = background) +
theme(
plot.title = element_text(family = map_title, face = "bold"),
legend.title = element_text(family = map_title, face = "bold"),
strip.text = element_text(family = map_title, face = "bold"),
plot.margin = margin(10, 10, 10, 10),
panel.background = element_rect(fill = "lightblue")
)
plot +
# Zoom on Africa
coord_sf(
xlim = c(-2000000, 6000000),
ylim = c(-4000000, 5000000)
)

Now, let’s add the flags with a loop:
# We paste the ISO2 code to each African country
africa$iso2 <- countrycode(africa$ISO3_CODE, "iso3c", "iso2c")
# Get flags from repo
flagrepo <- "https://cdn.jsdelivr.net/npm/svg-country-flags@1.2.10/png1000px/"
# Loop and add
for (iso in africa$iso2) {
# Download pic and plot
imgurl <- paste0(flagrepo, tolower(iso), ".png")
tmpfile <- tempfile(fileext = ".png")
download.file(imgurl, tmpfile, quiet = TRUE, mode = "wb")
# Raster
x <- africa %>% filter(iso2 == iso)
x_rast <- rasterpic_img(x, tmpfile, crop = TRUE, mask = TRUE)
plot <- plot + layer_spatial(x_rast)
}
plot +
geom_sf(data = africa, fill = NA) +
# Zoom on Africa
coord_sf(
xlim = c(-2000000, 6000000),
ylim = c(-4000000, 5000000)
)

How to cite this post
For attribution, please cite this work as:
Hernangómez, D. (2022, January 28). Beautiful maps with R (IV): Fun with flags revisited. One World. https://dieghernan.github.io/202201_maps-flags/
BibTeX
@misc{post20220128-maps-flags,
author = {Hernangómez, Diego},
title = {Beautiful maps with R (IV): Fun with flags revisited},
year = {2022},
month = jan,
howpublished = {One World},
url = {https://dieghernan.github.io/202201_maps-flags/},
}