1  Getting started

Students organise folders beside a computer workspace displaying a point map.

Get started with crime mapping by learning about why putting crime on maps is useful, get a tour of the Positron software we will use throughout this course and learn how to keep your files organised.

1.1 Welcome

Welcome to Learn Crime Mapping with R. Together, we will learn how to use maps and spatial analysis techniques to understand crime. Watch this video to learn more.

In this course, we’re going to learn how to make maps to help us to understand patterns of crime, so we can take steps to prevent crime or respond to it in other ways. One simple example of that is this map of shootings in part of New York City, which we could use to identify maybe where a new trauma hospital should be built, or where we should work with community groups to identify how to stop shootings happening.

My name is Dr Matt Ashby, and I’m a lecturer in crime science at University College London. My work focuses on how police can use data to prevent crime, so I spend a lot of time using maps and other types of data analysis to understand crime patterns. Before I was a lecturer, I was a police officer, and I have also worked in intelligence analysis for the police, so I’ve created and used crime maps in lots of different circumstances.

In this course, we’ll make maps using a programming language called R. If you haven’t learned a programming language before, then you might find this a bit daunting, but the course doesn’t require any previous programming experience, and we will help you at every step along the way. By the end of this course, you’ll be able to use R to produce maps and reports that people can use to effectively respond to crime.

We’re going to use R because it’s designed specifically for data analysis, and it has very good mapping capabilities. There are also lots of resources online to help people learn R, as well as a big community of R users who are willing to help others. That means that if you want to develop your R skills further after finishing this course, there will be lots of support available.

This course is made up of interactive tutorials that will help you learn how to make maps step by step. Each tutorial includes videos like this one that help explain the concepts you need to understand to make good maps, as well as including interactive exercises that will help you write each piece of code. You can work through each tutorial at your own pace and get help at any point.

I hope that you enjoy this course. I’d really like to hear how to make this course better, so if you have any feedback, then please get in touch.

1.2 Why put crimes on maps?

This course is about how we can use maps and other spatial analysis tools to help understand, prevent and respond to crime. Watch this video to understand why spatial analysis is a useful tool for understanding crime.

Perhaps the most important thing to know about crime is that it doesn’t happen at random. Crime is heavily concentrated in several different ways. We know that a small number of offenders are responsible for a big chunk of all crime. We know that unfortunately a small number of people are at higher risk of being victims of crime over and over again. And we also know that crime is heavily concentrated in a small number of places. So the reason we put crime on maps is to help us understand where different types of crime happen most often.

Take this map of homicides in the city of Medellín in Colombia. We can see straightaway that homicides aren’t evenly distributed throughout the city – a big proportion of all the homicides happen in a small number of places. There are more homicides in the city centre and very few in some outlying districts. If we wanted to set up a project to prevent homicides in Medellin, or we wanted to know where it would be best to build a new trauma hospital, the information on this map could be very useful.

Researchers have done lots of work to find out exactly how concentrated crime is in particular places. Generally, this has found that about half of crime happens on about five percent of streets, with violent crimes tending to be slightly more concentrated than property crimes. That also means that most places experience either very little crime or no crime at all. This rule – that most crime happens in a small minority of places – is known as the law of crime concentration.

The early research on this topic was mostly done in the United States, with researchers finding that half of crime in Seattle happened on five percent of streets, while in Cincinnati half of crime happened on six percent of streets. More recent research has found similar results for other countries. For example half of violence in Oslo in Norway happens in six percent of small areas, while in Kaduna in Nigeria half of burglaries happen at about eight percent of addresses. In fact, this heavy concentration of crime in a small number of places has been found for every type of crime in every country that researchers have studied.

It’s important to note when we’re talking about crime concentration that crime is primarily concentrated at small places such as individual addresses or streets. Even in an area that might be described as a high crime neighbourhood, we will typically find that most crime happens at a few addresses or on a few streets, with the rest of the neighbourhood having quite low levels of crime. That is why in this course, we will primarily be looking at concentrations of crime at micro places, such as addresses and streets, rather than analysing crime at a neighbourhood level.

All this means that if we want to understand crime, or to try to stop it, then it’s really important to know where crime happens. And that’s why it’s useful to put crime on maps.

To summarise: Crime doesn’t happen at random – it is concentrated in several different ways. Most crime happens in a small number of high-crime places, while most places have no crime at all. The law of crime concentration applies to different types of crime in many different countries.

QuizConcentrations of crime

Why is it important to map crime?

What does the law of crime concentration state?

According to research on the concentration of crime, how much crime typically happens in the 5–8% of streets or addresses with the most crime?

Why do researchers often prefer to focus on micro places, such as streets and addresses, rather than neighbourhoods?

Which of the following findings is consistent with the law of crime concentration?

Weisburd, D. (2015). The law of crime concentration and the criminology of place. Criminology, 53(2), 133-157.

Johnson, S. (2010). A brief history of the analysis of crime concentration. European Journal of Applied Mathematics, 21(4-5), 349.

Farrell, G. (2015). Crime concentration theory. Crime Prevention and Community Safety, 17(4), 233-248.

1.3 Why is crime concentrated in space?

Why is crime concentrated in space? Watch this video to find out more about how our environment influences opportunities for crime and how that causes clusters of different crimes.

One of the things that we know about all types of crime is that they are highly concentrated in a small number of places. But why is crime concentrated in so few places?

The simplest answer to that question is that opportunities for different types of crime concentrate in some places more than others. Shoplifting tends to concentrate on streets with lots of shops, especially those shops that sell items that are easy to steal. Meanwhile, alcohol-related violence at weekends tends to concentrate in areas with lots of places selling alcohol, especially bars that don’t mind selling drinks to people who are already drunk.

Sometimes a particular type of place will create opportunities for multiple different types of crime. For example, this street has lots of cars parked in it and also has lots of houses, so there might be opportunities for both residential burglaries and vehicle thefts.

But not all crimes happen in the same places. Fuel theft tends to concentrate along major roads where petrol stations are sited, while theft of farm equipment is much more likely to happen in the countryside.

Saying that crime is concentrated because opportunities for crime are concentrated might seem a bit obvious. But understanding how opportunities for crime concentrate turns out to be really important in understanding and preventing crime more generally.

One way that we can better understand opportunities for crime is to use a model like the routine activities approach. This model doesn’t tell us where a specific crime will happen, but instead tells us where opportunities for crime are most likely to occur.

The routine activities approach says that an opportunity for crime happens when a potential offender and a potential crime target come together at a place, as long as there is no-one there who can stop that crime occurring.

A potential offender is anyone who has the knowledge and skills necessary to commit a particular crime, and who feels that committing a crime is a valid thing to do in a given set of circumstances.

A target for crime is whatever the offender is trying to do something to. For a violent crime, the target is the victim – the person being assaulted, for example. For property crimes, the target is whatever the offender is trying to steal or damage. And for crimes like drug dealing, the target is the person the dealer is trying to sell the drugs to.

Sometimes offenders and targets will come together without creating a crime opportunity, because there is someone present who can stop any crime occurring. These people are known as controllers of crime, although you might sometimes hear them referred to as guardians.

Controllers are people who can make offenders less likely to attack targets, make targets themselves less vulnerable, or make places less suitable for offenders and targets to come together.

In most cases controllers aren’t people who are paid to prevent crime like police officers – usually controllers are people who prevent crime incidentally while going about their day-to-day activities.

For example, a potential burglar’s partner might insist they go to the cinema on a particular evening, incidentally stopping the person finding any suitable burglary targets.

Likewise, if someone travels on a late-night train looking for a sleeping passenger they can sexually assault, the presence of other passengers might make them decide the risk of offending is too high.

Sometimes the routine activities approach is represented using the crime triangle. The basic idea here is that all three sides of the triangle – offenders targets and places – influence how many opportunities for crime there will be, with controllers able to influence each side of the triangle.

Since offenders and targets are both concentrated in particular places, we can use this to explain why different crimes concentrate in some places more than others.

But it’s important to remember that different combinations of offenders, targets and controllers create different opportunities for different crimes, which explains why we see different types of crime cluster in different places.

This is important because it means that we will usually need to analyse different types of crime separately, for example by creating separate maps for different crime types rather than showing multiple types of crime on a single map.

To summarise: Opportunities for crime are concentrated in some places more than others Different crimes concentrate in different places because different features of the environment create opportunities for different crimes We can use the routine activities approach to help understand why crime concentrates in particular places

QuizRoutine activities

Why are opportunities for crime concentrated in some places more than others?

According to the routine activities approach, which of the following is not required for a crime opportunity to occur?

What role do controllers play in preventing crime, according to the routine activities approach?

Why do different types of crime concentrate in different places?

What is a target in the context of the routine activities approach?

Santos, R. B. (2015). Routine Activity Theory: A Cornerstone of Police Crime Analyst Work. In The Criminal Act: the role and influence of routine activity theory by Andresen, M. A. and Farrell, G. Palgrave Macmillan.

Cohen, L. E., and Felson, M. (1979). Social Change and Crime Rate Trends: A Routine Activity Approach. American Sociological Review, 44(4), 588–608.

1.4 Finding your way around Positron

We will use a piece of software called Positron for almost all of this course. Positron is designed specifically for data analysis. If you have previously used either VS Code or RStudio, you will probably find Positron quite familiar, but if you haven’t used either of those before, don’t worry – we will guide you through the basics of using Positron in this course.

Watch the first three minutes of this video to find your way around the different panels in the Positron window. For now, don’t worry about opening a folder – we will cover that shortly. Also don’t worry if you don’t understand all the terms used in the video.

This transcript covers the complete video. For this chapter, you only need to watch or read the first three minutes, up to the introduction of Positron Assistant.

Hi. I’m Sara, a data science educator at Posit. In this video, I’ll give you a brief tour of Positron, Posit’s next-generation data science IDE.

Positron is designed specifically for doing data science. It combines a modern coding environment with tools tailored for working with data. Positron supports both R and Python and makes it easy to switch between the languages.

If you’ve used RStudio or VS Code, you’ll notice some familiar elements. Positron is informed by our experience building RStudio and is a fork of VS Code.

You can download Positron for Mac, Windows, or Linux on the Positron website. Next, let’s take a look at some of Positron’s key features.

When you first open up Positron, it’ll look something like this. The first step is to open a folder. You can either open an existing folder or a new folder. Opening a folder will create a workspace and set the working directory similar to opening up a project in RStudio. I’m going to open an existing folder.

Let’s take a look at the layout of Positron. All the way in the left hand side, the activity bar gives access to features like the file explorer, Positron Assistant, and extensions. Primary sidebar changes depending on what you’ve selected in the activity bar. So right now, it’s showing our files because we have the file explorer selected.

In the center, we have the editor where we write our code. At the bottom, you can find the console and the terminal.

And over on the right hand side, you have the secondary sidebar. This is where plots will show up, where we see our loaded variables, explore database connections, and look at documentation.

Positron supports multiple R and Python interpreters. The active interpreter is shown over here in the upper right hand corner. You can click on the interpreter picker to switch between R and Python or between R and Python versions.

You’ll notice that when I selected R, the console is now an R console. You can also switch between languages or versions in the console. The active interpreter will be highlighted.

Loaded variables will show up in the variables pane on the right hand side. You can inspect variables by expanding them.

For data frames, you’ll notice this table icon. Clicking on that will launch the data explorer, which gives you a spreadsheet style view of the data.

You can use the Data Explorer to get a high level overview of your data. You can see the distribution of values in each of the columns as well as filter by condition. You can also sort by a column and view missing data percentages and summary statistics all without writing additional code.

Positron Assistant is an AI tool that provides LLM integration within Positron.

You can access Positron Assistant from the activity bar. Use Positron Assistant to generate code, debug, or get suggestions for next steps. Positron Assistant can access context from the console, variables pane, and plots pane. But you can also manually attach files or folders to give it additional context.

Positron Assistant has three chat modes. Ask, Edit, and Agent.

Use ask mode when you want to ask questions, get help with debugging, or use Assistant to generate code.

For example, we could ask it what we should do in our analysis of the diamonds data. It can reference our Quarto document and see what we’ve already done and suggest next steps.

On edit mode, Assistant will suggest code changes.

So let’s say I have another file and I’m getting an error. On edit mode, I can ask Positron Assistant to help fix the problem. And notice that it now knows that read_diamonds.py is the current file.

Assistant identifies the issue and supplies a fix that I can accept or undo.

On agent mode, Assistant can figure out the next steps needed for your request and autonomously carry out those steps.

On your behalf, Positron Assistant could execute code in the console, create and modify files, and identify the next steps based on the results of the previous steps.

For example, we could ask it to move this code that’s in read_diamonds.py into a new Quarto file.

Positron Assistant creates a new Quarto document and moves my code over.

Note that Positron Assistant is currently a preview feature.

Just like in RStudio, you can pull up documentation for R or Python functions directly within the IDE. For either R or Python, you can either use question mark or help.

Pull up the documentation in the help pane. You can also use F1 key. This allows you to easily reference documentation without leaving Positron.

There’s a lot more you can do with Positron, but I hope this gave you a sense of what’s possible. Use the following link to download Positron and get started.

The Positron interface is divided into several panels, each of which has a different purpose. Some of the panels contain multiple tabs, so there are a lot of different things you can do in Positron. But on this course we will only use some of those, so it shouldn’t be overwhelming.

Main panels in Positron – click to expand

Annotated Positron screenshot with five labelled areas: Activity bar at the far left, Primary side bar with the Explorer beside it, Editor in the centre, Panel with the R Console below, and Secondary side bar with session information and a plot on the right. The list below explains each area.

The main panels in the Positron interface are:

  • Activity bar: This vertical bar on the far left contains icons for switching between different tools in Positron. The tool shown in the screenshot above is the Explorer. In the Activity bar you can also select tools that allow you to search in files and folders, load and manage extensions to complete different tasks, and interact with online services that can help you complete your work.
  • Primary side bar: This panel is next to the Activity bar and shows whatever tool is currently selected in the Activity bar. In the screenshot above, the Explorer is selected, which shows the files and folders in the folder you are working in.
  • Editor: This is where you edit different files, for example to write R code to complete a particular analytical task. You can have multiple files open at once, and switch between them using the tabs at the top of the Editor.
  • Panel: This panel is below the Editor and shows different types of output, for example the results of running R code, or messages about problems that have occurred. You can switch between different types of output using the tabs at the top of the Panel. Most of the time we will be working in the Console tab, which shows the results of running R code.
  • Secondary side bar: This panel is on the far right and shows information about your R session, for example details about data that you have loaded (see Section 2.3.3). The bottom half of the Secondary side bar shows plots that you have created in R, and allows you to export them as image files.

The Positron Cheat Sheet highlights some of the features available in Positron and gives a list of available keyboard shortcuts. You might want to keep this cheat sheet open in a separate window while you work through the course, or print off a version to keep with you.

1.5 Keeping your files organised

Throughout this course you will use and create several different types of file, for example data files, files containing R code and output files containing maps, charts and reports. It is very important that you keep these files organised in a way that makes it easy to find them again later. Getting into the habit of keeping your files well organised will help a lot later on when you have to deal with multiple files to achieve an objective.

ImportantCommon mistake: bad file organisation

One of the most common mistakes that people make when writing code is not keeping their files organised. This can make it much harder to find and fix problems with your code, something we will return to in Chapter 8. Make sure you organise your files well from the start (that’s right now) to avoid problems later on.

To keep your files organised, we will keep all the files used for this course in a single folder on your computer called crime_mapping, which we will then divide up into subfolders for storing different types of files. To create the crime_mapping folder in Positron:

  1. In the Welcome panel, open the New menu and choose New Folder from Template ….
  2. Choose R Project and click Next.
  3. Change Folder Name from my-r-project to crime_mapping. Note that the folder name is all lower-case letters with an underscore between the two words. This is important because it makes it easier to use the folder name in R later on.
  4. Click Browse and choose a place on your computer to save the folder. You can choose any location you like, but the Documents folder on your computer is usually a good choice.
  5. Click Next.
  6. If Positron asks you to choose a version of R, choose the latest numbered version in the list. If no version of R is listed, R might not be installed correctly on your computer.
  7. If Positron offers options to use renv or initialise a Git repository, leave them unselected for now, then click Create.
  8. Click Current Window. Positron will open the new folder as your workspace and start an R session.

Now that we have created a folder to store all the files for this course, we will create a few subfolders within it to keep different types of files separate. The structure of the crime_mapping folder will look like this:

crime_mapping/
├── data/
│   ├── raw/
│   └── processed/
├── output/
└── R/

These folders have different purposes:

  • data/ is used to store all the data we will use in this course. It is divided into two subfolders:
    • data/raw/ contains the original data files exactly as they were downloaded.
    • data/processed/ contains data that have been cleaned, transformed or combined using R.
  • output/ contains everything produced by your analysis, such as maps, charts and reports.
  • R/ will later be used to store R scripts.

The distinction between data/raw/ and data/processed/ is particularly important. Files in data/raw/ should be kept sterile. Once a file has been downloaded into this folder, you should not edit it or overwrite it. Instead, use R to read the original file, perform any cleaning or processing, and save the result into data/processed/. This means you can always return to the original data if you need to repeat your analysis. Keeping the original data sterile means that many mistakes you might make while cleaning or processing the data can be easily corrected by simply re-running your R code to produce a new processed file from the original raw file.

To create these folders in Positron:

  1. In the Explorer panel in the top-left corner, move the pointer over the CRIME_MAPPING folder.
  2. Click New Folder … icon (or right-click and choose New Folder) then name the new folder data.
  3. Repeat steps 1 and 2 to create the output and R folders.
  4. Right click on the name of the data folder in the Explorer panel, click on New Folder … then name the new folder raw. Right click again on the data folder to create the processed folder.
ImportantFolder and file names should be lower case

Folder and file names are easier to use in R when they contain only lower-case letters, numbers, hyphens and underscores. Avoid spaces and special characters where possible. In this book, we use lower-case words separated by underscores, a convention called snake case. For example, use crime_mapping rather than Crime Mapping.

The explorer panel should now look like this:

Screenshot of the Positron interface showing the Explorer panel. The workspace folder, named CRIME_MAPPING, is expanded and contains three items: a data folder, an output folder and an R folder. The data folder is also expanded to show two subfolders named processed and raw. The data folder is currently selected and the toolbar above the Explorer displays icons for creating a new file, creating a new folder, refreshing the Explorer and collapsing all folders. The screenshot illustrates the recommended folder structure for organising files in the course.

TipCheck your work

Before continuing, compare the Explorer panel in Positron with the screenshot above. Check that:

  • crime_mapping is the workspace folder shown at the top of the Explorer;
  • data, output and R are directly inside crime_mapping;
  • raw and processed are directly inside data; and
  • you have not accidentally created one of these folders inside another folder.

If the crime_mapping folder is not shown at the top of the Explorer, you should click on the Open Folder … icon in the Explorer toolbar and select the crime_mapping folder to open it. this is important since otherwise the next step will not work.

If the folders are not in the right places, correct that now. Correcting the structure will make the instructions in later chapters much easier to follow.

1.6 Formatting your R code automatically

R code can be written in lots of different ways, some of them easier to read and maintain than others. Well-formatted code is easier to read and less likely to contain mistakes, which we will cover in more detail in Chapter 8. Positron includes a tool called Air that can format your R code automatically whenever you save a file.

Air is included with Positron, but you need to enable automatic formatting for your workspace. You only need to do this once for the crime_mapping workspace.

  1. Check that crime_mapping is the folder shown at the top of the Explorer panel.
  2. Open the Command Palette by pressing on your keyboard. The Command Palette lets you find and run commands in Positron.
  3. Paste the text Air: Initialize Workspace Folder, then click on the matching command that appears in the list below the Command Palette box.
  4. If you are asked to choose a workspace folder, select crime_mapping.

This makes two small changes to the crime_mapping folder that you created above by creating a file called air.toml and a folder called .vscode. These tell Positron how to format your code. You do not need to edit these files or open them, but you should not delete them.

Positron will now format your R scripts automatically whenever you save them.

1.7 Registering to use base maps

Throughout this book we will make maps that include a layer called a base map, which shows the streets, buildings and other features of the area we are mapping. We will learn more about base maps in Section 7.7. To include base maps in our own maps, we need to use an external service as a source of base-map data. There are several base-map providers available, and in this book we will use a service called CARTO. To do that we need to first register for an account (called an API key) with that service.

To register for a free CARTO API key:

  1. Go to the CARTO website base-map registration page.
  2. Complete the form marked Request a key. You should answer the question ‘Is this a commercial project?’ with ‘No’. You should answer the question ‘What are you building?’ with ‘Learning how to make crime maps’. Do not enable any of the options in the ‘Restrictions’ section. Accept the terms and conditions then submit the form.
  3. You will receive an email from CARTO containing a long string of letters and numbers. This is your API key.
  4. R needs to know your API key to access the CARTO service. To do that, you need to store the API key somewhere where R can access it. The easiest way to do that is to put the API key in a special file called .Renviron that R reads whenever you start a new R session. To install the usethis package if needed and open the .Renviron file, paste the following code into the R Console and press :
Code 1.1
R Console
if (!requireNamespace("usethis")) {
  install.packages("usethis")
}
usethis::edit_r_environ()
  1. A text editor will open showing the .Renviron file. If there are already any lines in the file, do not delete or change them. Instead, add a new line to the end of the file and type the text CARTO_API_KEY= followed by your API key in double quotes. For example, if your API key was 1234567890abcdef, the line you add to the .Renviron file would look like this:
CARTO_API_KEY="1234567890abcdef"
  1. Make sure there is no blank space at the start or end of the new line you have added to the .Renviron file. Save the file by pressing on your keyboard, then close the text editor.
  2. Changes to the .Renviron file will not take effect until you start a new R session. To do that, click the Restart R (⟳) button in the Console panel in Positron.

1.8 Checking your setup

Before moving on to making your first crime map, check that your software and workspace are set up correctly. Paste the following code into the R Console and press :

Code 1.2
R Console
learncrimemapping::check_workspace()

The function prints a message for each check, including those that succeed. Read through the output and look for messages saying PROBLEM or MANUAL CHECK. Complete any instructions given in these messages: they will explain what you need to fix or check yourself, and may include R code to paste into the Console.

After completing the instructions, run learncrimemapping::check_workspace() again to check your setup. If you are unsure how to resolve a problem, ask your lecturer for help and show them the output so they can see which checks worked and which need attention.

1.9 In summary

We have explored why crime mapping is useful for understanding crime and why crime is typically concentrated in space, and practised finding our way around Positron and keeping our files organised. In Chapter 2 we will produce our first crime map in R.

If you’re not feeling too confident at this point in the course, don’t worry – learning something new is always a bit of a roller coaster and there is lots of help available in subsequent chapters.

Cartoon chart with time on the horizontal axis and how much the learner thinks they know about R on the vertical axis. The line repeatedly rises and falls, accompanied by hopeful and frustrated faces, illustrating that confidence can fluctuate while learning. A note at one peak encourages joining the R community.

Artwork by Allison Horst