City maps of the 150 largest urban areas in NZ

code
maps
population
Author

Isaac Bain

Published

August 27, 2024

1 Introduction

City maps in a particular minimalist style have become quite trendy recently. You’ve probably seen them all over the internet, with sellers on platforms like Etsy charging anywhere from $5 to $20 for a digital download, and even more for printed versions.

But here’s a little secret… they’re super easy to create in R.

There’s an R package called rcityviews that allows you to make these maps with just one line of code:

rcityviews::cityview("Christchurch")

Pretty cool, right? But it doesn’t stop there. The rcityviews package offers a range of themes and border options. And if coding isn’t your thing, the package author has even provided a ShinyApp that lets you create custom maps for cities all over the world without writing a single line of R code.

2 New Zealand urban areas

To show you just how easy these are to create, I wrote a small for loop that iterates over the names and locations of New Zealand’s 150 largest urban areas to generate maps for each one. The process is highly efficient and requires minimal effort once the loop is set up. With just a few lines of code, you can create a comprehensive collection of maps that visually capture the essence of these diverse urban landscapes. This method not only saves time but also allows for consistent styling across all maps, ensuring that each map adheres to the same aesthetic standards.

Show the code
library(rcityviews)
library(sf)
library(dplyr)

# read cities
cities <- st_read("posts/city-maps/cities.shp") |>
  mutate(
    Longitude = st_coordinates(geometry)[, 1],
    Latitude = st_coordinates(geometry)[, 2]
  ) |> 
  st_drop_geometry() |>
  dplyr::arrange(desc(Population))

myTheme <- list(
  colors = list(
    background = "#ffffff",
    water = "#9ddffb",
    landuse = c("#f2f4cb", "#d0f1bf", "#64b96a"),
    contours = "#eeefc9",
    streets = "#2f3737",
    rails = c("#2f3737", "#eeefc9"),
    buildings = c("#8e76a4", "#a193b1", "#db9b33", "#e8c51e", "#ed6c2e"),
    text = "#2f3737",
    waterlines = "#9ddffb"
  ),
  font = list(
    "family" = "Imbue",
    "face" = "plain",
    "scale" = 3,
    append = "\u2014"
  ),
  size = list(
    borders = list(
      contours = 0.15,
      water = 0.4,
      canal = 0.5,
      river = 0.6
    ),
    streets = list(
      path = 0.2,
      residential = 0.3,
      structure = 0.35,
      tertiary = 0.4,
      secondary = 0.5,
      primary = 0.6,
      motorway = 0.8,
      rails = 0.65,
      runway = 3
    )
  )
)

# do one individually

# city <- new_city(name = "Hokitika", country = "New Zealand", lat = -42.717507, long = 170.973959)
# 
# p <- cityview(name = city, license = FALSE, border = "circle", theme = myTheme)
# 
# ggplot2::ggsave(filename = "posts/city-maps/output/Hokitika.jpg",
#                 plot = p,
#                 height = 500,
#                 width = 500,
#                 units = "mm",
#                 dpi = 300)


# Loop through each city in the data frame
for (i in 1:nrow(cities)) {
  
  # Extract city details
  city_name <- cities$Urban.Area[i]
  city_lat <- cities$Latitude[i]
  city_long <- cities$Longitude[i]
  
  # Create a new city object
  city <- new_city(name = city_name, country = "New Zealand", lat = city_lat, long = city_long)
  
  # Generate a filename for the output
  output_filename <- paste0("posts/city-maps/output/", city_name, ".jpg")
  
  # Produce the cityview
  p <- cityview(name = city, license = FALSE, border = "circle", 
                theme = myTheme) # replace myTheme with "vintage" or another
  
  ggplot2::ggsave(filename = output_filename,
                  plot = p,
                  height = 500,
                  width = 500,
                  units = "mm",
                  dpi = 300)
}

Here are a few of my favourites: (more below)

Bluff
Kaikōura
Timaru
Christchurch
Wellington
Hamilton
Rotorua
Pukekohe
Havelock North
Lincoln
Huntly
Picton
Balclutha
Lyttelton
Edgecumbe
Blenheim
Dunedin
Auckland

Footnotes

  1. Feel free to print these, share them, or use them as you please. Just maybe don’t start selling them without asking me first.↩︎