-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
263 lines (263 loc) · 15 KB
/
.Rhistory
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#set workspace
setwd("~/Documents/Work/Spring 2022/Police Budgets/police_budget_analysis_2018_2022")
#load packages
library(reshape2)
library(fitdistrplus)
library(brms)
library(ggplot2)
library(performance)
#load data
data <- readxl::read_xlsx("police_budgets_2018_2022.xlsx")
#subset and clean data
data <- data[-which(is.na(data$p18) | is.na(data$gf18) | is.na(data$p19) | is.na(data$gf19) | is.na(data$p20) | is.na(data$gf20) | is.na(data$p21) | is.na(data$gf21) | is.na(data$p22) | is.na(data$gf22)), ]
data <- data[, which(colnames(data) %in% c("state", "city", "p18", "gf18", "p19", "gf19", "p20", "gf20", "p21", "gf21", "p22", "gf22"))]
colnames(data) <- c("state", "city", "p18", "g18", "p19", "g19", "p20", "g20", "p21", "g21", "p22", "g22")
#create summary data frame
summary_data <- data.frame(Year = c(rep("2018", 2), rep("2019", 2), rep("2020", 2), rep("2021", 2), rep("2022", 2)),
Category = rep(c("Police", "General"), 5),
values = c(mean(data$p18), mean(data$g18)-mean(data$p18),
mean(data$p19), mean(data$g19)-mean(data$p19),
mean(data$p20), mean(data$g20)-mean(data$p20),
mean(data$p21), mean(data$g21)-mean(data$p21),
mean(data$p22), mean(data$g22)-mean(data$p22)))
#plot stacked bar graph
bar_plot <- ggplot(summary_data, aes(fill = Category, y = values, x = Year)) + geom_bar(position = "stack", stat = "identity") +
theme_linedraw() +
scale_y_continuous(breaks = c(0, 50000000, 100000000, 150000000, 200000000, 250000000), labels = c("$0", "$50M", "$100M", "$150M", "$200M", "$250M")) + ylab("Average Budget")
#save and print
ggsave("bar_plot.png", plot = bar_plot, width = 5, height = 4, dpi = 300)
bar_plot
#temporary data frame with 2019 data
temp_data <- data.frame(prop = data$p19/data$g19, overall = data$g19)
#plot
ggplot(temp_data, aes(x = overall, y = prop)) + geom_point() + scale_x_continuous(trans = "log2", labels = scales::label_number_si(accuracy = 0.1, prefix = "$")) + theme_linedraw() +
geom_smooth(method = lm , color = "black", fill = "grey", se = TRUE) + xlab("Total General Fund (Log-Transformed)") + ylab("Proportion on Police")
rm(temp_data)
#convert cities and states to lower case
data$city <- as.factor(tolower(data$city))
data$state <- as.factor(tolower(data$state))
#restructure to be proportion spent on police
data_p <- data.frame(city = data$city, state = data$state, "2018" = data$p18/data$g18, "2019" = data$p19/data$g19, "2020" = data$p20/data$g20, "2021" = data$p21/data$g21, "2022" = data$p22/data$g22)
#convert police data to long format
data_p <- melt(data_p, id.vars = c("city", "state"))
colnames(data_p) <- c("city", "state", "year", "police")
data_p$year <- as.numeric(substr(data_p$year, 2, 5))
#do the same for general fund data and combine
data_g <- data.frame(city = data$city, state = data$state, "2018" = data$g18, "2019" = data$g19, "2020" = data$g20, "2021" = data$g21, "2022" = data$g22)
data_g <- melt(data_g, id.vars = c("city", "state"))
colnames(data_g) <- c("city", "state", "year", "general")
data_g$year <- as.numeric(substr(data_g$year, 2, 5))
#overwrite original data object
data <- cbind(data_p, general = data_g$general)
bar_plot
cowplot(violin_plot, bar_plot)
library(cowplot)
plot_grid(violin_plot, bar_plot)
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police, fill = as.factor(year))) + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
plot_grid(violin_plot, bar_plot)
plot_grid(violin_plot, bar_plot)
?plot_grid
plot_grid(violin_plot, bar_plot, rel_widths = 0.5)
plot_grid(violin_plot, bar_plot, rel_widths = 0.1)
plot_grid(violin_plot, bar_plot, rel_widths = c(1, 2))
plot_grid(violin_plot, bar_plot, rel_widths = c(1, 1.2))
plot_grid(violin_plot, bar_plot, rel_widths = c(1, 1.3))
plot_grid(violin_plot, bar_plot, rel_widths = c(1, 1.4))
plot_grid(violin_plot, bar_plot, rel_widths = c(1, 1.35))
?plot_grid
plot_grid(bar_plot, violin_plot, rel_widths = c(1, 1.35), labels = c("A", "B"))
plot_grid(bar_plot, violin_plot, rel_widths = c(1.35, 1), labels = c("A", "B"))
plot_grid(bar_plot, violin_plot, rel_widths = c(1.3, 1), labels = c("A", "B"))
plot_grid(bar_plot, violin_plot, rel_widths = c(1.35, 1), labels = c("A", "B"))
plot_grid(violin_plot, bar_plot, rel_widths = c(1, 1.35), labels = c("A", "B"))
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police)) + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
plot_grid(violin_plot, bar_plot, rel_widths = c(1, 1.35), labels = c("A", "B"))
scales::show_col(hue_pal()(5))
scales::show_col(hue_pal()(5))
library(scales)
hue_pal(5)
hue_pal()(5)
show_col(hue_pal()(5))
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police, fill = "#00BF7D")) + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
plot_grid(violin_plot, bar_plot, rel_widths = c(1, 1.35), labels = c("A", "B"))
show_col(hue_pal()(5))
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police, fill = "#00BF7D")) + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
violin_plot
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police, fill = "blue")) + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
violin_plot
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police), fill = "blue") + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
violin_plot
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police, color = "blue")) + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
violin_plot
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police, color = "red")) + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
violin_plot
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police, fill = "orange")) + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
violin_plot
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police)) + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
violin_plot
#violin plot
violin_plot <- ggplot(data, aes(x = as.factor(year), y = police, fill = as.factor(year))) + geom_violin() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
violin_plot
#violin plot
avg_bar_plot <- ggplot(data, aes(x = as.factor(year), y = police)) + geom_bar() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
avg_bar_plot
data$police
#violin plot
avg_bar_plot <- ggplot(data, aes(x = as.factor(year), y = police)) + geom_bar() +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
avg_bar_plot
#violin plot
avg_bar_plot <- ggplot(data, aes(x = as.factor(year), y = police)) + geom_bar(stat = "identity") +
theme_linedraw() + theme(legend.position = "none") +
scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police") +
scale_x_discrete(labels = c("2018", "2019", "2020", "2021", "2022")) + xlab("Year")
avg_bar_plot
#set workspace
setwd("~/Documents/Work/Spring 2022/Police Budgets/police_budget_analysis_2018_2022")
#load packages
library(reshape2)
library(fitdistrplus)
library(brms)
library(ggplot2)
library(performance)
library(cowplot)
#load data
data <- readxl::read_xlsx("police_budgets_2018_2022.xlsx")
#subset and clean data
data <- data[-which(is.na(data$p18) | is.na(data$gf18) | is.na(data$p19) | is.na(data$gf19) | is.na(data$p20) | is.na(data$gf20) | is.na(data$p21) | is.na(data$gf21) | is.na(data$p22) | is.na(data$gf22)), ]
data <- data[, which(colnames(data) %in% c("state", "city", "p18", "gf18", "p19", "gf19", "p20", "gf20", "p21", "gf21", "p22", "gf22"))]
colnames(data) <- c("state", "city", "p18", "g18", "p19", "g19", "p20", "g20", "p21", "g21", "p22", "g22")
data$p18
data$p18/data$g18
mean(data$p18/data$g18)
#create summary data frames
summary_data_a <- data.frame(Year = c(rep("2018", 2), rep("2019", 2), rep("2020", 2), rep("2021", 2), rep("2022", 2)),
Category = rep(c("Police", "General"), 5),
values = c(mean(data$p18), mean(data$g18)-mean(data$p18),
mean(data$p19), mean(data$g19)-mean(data$p19),
mean(data$p20), mean(data$g20)-mean(data$p20),
mean(data$p21), mean(data$g21)-mean(data$p21),
mean(data$p22), mean(data$g22)-mean(data$p22)))
summary_data_b <- data.frame(Year = c("2018", "2019", "2020", "2021", "2022"),
avg_percent = c(mean(data$p18/data$g18), mean(data$p19/data$g19), mean(data$p20/data$g20),
mean(data$p21/data$g21), mean(data$p22/data$g22)))
summary(data_b)
summary_data_b
#plot basic bar graph
bar_plot_b <- ggplot(summary_data_b, aes(fill = "red", y = avg_percent, x = Year)) + geom_bar(stat = "identity")
bar_plot_b
#plot basic bar graph
bar_plot_b <- ggplot(summary_data_b, aes(fill = "red", y = avg_percent, x = Year)) + geom_bar(stat = "identity") +
theme_linedraw() + + theme(legend.position = "none") + scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police")
#plot basic bar graph
bar_plot_b <- ggplot(summary_data_b, aes(fill = "red", y = avg_percent, x = Year)) + geom_bar(stat = "identity") +
theme_linedraw() + theme(legend.position = "none") + scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police")
bar_plot_b
show_col(hue_pal()(3))
scales::show_col(hue_pal()(3))
scales::show_col(scales::hue_pal()(3))
scales::show_col(scales::hue_pal()(2))
#plot basic bar graph
bar_plot_b <- ggplot(summary_data_b, aes(fill = "#00BFC4", y = avg_percent, x = Year)) + geom_bar(stat = "identity") +
theme_linedraw() + theme(legend.position = "none") + scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police")
bar_plot_b
#plot basic bar graph
bar_plot_b <- ggplot(summary_data_b, aes(y = avg_percent, x = Year)) + geom_bar(stat = "identity", fill = "#00BFC4") +
theme_linedraw() + theme(legend.position = "none") + scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police")
bar_plot_b
plot_grid(bar_plot_a, bar_plot_b)
#plot stacked bar graph
bar_plot_a <- ggplot(summary_data_a, aes(fill = Category, y = values, x = Year)) + geom_bar(position = "stack", stat = "identity") +
theme_linedraw() +
scale_y_continuous(breaks = c(0, 50000000, 100000000, 150000000, 200000000, 250000000), labels = c("$0", "$50M", "$100M", "$150M", "$200M", "$250M")) + ylab("Average Budget")
plot_grid(bar_plot_a, bar_plot_b)
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.35, 1))
#plot basic bar graph
bar_plot_b <- ggplot(summary_data_b, aes(y = avg_percent, x = Year)) + geom_bar(stat = "identity", fill = "#00BFC4") +
theme_linedraw() + theme(legend.position = "none") + scale_y_continuous(labels = scales::percent_format()) + ylim(0, 1) + ylab("Percent on Police")
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.35, 1))
?scale_y_continuous
#plot basic bar graph
bar_plot_b <- ggplot(summary_data_b, aes(y = avg_percent, x = Year)) + geom_bar(stat = "identity", fill = "#00BFC4") +
theme_linedraw() + theme(legend.position = "none") + scale_y_continuous(labels = scales::percent_format(), limits = c(0, 1)) + ylab("Percent on Police")
bar_plot_b
#plot basic bar graph
bar_plot_b <- ggplot(summary_data_b, aes(y = avg_percent, x = Year)) + geom_bar(stat = "identity", fill = "#00BFC4") +
theme_linedraw() + theme(legend.position = "none") + scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police")
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.35, 1))
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.35, 1))
#plot stacked bar graph
bar_plot_a <- ggplot(summary_data_a, aes(fill = Category, y = values, x = Year)) + geom_bar(position = "stack", stat = "identity") +
theme_linedraw() +
scale_y_continuous(breaks = c(0, 50000000, 100000000, 150000000, 200000000, 250000000), labels = c("$0", "$50M", "$100M", "$150M", "$200M", "$250M")) + ylab("Average Budget")
#plot basic bar graph
bar_plot_b <- ggplot(summary_data_b, aes(y = avg_percent, x = Year)) + geom_bar(stat = "identity", fill = "#00BFC4") +
theme_linedraw() + theme(legend.position = "none") + scale_y_continuous(labels = scales::percent_format()) + ylab("Percent on Police")
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.35, 1))
bar_plot_b
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.35, 1))
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.35, 1))
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.35, 1), labels = c("A", "B"))
png("bar_plot_combined.png", width = 9, height = 4, units = "in" , res = 300)
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.35, 1))
dev.off()
png("bar_plot_combined.png", width = 9, height = 4, units = "in" , res = 300)
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.32, 1), labels = c("A", "B"))
dev.off()
png("bar_plot_combined.png", width = 9, height = 4, units = "in" , res = 300)
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.3, 1), labels = c("A", "B"))
dev.off()
png("bar_plot_combined.png", width = 9, height = 4, units = "in" , res = 300)
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.25, 1), labels = c("A", "B"))
dev.off()
png("bar_plot_combined.png", width = 9, height = 4, units = "in" , res = 300)
plot_grid(bar_plot_a, bar_plot_b, rel_widths = c(1.28, 1), labels = c("A", "B"))
dev.off()