-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrace_data.Rmd
91 lines (68 loc) · 3.14 KB
/
trace_data.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
title: "The Trace Data Cleaning and Graphing"
author: "Yao Yu"
date: "11/19/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggmap)
library(gganimate)
library(ggthemes)
library(tidyverse)
```
```{r The Trace Okland Data Cleaning}
# Reading in the trace dataset downloaded from here: https://www.thetrace.org/violent-crime-data/
trace_OK <- read.csv("the_trace_data/Oakland_CA/PRR# 21044.csv")
# Cleaning the data to add a year column. first_char is used to detec the first
# character in the address to see if the address is valid (starts with a
# number). Uses as.numeric to check if the first character is a number or not
# and then drops all NAs (non numeric characters).
# I used supressWarnings after finding this StackOverflow:
# https://stackoverflow.com/questions/14984989/how-to-avoid-warning-when-introducing-nas-by-coercion
trace_OK <- trace_OK %>%
mutate(year = format(as.Date(trace_OK$Date, format="%d-%b-%Y"),"%Y")) %>%
mutate(first_chr = substr(trace_OK$Location..Address, 1, 1)) %>%
mutate(number = suppressWarnings(as.numeric(first_chr))) %>%
drop_na(number)
# Filters data for violent crimes that involve a firearm to look specifically at
# gun violence.
trace_OK <- trace_OK %>%
filter(Weapon == "Firearm") %>%
mutate(year = as.integer(year)) %>%
mutate(year = ifelse(year == 10, 2010, year),
year = ifelse(year == 11, 2011, year),
year = ifelse(year == 12, 2012, year),
year = ifelse(year == 13, 2013, year),
year = ifelse(year == 14, 2014, year),
year = ifelse(year == 15, 2015, year),
year = ifelse(year == 16, 2016, year),
year = ifelse(year == 17, 2017, year))
# Writes clean data to a new csv file
write.csv(x = trace_OK, file = "the_trace_data/Oakland_CA/OK_trace_cleaned.csv")
```
```{r Graphing Trace Okland data}
# This following method of graphing points on a map can be found here:
# https://www.littlemissdata.com/blog/maps
# Reads in clean csv data with a specification between 2011-2017 to stay
# consistent with the violent crimes data
trace_OK <- read.csv("clean-data/trace_OK.csv") %>%
filter(year %in% c(2011, 2012, 2013, 2014, 2015, 2016, 2017))
#Google api key (hidden for obvious reasons)
ggmap::register_google(key = "***************************************")
# Gets a google map image of Oakland using Google Map's api
p <- ggmap(get_googlemap(center = c(lon = -122.225, lat = 37.775),
zoom = 12, scale = 2, size = c(640,640),
maptype ='roadmap',
color = 'color'))
# Plots the crime points onto the map of Oakland saved to p, I used theme_map
# after finding this: https://rdrr.io/cran/ggthemes/man/theme_map.html
graphic_OK_Google <- p + geom_point(aes(x = lon, y = lat), data = trace_OK, size = 0.5) +
transition_manual(trace_OK$year) +
labs(title = "Oakland, CA gun violence Data from 2011-2017",
subtitle = "Year: {current_frame}",
caption = "Source: The Trace") +
ggthemes::theme_map(base_size = 12)
# Saves the animated graph into a gif
anim_save(filename = "trace_OK.gif", animation = graphic_OK_Google, path = "graphics/")
```