Site icon R-bloggers

30 Day Map Challenge 2022

[This article was first published on R on Nicola Rennie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The #30DayMapChallenge is a daily mapping, cartography, and data visualization challenge aimed at the spatial community. Every day in November participants create a map with a given theme (e.g. red, movement, two colours) and share their results on social media using the #30DayMapChallenge hashtag. Check out the challenge here. This year, I created all 30 maps! You can view all of my maps and the code used to generate them on GitHub.

I also completed the challenge last year, and you can read my review of last year’s challenge in a blog post. Last year, I decided to create (almost) all of my maps with a theme of Scotland. This year, I did something slightly different: I decided to set myself a 15 minute time limit for each of my maps. Why did I decide to do this? Well…

A few things that I found helpful with making maps within a 15 minute time limit:

R packages I used for the first time < svg class="anchor-symbol" aria-hidden="true" height="26" width="26" viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg"> < path d="M0 0h24v24H0z" fill="currentColor"> < path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76.0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71.0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71.0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76.0 5-2.24 5-5s-2.24-5-5-5z">

Most of the maps I created during the challenge were made using R and, due to the time limit I set myself, I often found myself reaching for packages I was already familiar with. However, I did manage to try out a few new packages (and some new functions from packages I’d used before)! Here are a few of the highlights:


Map of Scotland under different grid resolutions

Comparing R and Python < svg class="anchor-symbol" aria-hidden="true" height="26" width="26" viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg"> < path d="M0 0h24v24H0z" fill="currentColor"> < path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76.0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71.0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71.0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76.0 5-2.24 5-5s-2.24-5-5-5z">

For the out of my comfort zone theme on day 29, I decided to start recreating my map from day 2 in Python instead of R. I have some experience of programming in Python but I haven’t done much plotting or mapping in Python (at least not for a very long time)!


Map of South America created using R (left) and Python (right)

As you can see, the Python map isn’t quite so well styled as the R version – largely down to me being less familiar with Python and not getting quite as far in 15 minutes. There were a couple of similarities between the two languages which made converting the code to Python easier:

< details> < summary>Show R Code
library(tmap)
library(viridis)
sa <- rnaturalearth::ne_countries(scale = "medium", continent = "south america", returnclass = "sf") 
tm_shape(sa) +
  tm_polygons(col = "gdp_md_est",
              style = "cont",
              pal = viridis(3, direction = -1),
              title = "Estimated GDP") +
  tm_style("cobalt") +
  tm_layout(
    title = "South America",
    frame = FALSE,
    title.position = c("left", "center"),
    legend.position = c(0.02, 0.3)
  )
< details> < summary>Show Python Code
import geopandas as gpd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
sa = world[(world.continent=="South America")]
plt.figure()
fig, ax = plt.subplots(1, 1)
divider = make_axes_locatable(ax)
cax = divider.append_axes("bottom", size="5%", pad=0.5)
sa.plot(column='gdp_md_est',
        ax=ax,
        legend=True,
        cax=cax,
        cmap='viridis',
        legend_kwds={'label': "Estimated GDP",
                     'orientation': "horizontal",
                     'pad': 0.01,
                     'fmt': '%f'})
plt.title('South America')
ax.axis('off')
plt.ticklabel_format(useOffset=False, style='plain')
plt.show()

My favourite map < svg class="anchor-symbol" aria-hidden="true" height="26" width="26" viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg"> < path d="M0 0h24v24H0z" fill="currentColor"> < path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76.0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71.0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71.0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76.0 5-2.24 5-5s-2.24-5-5-5z">

The map that I enjoyed the most, was this minimalist version of the Glasgow subway for the “network” theme. My initial idea for this map was to recreate the Glasgow subway logo which has a very simplified version of the subway as the main image. I obtained the data from OpenStreetMap using the {osmdata} package, and the data turned out to be a little bit more complicated than I expected: it gave me some extra parts of the subway tracks I wasn’t expecting, the river polygons split in different places, and the stations data was different from the station entrances data. Lots of small issues that meant the data wrangling took up a little bit more time.

I ended up spending a bit more time on this, and finishing it off. You can also see the process of making the map, which I recorded using the {camcorder} package!

Resources < svg class="anchor-symbol" aria-hidden="true" height="26" width="26" viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg"> < path d="M0 0h24v24H0z" fill="currentColor"> < path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76.0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71.0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71.0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76.0 5-2.24 5-5s-2.24-5-5-5z">

The Geocomputation with R book is an excellent reference for getting started in working with spatial data in R – and it’s freely available online!

The R package I use most often for manipulating and visualising spatial data is {sf}. It’s compatible with the {tidyverse} suite of packages, which means I can use functions from {dplyr} for data wrangling, and {ggplot2} for mapping. The Spatial Data Science book provides a nice introduction.

Look out for another blog post coming soon, where I’ll discuss my favourite packages for spatial data in R!

Final thoughts < svg class="anchor-symbol" aria-hidden="true" height="26" width="26" viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg"> < path d="M0 0h24v24H0z" fill="currentColor"> < path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76.0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71.0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71.0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76.0 5-2.24 5-5s-2.24-5-5-5z">

I definitely learnt a few new ways to visualise spatial data in the last 30 days, including some new packages and functions. Thanks to Topi Tjukanov for creating this challenge a few years ago, and well done to everyone who participated in this year’s challenge whether you made one or thirty maps in November!

To leave a comment for the author, please follow the link and comment on their blog: R on Nicola Rennie.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.