forked from UBC-MDS/vs-sf_crime-statistic-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_plots.R
66 lines (48 loc) · 2.11 KB
/
static_plots.R
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
library(tidyverse)
library(lubridate)
library(gridExtra)
# load in the crime data
df <- read_csv('data/Police_Department_Incidents_-_Previous_Year__2016_.csv')
# filter the data to the top 4 crimes
top4_df <- df %>%
drop_na() %>%
filter(Category == c('ASSAULT') |
Category == 'LARCENY/THEFT' |
Category == 'VANDALISM' |
Category == 'VEHICLE THEFT')
# create a frequency table for the crimes in each neighborhood
neighborhood_freq <- as.data.frame(table(top4_df$PdDistrict)) %>%
mutate(Var1 = fct_reorder(as.factor(Var1), Freq, .desc = TRUE))
colnames(neighborhood_freq) <- c('neighborhood', 'Freq')
# make the frequency chart for crime over neighborhood
# note: static plot is counts of all 4 crimes together for each neighborhood
bar_chart <- neighborhood_freq %>%
ggplot(aes(x = neighborhood, y = Freq, fill = neighborhood)) +
geom_col() +
labs(title = "Distribution of Crime Reports Across Neighborhood",
x = "Neighborhood",
y = 'Count of Report') +
theme(legend.position = 'none',
title = element_text(size = 14),
axis.title = element_text(size = 14),
axis.text = element_text(size = 6)) +
scale_fill_brewer(palette = 'Spectral')
bar_chart
# make map plot
# note: static plot is density for all top 4 crimes in each neighborhood
map_plot <- top4_df %>% q
ggplot(aes(x = X, y = Y, color = PdDistrict)) +
geom_point(size = 0.07) +
labs(title = 'Crime Density Across Neighborhoods',
x = "Longitude",
y = "Latitude") +
guides(color =guide_legend(title = "Neighborhood",
size = 12,
override.aes = list(size=5))) +
theme(title = element_text(size = 16),
axis.title = element_text(size = 14),
axis.text = element_text(size = 14),
legend.text = element_text(size = 12)) +
scale_colour_brewer(palette = 'Spectral')
map_plot
grid.arrange(my_plot, map_plot, bar_chart, ncol = 2, nrow = 2)