I'm trying to have a rectangular "zoom in" into my chart. So far, I can create the chart itself and a smaller version, but I haven't figured out how to zoom in rectangularly.
library(sf)
library(dplyr)
library(tmap)
Get shape files for Germany [55 MB]. In Germany zip codes are called Postleitzahlen (PLZ).
germany <- read_sf("data/OSM_PLZ.shp")
Create some arbitrary groups:
germany <- germany %>%
mutate(plz_groups = case_when(
substr(plz, 1, 1) == "1" ~ "Group A",
substr(plz, 2, 2) == "2" ~ "Group B",
substr(plz, 3, 3) == "2" ~ "Group C",
TRUE ~ "Group X" # rest
))
Make plot filling by PLZ:
map_de <- tm_shape(germany) +
tm_fill(col = "plz_groups")
map_de
germany_zoomin <- germany %>%
filter(substr(plz, 1, 1) == "4")
map_zoomin <- tm_shape(germany_zoomin) +
tm_fill(col = "plz_groups")
So I can create a zoomed in chart, but this is NOT what I want:
map_zoomin
print(map_de, vp = grid::viewport(0.8, 0.185, width = 0.2, height = 0.45))
# tmap_save("test.png")
Instead, I would like to specify the location, e.g. the PLZ of Cologne (50667) and draw a rectangular box around it.