Appendix F — Chapter code library
These completed files bring together the code developed in the book. Use the descriptions to find a starting point for your own analysis, and follow the section links in parentheses to learn how each part works. The files use the example datasets from the chapters; adapt the data sources and analytical choices to suit your own project.
Save an R script in the R folder of your crime mapping workspace and open it in Positron. Save the Quarto report as chapter_11.qmd in your workspace, following Chapter 11. Before running a file, follow its chapter’s instructions for installing packages and creating the required folders, including data/raw, data/processed and output where needed (Section 1.5). The corrected chapter_08.R also needs the Bronx shootings and precinct files downloaded in Section 7.5.
You can find finished example reports and reusable templates in Appendix G. The Medellín report below is the same source file linked from that appendix, downloaded here with the filename used in Chapter 11.
| Chapter | What the code does | Download |
|---|---|---|
| Chapter 2 | Downloads and loads downtown Atlanta homicide data (Section 2.3.2), converts coordinates to spatial data and a projected coordinate reference system (Section 2.4), and draws a point map (Section 2.6). | Download chapter_02a.R |
| Chapter 2 | Downloads and loads Glenrose Heights homicide data (Section 2.3.2), converts coordinates to spatial data and a projected coordinate reference system (Section 2.4), and draws a point map (Section 2.6). | Download chapter_02b.R |
| Chapter 3 | Downloads San Francisco robbery data and reads the local CSV file into R (Section 3.2.1). | Download chapter_03a.R |
| Chapter 3 | Downloads an Excel workbook and reads the Austin aggravated-assault worksheet into R (Section 3.2.3). | Download chapter_03b.R |
| Chapter 4 | Downloads and loads San Francisco robbery data (Section 3.2.1), filters dates to the first quarter of 2019 (Section 3.4), derives weekdays and counts robberies (Section 4.2, Section 4.3.1), and saves the counts as a CSV file (Section 4.5). | Download chapter_04a.R |
| Chapter 4 | Downloads and loads Austin aggravated-assault data (Section 3.2.3), removes rows with missing dates or location categories (Section 3.4), derives weekdays and counts assaults by location category and weekday (Section 4.2, Section 4.3.1), and saves the counts as a CSV file (Section 4.5). | Download chapter_04b.R |
| Chapter 5 | Downloads Vancouver crime data, cleans column names and converts coordinates to spatial data (Section 5.4), selects bicycle thefts (Section 5.4.5), and maps them with customised point symbols (Section 5.6.1). | Download chapter_05.R |
| Chapter 6 | Estimates bicycle-theft density in Vancouver (Section 6.2), clips the density layer to neighbourhood boundaries (Section 6.3), and adds neighbourhood boundaries and names to the map (Section 6.6). | Download chapter_06.R |
| Chapter 7 | Creates a density map of Bronx shootings (Section 7.5), sets density colours and a basemap (Section 7.6, Section 7.7), adds titles, captions, a scale bar and a north arrow (Section 7.8, Section 7.9, Section 7.9.2), and saves the map as a PDF (Section 7.10). | Download chapter_07.R |
| Chapter 8 | Loads and filters the local Bronx shootings and precinct-boundary files, then draws a map of fatal shootings with precinct boundaries and titles using the corrected complete script (Section 8.5.4). | Download chapter_08.R |
| Chapter 9 | Downloads Mexico City carjacking and boundary data (Section 9.2.1), counts offences in each borough and creates a choropleth map (Section 9.2.2). | Download chapter_09a.R |
| Chapter 9 | Joins murder counts to district boundaries in Uttar Pradesh (Section 9.4.1), creates an interactive map of counts (Section 9.4.2), calculates population-adjusted murder rates (Section 9.5.3), and maps those rates interactively (Section 9.5.4). | Download chapter_09b.R |
| Chapter 10 | Standardises fictional Chicago addresses, geocodes each distinct address, joins the coordinates back to the original incidents, and maps the results (Section 10.4). | Download chapter_10.R |
| Chapter 11 | Combines explanatory text and inline R results (Section 11.3.1) with a Medellín homicide-density map, a figure caption and a cross-reference (Section 11.3.2), and renders a self-contained HTML report (Section 11.4.1). | Download chapter_11.qmd |
| Chapter 12 | Downloads and reads Medellín metro-line shapefiles (Section 12.3), retrieves bus stops from OpenStreetMap (Section 12.4), and combines them with a clipped homicide-density layer and neighbourhood boundary in a map (Section 12.4). | Download chapter_12.R |
| Chapter 13 | Retrieves and prepares Nottingham building and burglary data (Section 13.2.1), then estimates and maps burglary risk using dual kernel density (Section 13.2.2). | Download chapter_13a.R |
| Chapter 13 | Calculates the Gi* statistic for Nottingham robberies, selects statistically significant hotspots, clips them to ward boundaries and maps them (Section 13.3). | Download chapter_13b.R |
| Chapter 13 | Identifies clusters of Nottingham robberies using DBSCAN and maps their boundaries and shares of offences (Section 13.4). | Download chapter_13c.R |
| Chapter 13 | Calculates significant Gi* hotspots and DBSCAN clusters for Nottingham robberies (Section 13.3, Section 13.4), then combines them with ward boundaries in one map (Section 13.5). | Download chapter_13d.R |
| Chapter 14 | Reshapes Malaysian violence counts into a wide table (Section 14.2.1), formats the table with gt (Section 14.2.2), and adds readable column names and summary rows (Section 14.2.3, Section 14.2.4). | Download chapter_14a.R |
| Chapter 14 | Creates a bar chart of Malaysian murder counts (Section 14.3), orders states by count (Section 14.3.1), simplifies the chart theme (Section 14.3.2), and uses accessible colours to distinguish regions (Section 14.3.3). | Download chapter_14b.R |
| Chapter 14 | Loads Northamptonshire burglary counts and compares their distributions across districts using a ridge plot (Section 14.4). | Download chapter_14c.R |
| Chapter 14 | Reshapes South African vehicle-theft rates and creates a scatter plot comparing thefts of and from vehicles, with median reference lines, a fitted trend and labels for municipalities with unusually high rates (Section 14.5). | Download chapter_14d.R |
| Chapter 15 | Counts Chicago aggravated assaults by week and plots a moving average (Section 15.3.2), shows seasonal and hourly patterns (Section 15.4, Section 15.4.1), and maps the highest-density locations by police shift (Section 15.5). | Download chapter_15a.R |
| Chapter 15 | Estimates Chicago aggravated-assault density for each hour using a common grid, animates the highest-density locations, and saves the animation as a GIF (Section 15.6). | Download chapter_15b.R |
| Chapter 16 | Orders the Hungerford shootings and draws curved arrows between consecutive incidents (Section 16.2.2), combines overview and detail maps (Section 16.3), and saves the combined map as an image (Section 7.10). | Download chapter_16.R |