Code
library(sf)
library(tmap)
library(dplyr)
library(ggplot2)
Let’s get this over with.
library(sf)
library(tmap)
library(dplyr)
library(ggplot2)
<- st_read("data/sh1.gpkg") %>%
sh1 st_transform(4326) %>%
select(geom) %>%
mutate(Mode = "Road")
<- st_read("data/interislander.gpkg") %>%
interislander st_transform(4326) %>%
select(geom) %>%
mutate(Mode = "Boat")
<- sh1 %>%
combined bind_rows(interislander) %>%
mutate(Mode = as.factor(Mode))
New Zealand’s mighty State Highway 1 (one of the world’s better road trips). I wouldn’t start from here.
tmap
In this example, I wanted to get a better handle on how the legend options work in tmap
v4. The tm_legend_combine
function is a nice feature, which combines the two symbolisations of colour and line style.
tm_shape(combined) +
tm_lines(
col = "Mode",
col.scale = tm_scale_categorical(
values = c("blue", "red")),
lty = "Mode",
lty.scale = tm_scale_categorical(
values = c("dashed", "solid")),
lty.legend = tm_legend_combine("col"),
lwd = 0.5) +
tm_layout(
frame = FALSE,
legend.frame = FALSE,
legend.outside = TRUE) +
tm_compass()
ggplot2
Here we need an additional package for the entirely useless north arrow.
ggspatial
can do it and seems preferable to the not-very-configurable ggsn
. The configurability of ggspatial
is a bit over the top for a simple map like this one. On the other hand its default north arrow is very on brand for the theme “Bad Map”!
library(ggspatial)
ggplot(combined) +
geom_sf(aes(colour = Mode, linetype = Mode),
linewidth = 0.5 * 25.4 / 72.27) +
scale_colour_manual(values = c("blue", "red")) +
scale_linetype_manual(values = c("dashed", "solid")) +
annotation_north_arrow(location = "br") +
theme_void()