-
Notifications
You must be signed in to change notification settings - Fork 145
/
2021_02_02_hbcu.Rmd
191 lines (159 loc) · 5.99 KB
/
2021_02_02_hbcu.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
---
title: "TidyTemplate"
date: 2021-02-02
output: html_output
---
# TidyTuesday
Join the R4DS Online Learning Community in the weekly #TidyTuesday event!
Every week we post a raw dataset, a chart or article related to that dataset, and ask you to explore the data.
While the dataset will be “tamed”, it will not always be tidy! As such you might need to apply various R for Data Science techniques to wrangle the data into a true tidy format.
The goal of TidyTuesday is to apply your R skills, get feedback, explore other’s work, and connect with the greater #RStats community!
As such we encourage everyone of all skills to participate!
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(tidytuesdayR)
theme_set(theme_light())
library(scales)
```
# Load the weekly Data
Dowload the weekly data and make available in the `tt` object.
```{r Load}
tt <- tt_load("2021-02-02")
hbcu_all_long <- tt$hbcu_all %>%
gather(metric, enrollment, -Year) %>%
rename(year = Year)
hbcu_all_long %>%
filter(str_detect(metric, " - ")) %>%
separate(metric, c("degree_length", "type"), sep = " - ") %>%
filter(degree_length != "Total") %>%
ggplot(aes(year, enrollment, color = type)) +
geom_line() +
facet_wrap(~ degree_length) +
labs(y = "# enrolled in HBCU",
color = "")
hbcu_all_long %>%
filter(metric %in% c("Males", "Females")) %>%
ggplot(aes(year, enrollment, color = metric)) +
geom_line() +
expand_limits(y = 0) +
labs(y = "# enrolled in HBCU",
color = "")
```
* Most degrees from HBCU are 4-year, and 2-year degrees are almost entirely from public schools
* More women than men enroll in HBCU, and that has been increasing over time since 1980s
```{r}
hbcu_black_long <- tt$hbcu_black %>%
gather(metric, black_enrollment, -Year) %>%
rename(year = Year)
hbcu_compare_long <- hbcu_all_long %>%
full_join(hbcu_black_long, by = c("year", "metric")) %>%
mutate(pct_black = black_enrollment / enrollment)
hbcu_compare_long %>%
filter(metric == "Total enrollment") %>%
ggplot(aes(year, pct_black)) +
geom_line() +
scale_y_continuous(labels = percent) +
expand_limits(y = 0) +
labs(y = "% of HBCU enrollment that is Black")
hbcu_compare_long %>%
filter(metric %in% c("Males", "Females")) %>%
ggplot(aes(year, pct_black, color = metric)) +
geom_line() +
scale_y_continuous(labels = percent) +
expand_limits(y = 0) +
labs(y = "% of HBCU enrollment that is Black")
hbcu_compare_long %>%
filter(str_detect(metric, "Total -")) %>%
mutate(metric = str_remove(metric, "Total - ")) %>%
ggplot(aes(year, pct_black, color = metric)) +
geom_line() +
scale_y_continuous(labels = percent) +
expand_limits(y = 0) +
labs(y = "% of HBCU enrollment that is Black",
color = "")
```
```{r}
gather_race_ethnicity <- function(tbl) {
tbl %>%
mutate_if(is.character, parse_number) %>%
rename(year = Total) %>%
filter(!is.na(year)) %>%
gather(race_ethnicity, value, -year) %>%
mutate(column = ifelse(str_detect(race_ethnicity, "Standard Errors - "), "standard_error", "percent"),
race_ethnicity = str_remove(race_ethnicity, "Standard Errors - ")) %>%
spread(column, value) %>%
mutate(standard_error = abs(standard_error)) %>%
filter(!is.na(percent)) %>%
mutate(race_ethnicity = str_remove(race_ethnicity, "1$"),
percent = percent / 100,
standard_error = standard_error / 100)
}
hs_over_time <- tt$hs_students %>%
slice(-(1:3)) %>%
gather_race_ethnicity()
bach_over_time <- tt$bach_students %>%
gather_race_ethnicity()
education_over_time <- bind_rows(hs_over_time %>% mutate(degree = "High School"),
bach_over_time %>% mutate(degree = "Bachelor's"))
hs_over_time %>%
mutate(race_ethnicity = fct_reorder(race_ethnicity, -percent)) %>%
ggplot(aes(year, percent, color = race_ethnicity)) +
geom_line() +
scale_y_continuous(labels = percent) +
labs(color = "Race/ethnicity",
y = "% of people aged >=25 who graduated HS") +
expand_limits(y = 0)
bach_over_time %>%
mutate(race_ethnicity = fct_reorder(race_ethnicity, -percent)) %>%
ggplot(aes(year, percent, color = race_ethnicity)) +
geom_line() +
scale_y_continuous(labels = percent) +
labs(color = "Race/ethnicity",
y = "% of people aged >=25 who graduated a bachelor's program") +
expand_limits(y = 0)
education_over_time %>%
filter(year >= 1940,
!str_detect(race_ethnicity, "Islander -")) %>%
mutate(degree = fct_relevel(degree, "High School"),
race_ethnicity = str_remove(race_ethnicity, "Total - ")) %>%
mutate(race_ethnicity = fct_reorder(race_ethnicity, percent, last, .desc = TRUE)) %>%
ggplot(aes(year, percent, color = race_ethnicity)) +
geom_line() +
facet_wrap(~ degree) +
scale_y_continuous(labels = percent) +
labs(x = "Year",
color = "Race/ethnicity",
y = "% of people aged >=25 who have this degree") +
expand_limits(y = 0)
```
Bring in a new dataset on fields
```{r}
a25 <- readxl::read_excel("~/Downloads/A-25.xls")
a25_cleaned <- a25 %>%
select(-starts_with("...")) %>%
rename(field_gender = 1) %>%
mutate(group = cumsum(is.na(field_gender))) %>%
filter(!is.na(field_gender)) %>%
select(group, everything()) %>%
mutate(field_gender = str_remove(field_gender, " \\.\\.\\..*")) %>%
group_by(group) %>%
mutate(field = first(field_gender),
gender = ifelse(field_gender %in% c("Men", "Women"), field_gender, "Total")) %>%
ungroup() %>%
select(field, gender, everything()) %>%
select(-field_gender, -group)
a25_cleaned %>%
select(field, gender, contains("HBCU")) %>%
rename(pct_hbcu_total = 3,
pct_hbcu_black = 4) %>%
filter(gender != "Total") %>%
mutate(field = fct_reorder(field, pct_hbcu_black, na.rm = TRUE),
pct_hbcu_black = pct_hbcu_black / 100) %>%
ggplot(aes(pct_hbcu_black, field, fill = gender)) +
geom_col(position = "dodge") +
scale_x_continuous(labels = percent) +
labs(x = "% of first degrees from an HBCU, among Black students",
y = "Field",
fill = "")
```