1 Getting started

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.
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.
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.
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.
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.
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.
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:
- In the Welcome panel, open the New menu and choose New Folder from Template ….
- Choose R Project and click Next.
- Change Folder Name from
my-r-projecttocrime_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. - Click Browse and choose a place on your computer to save the folder. You can choose any location you like, but the
Documentsfolder on your computer is usually a good choice. - Click Next.
- 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.
- If Positron offers options to use
renvor initialise a Git repository, leave them unselected for now, then click Create. - 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:
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:
- In the Explorer panel in the top-left corner, move the pointer over the
CRIME_MAPPINGfolder. - Click New Folder … icon (or right-click and choose New Folder) then name the new folder
data. - Repeat steps 1 and 2 to create the
outputandRfolders. - Right click on the name of the
datafolder in the Explorer panel, click on New Folder … then name the new folderraw. Right click again on thedatafolder to create theprocessedfolder.
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:

Before continuing, compare the Explorer panel in Positron with the screenshot above. Check that:
crime_mappingis the workspace folder shown at the top of the Explorer;data,outputandRare directly insidecrime_mapping;rawandprocessedare directly insidedata; 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.
- Check that
crime_mappingis the folder shown at the top of the Explorer panel. - Open the Command Palette by pressing on your keyboard. The Command Palette lets you find and run commands in Positron.
- Paste the text
Air: Initialize Workspace Folder, then click on the matching command that appears in the list below the Command Palette box. - 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:
- Go to the CARTO website base-map registration page.
- 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.
- You will receive an email from CARTO containing a long string of letters and numbers. This is your API key.
- 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
.Renvironthat R reads whenever you start a new R session. To install theusethispackage if needed and open the.Renvironfile, paste the following code into the R Console and press :
- A text editor will open showing the
.Renvironfile. 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 textCARTO_API_KEY=followed by your API key in double quotes. For example, if your API key was1234567890abcdef, the line you add to the.Renvironfile would look like this:
- Make sure there is no blank space at the start or end of the new line you have added to the
.Renvironfile. Save the file by pressing on your keyboard, then close the text editor. - Changes to the
.Renvironfile 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 :
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.
