#30DayMapChallenge & #RStats

Day 30: Metamapping day

r-spatial
dataviz
rstats
Author
Affiliation
Published

November 30, 2021

The challenge

Since 2019, the #30DayMapChallenge by Topi Tjukanov has brought map geeks together for a month to flood our Twitter feeds with mind-blowing maps.

This year, I decided to participate by making maps with R! I had a lot of fun, but it was also quite a journey. I chose as an overarching topic my home country: Ecuador. I posted each of the maps on Twitter, and these two are the ones that received most attention:

My personal favorite, however, is day 13. I live away from home and so many other Ecuadorians do. It was amazing to see where we all are, and how “far-away” time wise each of us is from calling our loved ones.

You can see my complete gallery on this GitHub repository. This post is meant to show the back-end of these maps, so here we go!

Code-driven maps

I get a certain satisfaction of generating maps with code. To know that a bunch of commands can result in pretty figures is not only rewarding but also helpful. The certainty that I can always go back to a piece of code to re-generate a certain plot is the peak of reproducible data visualization.

Don’t get me wrong, GUIs are great and have many advantages, like not taking 30 minutes to figure out the best coordinates for your labels. But it is that nice feeling of seeing a map somewhere and thinking: How did this person do that? Which tools/packages did they use? Did they go through crazy workarounds to get that effect?; and then actually being able to scan the building blocks of that map and answer those questions and test those effects for yourself, that makes the difference for me.

Of course, this can only happen when code is shared. I personally love R’s capabilities for creating nice maps/figures and I myself used only R for this challenge (except for Day 15: map without a computer).

Many other mappers used R for their challenge as well. Here are some of the GitHub repositories and Gists I found, that used R for their challenge:

richardvogg | gkaramanis | schochastics | shandiya | dakvid | leeolney3 | tashapiro | bydata

I don’t mean this in any way as a comprehensive list, but just a sample of all the creative code out there during this edition of the challenge. For some further examples check David Frigge’s awesome gallery!

Metamapping my challenge

The final day of the challenge is metamapping day. According to the details one can:

Spend the day either by 1) collecting your entries from the challenge to a common gallery, 2) writing a tutorial or a blog post on one of your maps or 3) create a map from a theme you have chosen yourself

Well, I decided to derail a bit and write a meta blog post for all the maps I created with R. In my tweets I always tried to include the packages I used for the day, however, I used so many for this challenge, that I thought they deserved some extra love. Some packages I already knew by heart (all their ins-and-outs and awesome capabilities), but others I discovered just because of this challenge and that was pretty cool. So, I chose for Day 30 to do a simple analysis of the packages I used.

My first task was to collect all the packages from my 28 R scripts. I was bracing myself either to writing my own function that would find all my library() calls, or to do some manual check of each. Luckily, renv came to my rescue. Did you know you can scan all the packages your project depends on? This was exactly what I needed, so with one line:

renv::dependencies()

I managed to get all of the packages I used, per script. Next tasks: summarize by number of times I called each package, and assign them a category. You can see the code I used here.

The final result? The packages I used for each of my self-assigned categories and how many times I used them:

Insights:

  • Total number of packages used: `r emo::ji("five")`9️⃣
  • There was not one package that was used every day of the challenge 🤯
  • Natural Earth was very frequently a data source, given that it is my go-to package to get country polygons 😁
  • Want to work with fonts on Windows? extrafont is your package! 😉
  • I limited my interactive mapping quite strictly to the set day 😅

Most used R packages

sf

Take a look again at the plot above, which package stands out the most? Beautiful sf! Simple features came here to stay and I am very glad it exists. Working with spatial data in R has never been easier, and that is because of this awesome package. I used it to read spatial data into R, to work with projections and to perform spatial analyses like joins, generating random spatial points, etc.

dplyr

The tidyverse has made working with data in R extremely intuitive. Being able to work with structured data frames brings data science to a new level. dplyr is the first tidyverse package I learnt and I use it constantly for tasks as filtering, selecting, mutating, etc.

ggplot2

Another package of the tidyverse is ggplot2, which introduces the grammar of graphics into R and is extremely powerful to create awesome visualizations. Two main functions allow you to combine this package with spatial data: geom_sf() and geom_stars(). One interesting tip, if you learn to use coord_sf(), you can do things like assigning a CRS to your plot, although your data has a different projection, as coord_sf(crs ="+proj=geos")

This are the ggplot extensions I use the most:

ggfx

To give shading and glow effects to spatial features

ggtext

To add special formatting to text

gganimate

To animate the plots

ggspatial

To add north arrows and spatial scales

ggnewscale

To map multiple variables to the fill/color aesthetics

ggimage

To add images in spatial locations or as annotations

ggrepel

To automatically place overlapping labels

osmdata

This package is simply the easiest way I have come across in R to query OpenStreetMap data. I use it very often when I need to get data for small areas. For larger areas, for example a whole country, I use osmextract.

tmap

Want to plot spatial features fast and easy, static and interactive? Use tmap! I use this package a lot for my scientific publications because it allows you to use raster RGB images and vector data really easy. To get a bit more creative it has some limitations, and that is why I did not use it as often as ggplot2 on this challenge. But, one of the main features I like about this one is the ability to call tm_facets() and get individual plots of features grouped by a category, and let scales x and y be free. This is not possible with ggplot2. See that in action on Day 22: Boundaries.

Note: This hex logo is not official yet but I love it! Waiting eagerly for version 4 of tmap to launch this nice logo and many new features!

scico

Fabio Crameri provided the whole scientific community a great set of scientific color palettes to fairly represent data that is universaly readable! scico allows you to use the palettes in R. Another great source of palettes is viridis, which is already easily accessible in ggplot2 with scale_*_viridis_*().

stars

stars really needs a hex logo 🙏

Spatiotemporal Arrays, Raster and Vector Data Cubes are handled neatly with stars. For anything raster or netcdf related this is my go-to package. It might be a bit complex to start with this package, but it is definitely worth it.

Other options to work with raster data are raster and terra form the rspatial universe, which I actually use when I need to create hillshades from elevation data.

Honorable mentions

Although not on the top 7 above, there are some packages that I use frequently, but were not so used for this challenge. There are also really cool packages that I only discovered in the past month. Here is a table of how I used them:

sfnetworks

OK, I am biased here, but have you checked out sfnetworks yet? Well, if you work with spatial networks you should. I used it to compute travel time using public transport in Quito for Day 9: Monochrome

rgee

Do you work with Google Earth Engine? Do you want to do that via R? Then use rgee. I use it to extract RGB thumbnails of Sentinel-2 imagery. See the RGB collection for Day 6, Day 7 and Day 8

rayshader

For Day 11: 3D I could not use any other package than rayshader. 3D rendering can get so creative with this package

haven

Open data can come in so many formats and with so many extensions. When I first saw a .dta or a .sav file I thought, OK now what? Luckily, haven was just there ready made for these cases!

ggpomological

For Day 24: Historical I wanted to make a map with an old-style look. And there it was: ggpomological! I was very happy to run into this package

gggibbous

I had seen moon plots in other dataviz projects before but had never tried them myself. For Day 19: Island(s) I finally got the chance!

biscale

I remember I made some bivariate choropleth maps once and it required a lot of code to achieve. biscale made it easy! I really wanted to flip the legend, and I managed so if you are interested, code is there. Featured on Day 26: Choropleth

leaftime & leaflet

I normally handle my interactive maps with tmap or mapview, but this time I stubbornly wanted a time slider for my Day 25: Interactive map, hence I ended up learning a lot on hot to use leaflet and its addons

Wrap-up

With this challenge I wanted to show how flexible R is to generate beautiful reproducible maps. Maybe mine are not the best examples but there are so many awesome contributions out there! I hope this post helps anyone who wants to use R for spatial data visualization to point them to good resources.

This was my first #30DayMapChallenge, it was exciting to think of new topics every day but frustrating when Ecuador’s open data repositories either had their servers down or chose to share the data only as WMS or with no metadata. I know this does not happen only for Ecuador, as Sanna Jokela explained on her tweet for one of her contributions.

I cannot think of a better way to end this challenge. Thanks to anyone who followed my map making for 30 days, and sorry to those who follow me and were annoyed of having so many maps on your feed!

Reuse

Citation

BibTeX citation:
@online{abad2021,
  author = {Abad, Lorena},
  title = {\#30DayMapChallenge \& {\#RStats}},
  date = {2021-11-30},
  langid = {en}
}
For attribution, please cite this work as:
Abad, Lorena. 2021. “#30DayMapChallenge & #RStats.” November 30, 2021.