-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz_big_entities.Rmd
254 lines (197 loc) · 8.24 KB
/
viz_big_entities.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
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
---
title: "Some visualization of Cities"
output:
html_document:
toc: true
toc_float: true
toc_depth: 3
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(results = 'asis', echo = FALSE, warning = FALSE, message = FALSE)
options(scipen = 999)
library(tidyverse)
library(dplyr)
library(purrr)
library(knitr)
library(DT)
library(ggrepel)
#acfrs_city_pop_added_char <- readRDS("acfrs_city_pop_added_char.RDS")
# probelem with integer64
acfrs_city_pop_added_char <- rio::import(here::here("acfrs_city_pop_added_char.csv"))
```
# Total Liabilities
## Cities with Population < 1 Mil
```{r}
acfrs_city_pop_added_char %>%
drop_na(total_liabilities) %>%
filter(population > 1000 & population < 1000000 &
total_liabilities > 0) %>%
select(population, name, total_liabilities, revenues) %>%
mutate(
tot_liabilities_mil = round(total_liabilities/1000000),
tot_revenues_mil = round(revenues/1000000)) -> d1
```
```{r}
d1 %>%
ggplot(aes(population, tot_liabilities_mil)) +
geom_point(color = "#8EA0CB", alpha = .5) +
# add lines
geom_hline(yintercept = round(mean(d1$tot_liabilities_mil)),
linetype = "dashed", color = "red", size = .5) +
annotate(geom = "text", x = 600000, y = 200, label = paste("Mean = ", round(mean(d1$tot_liabilities_mil)))) +
geom_hline(yintercept = round(median(d1$tot_liabilities_mil)),
linetype = "dashed", color = "blue", size = .5) +
annotate(geom = "text", x = 600000, y = 20, label = paste("Median = ", round(median(d1$tot_liabilities_mil)))) +
scale_x_log10() +
scale_y_log10() +
labs(
x = "Population (log scale)",
y = "Million USD (log scale)",
title = "Total Liabilities by population among the cities",
subtitle = "1000 < Population < 1000000") +
theme_minimal()
```
## Cities with Population > 1 Mil
```{r, results= 'asis'}
acfrs_city_pop_added_char %>%
select(population, name, total_liabilities, revenues) %>%
filter(population > 1000000) %>%
mutate(tot_liabilities_mil = round(total_liabilities/1000000),
tot_revenues_mil = round(revenues/1000000),
population_mil = population/1000000) -> d2
d2 %>%
ggplot(aes(population_mil, tot_liabilities_mil)) +
geom_point(color = "#8EA0CB", alpha = .8, size = 4) +
geom_text_repel(aes(label = name), nudge_y = 0.1, nudge_x = 0.1, segment.curvature = -0.1) +
geom_hline(yintercept = round(mean(d2$tot_liabilities_mil)),
linetype = "dashed", color = "red", size = .5) +
annotate(geom = "text", x = 7, y = 40000, label = paste("Mean = ", round(mean(d2$tot_liabilities_mil)))) +
geom_hline(yintercept = round(median(d2$tot_liabilities_mil)),
linetype = "dashed", color = "blue", size = .5) +
annotate(geom = "text", x = 7, y = 15000, label = paste("Median = ", round(median(d2$tot_liabilities_mil)))) +
scale_x_log10() +
scale_y_log10() +
labs(
x = "Population in Million (log scale))",
y = "Million USD (log scale)",
title = "Total Liabilities by population in 10 cities",
subtitle = "Population > 1000000"
) +
theme_minimal()
```
# Total Revenues
## Cities with population < 1 Mil
```{r}
d1 %>%
ggplot(aes(population, tot_revenues_mil)) +
geom_point(color = "#A6D753", alpha = .3) +
geom_hline(yintercept = round(mean(d1$tot_revenues_mil)),
linetype = "dashed", color = "red", size = .5) +
annotate(geom = "text", x = 600000, y = 100, label = paste("Mean = ", round(mean(d1$tot_revenues_mil)))) +
geom_hline(yintercept = round(median(d1$tot_revenues_mil)),
linetype = "dashed", color = "blue", size = .5) +
annotate(geom = "text", x = 600000, y = 20, label = paste("Median = ", round(median(d1$tot_revenues_mil)))) +
scale_x_log10() +
scale_y_log10() +
labs(
x = "Population (log scale)",
y = "Million USD (log scale)",
title = "Total Revenues by population among the cities",
subtitle = " 1000 < Population < 1 Million"
) +
theme_minimal()
```
## Cities with population > 1 Mil
```{r}
d2 %>%
ggplot(aes(population_mil, tot_revenues_mil)) +
geom_point(color = "#A6D753", alpha = .8, size = 4) +
geom_text_repel(aes(label = name), nudge_y = 0.1, nudge_x = 0.1, segment.curvature = -0.1) +
geom_hline(yintercept = round(mean(d2$tot_revenues_mil)),
linetype = "dashed", color = "red", size = .5) +
annotate(geom = "text", x = 7, y = 14000, label = paste("Mean = ", round(mean(d2$tot_revenues_mil)))) +
geom_hline(yintercept = round(median(d2$tot_revenues_mil)),
linetype = "dashed", color = "blue", size = .5) +
annotate(geom = "text", x = 7, y = 5200, label = paste("Median = ", round(median(d2$tot_revenues_mil)))) +
scale_x_log10() +
scale_y_log10() +
labs(
x = "Population in Million (log scale)",
y = "Million USD (log scale)",
title = "Total Revenue by population in 10 cities",
subtitle = "Population > 1000000"
) +
theme_minimal()
```
# Six biggest city governments
```{r}
acfrs_city_pop_added_char %>%
#arrange(desc(population)) %>% slice(1:6) # find 6 largest cities
#filter(name == "Phoenix")
filter(name %in% c("New York City", "Los Angeles", "Chicago", "Houston", "Philadelphia", "Phoenix") & !state.abb %in% c("TN", "OR")) %>%
datatable(fillContainer = FALSE,
options = list(pageLength = 5))
```
# Total revenues and Total Liabitity per person
```{r, results= 'asis', include=FALSE}
acfrs_city_pop_added_char %>%
filter(name %in% c("New York City", "Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia") & !state.abb %in% c("TN", "OR")) %>% # Philadelphia of PA, not TN; Phoenix of AZ, not OR
select(name, total_liabilities, state.abb, revenues, population) %>%
#normalize total_liabilities and revenues by mil population / or by each person
mutate(liability_person = round(total_liabilities/population)) %>%
mutate(revenues_person = round(revenues/population)) -> top6_cities
top6_cities %>%
pivot_longer(cols = 6:7,
names_to = "type",
values_to = "Value") %>%
# mutate(total_liabilities = bit64::as.integer64(total_liabilities),
# revenues = bit64::as.integer64(revenues),
# value = bit64::as.integer64(value)) %>%
select(name, type, Value) -> dp
```
## Total revenues and Total Liabitity per person in 6 largest cities
```{r, echo=FALSE}
dp %>%
ggplot(aes(name, Value, fill = type)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = Value),
position = position_dodge(0.9), vjust = 0) + #give the labels the virtual with
scale_fill_manual(values = c("#8EA0CB", "#A6D753" )) +
labs(
x = "",
y = "USD (raw scale)",
title = "Total revenues and Total Liabitity per person in 6 largest cities"
) +
guides(fill = guide_legend(title = NULL)) +
theme_minimal()
```
## Total revenues and Total Liabitity per person in 6 largest cities - Comparison
```{r, results= 'asis', echo=FALSE}
top6_cities %>%
pivot_longer(cols = 6:7,
names_to = "type",
values_to = "Value") %>%
ggplot(aes(name, Value, fill = type)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = Value),
position = position_dodge(0.9), vjust = 0) + #give the labels the virtual with
#average liabilities per person of all cities
geom_hline(yintercept = round(mean(acfrs_city_pop_added_char$total_liabilities/ acfrs_city_pop_added_char$population)),
linetype = "dashed", color = "#8EA0CB", size = 1) +
#average revenues per person of all cities
geom_hline(yintercept = round(mean(acfrs_city_pop_added_char$revenues/ acfrs_city_pop_added_char$population)),
linetype = "dashed", color = "#A6D753", size = 1) +
scale_fill_manual(values = c("#8EA0CB", "#A6D753" )) +
labs(
x = "",
y = "USD (raw scale)",
title = "Total revenues and Total Liabitity per person in 6 largest cities\nComparison with average values per person of all cities",
) +
guides(fill = guide_legend(title = NULL)) +
theme_minimal()
```
```{r}
# State governments
#
# Similarly, please send a sheet showing the data categories/columns that all state governments could be compared across by using big states like California, Texas, Florida, and New York to give us examples.
```