Skip to content
Invisible link to canonical for Microformats Map of Africa with national flags clipped to each country boundary.

Beautiful maps with R (IV): Fun with flags revisited

Any picture as a basemap


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:

I also received an interesting reply to this from Hefin Ioan Rhys @HRJ21:

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)
  )

Map of Africa with national borders and neighboring countries shown for context.

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)
  )

Map of Africa with national flags clipped to each country boundary.

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/},
}

Mentions and replies

Conversations from across the web.

Shared your thoughts on this post? Drop your link here!

Publish a page that links to this post, then submit its URL. Webmention.io will check the link before the mention appears.


Related posts