For When There's No Netflix...
In the airport, my favorite past time is watching whatever show I’ve been binging on Netflix. This time it’s Criminal Minds. However, in my rush to pack, I forgot headphones. So I figured I’d scratch that crime-obsessed itch by investigating Matthew Ashby’s {crimedata} package in R for y’all…
{crimedata} pulls from the Crime Open Database, allowing R users to conveniently explore and analyze crime data from major US cities.
{crimedata}, like {chorrrds} from last week’s tutorial, can be downloaded straight from the command line in R.
devtools::install_github("mpjashby/crimedata")
Your next step is to prepare your newly downloaded {crimedata} package for use:
library(crimedata)
crime_data <- get_crime_data()
The above command will download a list of the URLs you need for comprehensive crime data analysis. After your URLs are downloaded in tidy format, you can easily plot crime location data across several years for any given city. I tried New Orleans first, but the data did not show, so I tried my original home city New York using this command:
get_crime_data(
cities = "Fort Worth",
years = 2014:2017,
type = "core",
output = "sf"
) %>%
filter(offense_group == "homicide offenses") %>%
mutate(offense_year = year(date_single)) %>%
ggplot() +
geom_sf() +
facet_wrap(vars(offense_year))
The resulting plot, shown below, showed some interesting data:
It seems that the locations of the crimes in New York from 2014-2017 as well as the amount of crime has stayed more or less the same. What happens when we increase the time span? Spoiler alert: not much…
I wonder if this pattern is consistent across other cities, especially in a smaller city such as Fort Worth, TX:
As I suspected, there’s more variation in Fort Worth’s amount of crime and locations than shown in New York’s data.
Want to go even deeper?
You can access the homicides15 dataset that’s part of the {crimedata} package and store it as your own variable: It’s this simple:
homicides <- homicides15
Now you can play with the data, shown below, using any packages and commands you’ve learned in your other studies.
Like what you read?
Explore more exciting tutorials across programming languages by clicking below!