R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

title: “Problem Set 55” output: html_document —

knitr::opts_chunk$set(echo = TRUE)

library(tidyverse)
library(sf)
library(osmdata)
library(ggspatial)
library(sfhotspot)

options(osmdata.use_cache = TRUE)
options(osmdata.base_url = "https://overpass-api.de/api/interpreter")

fixed_plot_aspect <- function() {
  coord_sf(datum = NA)
}
wards <- read_sf("nottingham_wards.gpkg") |>
  st_transform("EPSG:27700") |>
  dplyr::filter(ward_name %in% c("Castle",
                                "Lenton & Wollaton East",
                                "Meadows"))
burglaries <- readr::read_csv("nottingham_burglary.csv.gz") |>
  st_as_sf(coords = c("longitude", "latitude"), crs = "EPSG:4326") |>
  st_transform("EPSG:27700") |>
  st_intersection(wards)
## Rows: 1795 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): location, lsoa_code
## dbl  (2): longitude, latitude
## date (1): month
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
nottingham_buildings <- wards |>
  st_transform("EPSG:4326") |>
  st_bbox() |>
  opq() |>
  add_osm_feature(key = "building") |>
  osmdata_sf()
sf::sf_use_s2(FALSE)
## Spherical geometry (s2) switched off
nottingham_building_centroids <- dplyr::bind_rows(
  purrr::pluck(nottingham_buildings, "osm_polygons"),
  purrr::pluck(nottingham_buildings, "osm_multipolygons")
) |>
  st_make_valid() |>
  st_centroid() |>
  st_transform("EPSG:27700") |>
  st_intersection(wards)
## Warning: st_centroid assumes attributes are constant over geometries
## Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
## of_largest_polygon): st_centroid does not give correct centroids for
## longitude/latitude data
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
burglary_risk <- hotspot_dual_kde(
  burglaries,
  nottingham_building_centroids,
  bandwidth_adjust = 0.25,
  grid = hotspot_grid(wards, cell_size = 100),
  quiet = TRUE
) |>
  st_intersection(wards)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
burglary_risk_filtered <- burglary_risk |>
  dplyr::filter(is.finite(kde))
ggplot() +
  annotation_map_tile(type = "cartolight", zoomin = 0, progress = "none") +
  geom_sf(aes(fill = kde), data = burglary_risk_filtered, alpha = 0.8, colour = NA) +
  geom_sf(data = wards, fill = NA) +
  scale_fill_distiller(direction = 1) +
  labs(
    title = "Burglary Risk in South-West Nottingham",
    fill = "Risk Density"
  ) +
  theme_void() +
  theme(legend.position = "bottom")

robbery <- readr::read_csv("nottingham_robbery.csv.gz") |>
  st_as_sf(coords = c("longitude", "latitude"), crs = "EPSG:4326") |>
  st_transform("EPSG:27700")
## Rows: 555 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): location, lsoa_code
## dbl  (2): longitude, latitude
## date (1): month
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
robbery_gistar <- robbery |>
  hotspot_gistar(cell_size = 100,
                 bandwidth_adjust = 0.25,
                 quiet = TRUE)

robbery_hot_cells <- robbery_gistar |>
  dplyr::filter(gistar > 0, pvalue < 0.05)
ggplot() +
  annotation_map_tile(type = "cartolight", zoomin = 0, progress = "none") +
  geom_sf(aes(fill = kde), data = robbery_hot_cells, alpha = 0.8, colour = NA) +
  geom_sf(data = wards, colour = "grey70", fill = NA) +
  scale_fill_gradient(low = "lightblue", high = "darkblue") +
  fixed_plot_aspect() +
  labs(
    title = "Robbery Hotspots (Gi*)",
    fill = "Density"
  ) +
  theme_void() +
  theme(legend.position = "right")