knitr::opts_chunk$set(
  collapse = TRUE,
  out.width = "100%",
  dpi = 300,
  fig.width = 7.2916667,
  comment = "#>"
)
hook_output <- knitr::knit_hooks$get("output")
knitr::knit_hooks$set(output = function(x, options) {
   lines <- options$output.lines
   if (is.null(lines)) {
     return(hook_output(x, options))  # pass to default hook
   }
   x <- unlist(strsplit(x, "\n"))
   more <- "..."
   if (length(lines)==1) {        # first n lines
     if (length(x) > lines) {
       # truncate the output, but add ....
       x <- c(head(x, lines), more)
     }
   } else {
     x <- c(more, x[lines], more)
   }
   # paste these lines together
   x <- paste(c(x, ""), collapse = "\n")
   hook_output(x, options)
 })
library(tmap)
library(geofacet)
library(dplyr)
library(sf)
tmap_options(scale = 0.75)

Get grid layout of Dutch provinces

nl_prov_grid1 = geofacet::nl_prov_grid1
class(nl_prov_grid1) = "data.frame"

nl_prov_grid1 = nl_prov_grid1 |>
    mutate(name = ifelse(name == "Friesland", "Fryslan", name)) |>
    select(-code)

Join with NLD datasets in tmap

NLD_prov2 = NLD_prov |>
    left_join(nl_prov_grid1,
              by = "name")

NLD_muni2 = NLD_muni |>
    left_join(NLD_prov2 |>
                st_drop_geometry() |>
                select(name, row, col), by = c("province" = "name"))

NLD_dist2 = NLD_dist |>
    left_join(NLD_prov2 |>
                st_drop_geometry() |>
                select(name, row, col), by = c("province" = "name"))

The map

tm_shape(NLD_dist2) +
    tm_fill("dwelling_value", 
            fill.scale = tm_scale_intervals(breaks = c(50, 250, 350, 500, 750, 1600), as.count = FALSE),
            fill.legend = tm_legend("", position = tm_pos_on_top("left", "top"), frame = FALSE)) +
    tm_facets_grid(rows = "row", columns = "col") +
tm_shape(NLD_muni2) +
    tm_borders(lwd = 1) +
    tm_facets_grid(rows = "row", columns = "col") +
tm_shape(NLD_prov2) +
    tm_borders(lwd = 2) +
    tm_facets_grid(rows = "row", columns = "col") +
tm_layout(panel.show = FALSE) +
    tm_title("Average dwelling value (in thousand Euros)")


r-tmap/tmap documentation built on Feb. 28, 2025, 7:54 a.m.