Visualizing Breathalyzer test Counts in Hungary with Inset Map

Shota Hasui

2023-01-28

For this project, I created a chloropleth map that visualizes the number of breathalyser tests per county in Hungary. I also added an inset map of where Hungary is in Europe as well as a scale line and an orienteering north arrow.

All credit for the instructions and data go to Crime Mapping and Spatial Data Analysis using R by Juanjo Medina and Reka Solymosi.

Code can be found below.

library(readr)
library(dplyr)
library(sf)
library(ggspatial)
library(rnaturalearth)
library(ggplot2)
library(RColorBrewer)
library(ggrepel)
library(cowplot)
hungary <- st_read("https://raw.githubusercontent.com/maczokni/crime_mapping/master/data/hungary.geojson")
drink_driving <- read_csv("https://raw.githubusercontent.com/maczokni/crime_mapping/master/data/drink_driving.csv")
hu_dd <- left_join(hungary, drink_driving, by = c("name" = "name"))
hu_dd <- hu_dd %>% 
  mutate(total_quantiles = cut(total_breath_tests, 
                               breaks = round(quantile(total_breath_tests),0),
                               include.lowest = TRUE, dig.lab=10))
new_levels <- c("< 4796", "4796 to < 10785", "10785  to < 15070", "> 15070")
map <- ggplot(data = hu_dd) + 
  geom_sf(aes(fill = total_quantiles), 
          lwd = 0.5, col = "black") + 
  scale_fill_brewer(type = "seq", palette = "Blues", name = "Total tests (quantiles)", labels = new_levels) + 
  theme_void() 
map <- map + 
  ggtitle(label = "Number of breathalyser tests per county in Hungary", 
          subtitle = "January 2020") + 
  annotation_north_arrow(height = unit(20, "mm"), 
                         width = unit(10, "mm"),
                         style = north_arrow_fancy_orienteering()) +
  annotation_scale(line_width = 0.5,     
                   height = unit(1, "mm"),
                   pad_x = unit(6, "cm"))  

europe_countries <- st_as_sf(countries110) %>%  
  filter(region_un=="Europe",
           name != "Russia" & name != "Iceland") %>%  
  pull(name) 

europe <- ne_countries(geounit = europe_countries, 
                       type = 'map_units', 
                       returnclass = "sf") 

inset_map <- ggplot() +  
  geom_sf(data = europe, 
          fill = "white") + 
  geom_sf(data = europe %>% filter(name == "Hungary"), 
          fill = "white" ,  
          col = "red",  
          lwd = 2) +   
  theme_void() +  #
  theme(panel.border = element_rect(colour = "black", 
                                    fill=NA)) 

hu_dd_with_inset <- ggdraw() +  
  draw_plot(map) + 
  draw_plot(inset_map,   
            x = 0.62,    
            y = 0.05,    
            width = 0.3,    
            height = 0.3)   

hu_dd_with_inset