-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path2020-12-29_usa-household-income.Rmd
203 lines (182 loc) · 5.04 KB
/
2020-12-29_usa-household-income.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
---
title: "USA Household Income"
author: "Joshua Cook"
date: "December 29, 2020"
output: github_document
---
## Setup
For the first week of the year, we were told to bring our favorite data from 2020.
I decided to prepare my own data on US household income acquired from the [US Census Bureau](https://www.census.gov/library/publications/2020/demo/p60-270.html).
The processing of that data was conducted in ["2020-12-29_usa-household-income.R"](2020-12-29_usa-household-income.R).
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, comment = "#>", dpi = 500)
library(jhcutils)
library(mustashe)
library(nakedpipe)
library(glue)
library(patchwork)
library(ggtext)
library(magrittr)
library(tidyverse)
theme_set(theme_minimal())
# To shut-up `summarise()`.
options(dplyr.summarise.inform = FALSE)
set.seed(0)
files_dir <- "2020-12-29_usa-household-income_files"
```
## Data
```{r}
annotations <- read_csv(here::here(files_dir, "annotations.csv"))
average_income <- read_csv(here::here(files_dir, "average_income.csv"))
income_by_race <- read_csv(here::here(files_dir, "income_by_race.csv"))
```
## EDA
```{r}
head(average_income)
```
```{r}
head(income_by_race)
```
```{r}
income_by_race <- income_by_race %.% {
filter(
!year %in% c(2017, 2013) |
(year == 2017 & year_annotation == "2") |
(year == 2013 & year_annotation == "3")
)
mutate(
race = str_remove(race, " ALONE$| ALONE(?=,)"),
race = str_to_lower(race)
)
distinct()
}
```
```{r}
income_by_race %.%
{
filter(race != "all races")
mutate(race = str_wrap(race, 20))
distinct(year, race, number)
} %>%
ggplot(aes(year, number)) +
geom_col(aes(fill = race), position = "stack") +
scale_x_continuous(expand = expansion(mult = c(0, 0.02))) +
scale_y_continuous(labels = scales::label_comma()) +
theme(legend.title = element_blank()) +
labs(
x = "year",
y = "number of people",
title = "Number census respondents per race category",
subtitle = "There are still some groups that that represent double counting."
)
```
```{r}
income_by_race %.%
{
mutate(income = fct_inorder(income))
filter(race != "all races")
mutate(race = str_wrap(race, 20))
filter(income %in% c("under 15 000", "200 000 and over"))
} %>%
ggplot(aes(x = year, y = percent)) +
facet_wrap(~income, ncol = 1) +
geom_line(aes(color = race))
```
```{r, fig.height=12}
income_by_race %.%
{
mutate(income = fct_inorder(income))
filter(race != "all races")
mutate(race = str_wrap(race, 20))
} %>%
ggplot(aes(x = year, y = percent)) +
facet_wrap(~income, ncol = 1) +
geom_line(aes(color = race), alpha = 0.8) +
theme(panel.spacing = unit(2, "mm"))
```
```{r}
average_income %.%
{
mutate(
race = str_wrap(race, 20),
race = str_to_lower(race),
median_low = estimated_median - estimated_median_error,
median_high = estimated_median + estimated_median_error,
mean_low = estimated_mean - estimated_mean_error,
mean_high = estimated_mean + estimated_mean_error
)
} %>%
ggplot(aes(x = year, color = race, fill = race)) +
facet_wrap(~race) +
geom_ribbon(
aes(ymin = median_low, ymax = median_high),
alpha = 0.2,
show.legend = FALSE
) +
geom_line(aes(y = estimated_median)) +
geom_ribbon(
aes(ymin = mean_low, ymax = mean_high),
alpha = 0.2,
show.legend = FALSE
) +
geom_line(aes(y = estimated_mean))
```
```{r}
black_name_change_year <- average_income %.% {
filter(race == "BLACK ALONE OR IN COMBINATION")
pull(year)
min()
}
average_income %.%
{
filter(
!year %in% c(2017, 2013) |
(year == 2017 & year_annotation == "2") |
(year == 2013 & year_annotation == "3")
)
mutate(
race = str_to_lower(race),
race = str_remove(race, " alone$| alone(?=,)")
)
filter(!race %in% c("all races", "asian"))
filter(!(year >= black_name_change_year & race == "black"))
mutate(
race = case_when(
str_detect(race, "asian") ~ "asian",
str_detect(race, "black") ~ "black",
TRUE ~ race
),
median_low = estimated_median - estimated_median_error,
median_high = estimated_median + estimated_median_error,
)
} %>%
ggplot(aes(x = year, y = estimated_median)) +
geom_ribbon(
aes(ymin = median_low, ymax = median_high, fill = race),
alpha = 0.2,
show.legend = FALSE
) +
geom_line(aes(color = race)) +
geom_point(aes(color = race), size = 0.8, alpha = 0.5) +
scale_x_continuous(n.breaks = 10) +
scale_y_continuous(labels = scales::dollar_format()) +
scale_color_brewer(
type = "qual", palette = "Set1",
guide = guide_legend(override.aes = list(shape = NA))
) +
scale_fill_brewer(type = "qual", palette = "Set1") +
theme(
plot.title = element_text(hjust = 0.5),
legend.position = c(0.15, 0.80),
legend.title = element_blank(),
legend.key.height = unit(5, "mm"),
legend.text = element_text(size = 10)
) +
labs(
x = "year",
y = "estimated median household income",
title = "Average household income by race in the USA"
)
```
```{r}
```