22 North Is Not Up

I have nothing to add to this one.

Libraries

Code
library(sf)
library(tmap)
library(dplyr)
library(stringr)
library(ggplot2)

source("~/Documents/code/30-day-maps-2023/maps/utils.R")

Data wrangling

Code
focus <- "Antarctica"
lon0 <- 0
lat0 <- -90
proj <- str_glue("+proj=ortho lon_0={lon0} lat_0={lat0}")
Code
world <- st_read("data/ne_110m_admin_0_map_units.gpkg") %>%
  st_make_valid() %>%
  select(CONTINENT) %>%
  st_transform(proj) %>%
  st_make_valid() %>%
  filter(!st_is_empty(.)) %>%
  group_by(CONTINENT) %>%
  summarise() 

continent <- world %>%
  filter(CONTINENT == focus)

graticule <- get_graticule(spacing = 15)

The maps

tmap

tm_shape(world) +
  tm_fill() + 
  tm_shape(graticule) +
  tm_lines(col = "lightgrey", lwd = 0.5) +
  tm_compass(cardinal.directions = c("UP", "S", "E", "W")) +
  tm_layout(frame = FALSE)

ggplot2

We need ggspatial for a north arrow here.

But even then, to customize it I have to make my own function.

Code
up_arrow <- function(label = "UP") {
  text_label = label
  text_adj = c(0.5, 0.5)

  grid::gList(
    grid::polygonGrob(
      x = c(0.625, 0.5, 0.5),
      y = c(0.3, 0.85, 0.4),
      default.units = "npc",
      gp = grid::gpar(
        lwd = 1,
        col = "black",
        fill = "black"
      )
    ),
    grid::polygonGrob(
      x = c(0.375, 0.5, 0.5),
      y = c(0.3, 0.85, 0.4),
      default.units = "npc",
      gp = grid::gpar(
        lwd = 0.5,
        col = "black",
        fill = NULL
      )
    ),
    grid::textGrob(
      label = text_label,
      x = 0.5,
      y = 1,
      hjust = 0.5,
      vjust = 0.5,
      rot = 0,
      gp = grid::gpar(fontsize = 9)
    )
  )
}

On the other hand, since I’ve done the work, I might as well make the most of it.

library(ggspatial)

ggplot(world) +
  geom_sf(linewidth = 0) +
  geom_sf(data = graticule, col = "lightgrey", linewidth = 0.25) +
  annotation_north_arrow(location = "br", style = up_arrow) + 
  annotation_north_arrow(location = "tr", style = up_arrow("LEFT"), rotation = 90) + 
  annotation_north_arrow(location = "tl", style = up_arrow("DOWN"), rotation = 180) + 
  annotation_north_arrow(location = "bl", style = up_arrow("RIGHT"), rotation = 270) + 
  theme_void()