Last active
February 10, 2023 19:23
-
-
Save elipousson/99f6a6dbc38285b8a271441303d1877a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
# requires bcpss, getdata, mapmaryland, ggplot2 and dplyr | |
params <- | |
list( | |
program_name = c( | |
"John Ruhrah Elementary/Middle", | |
"John Ruhrah Elementary" | |
), | |
program_number = 228 | |
) | |
prep_baltimore_enrollment <- function(number = NULL, | |
name = NULL, | |
replace_name = NULL, | |
grade_range = "All Grades", | |
race = NULL) { | |
enrollment <- bcpss::baltimore_enrollment | |
if (!is.null(number)) { | |
enrollment <- | |
dplyr::filter( | |
enrollment, | |
school_number %in% number | |
) | |
} | |
if (!is.null(name)) { | |
enrollment <- | |
dplyr::filter(enrollment, school_name %in% name) | |
} | |
if (!is.null(grade_range)) { | |
cliExtras::cli_warn_ifnot( | |
"{.arg race} is ignored if {.arg grade} is provided." = is.null(race) | is.na(race) | race == "All" | |
) | |
select_grade_range <- grade_range | |
enrollment <- | |
dplyr::filter( | |
enrollment, | |
.data[["grade_range"]] %in% select_grade_range, | |
.data[["race"]] == "All" | |
) | |
} else if (!is.null(race)) { | |
enrollment <- | |
dplyr::filter(enrollment, is.na(grade_range)) | |
if (is.character(race)) { | |
select_race <- match.arg( | |
race, | |
unique(bcpss::baltimore_enrollment$race), | |
several.ok = TRUE | |
) | |
enrollment <- | |
dplyr::filter(enrollment, .data[["race"]] %in% select_race) | |
} | |
} | |
if (!is.null(replace_name)) { | |
enrollment <- getdata::replace_with_xwalk(enrollment, "school_name", replace_name) | |
} | |
enrollment | |
} | |
plot_enrollment_year <- function(x) { | |
stopifnot( | |
all(rlang::has_name(x, c("year", "enrolled_count", "school_name"))) | |
) | |
ggplot2::ggplot( | |
data = x, | |
ggplot2::aes( | |
x = year, | |
y = enrolled_count, | |
color = school_name | |
) | |
) + | |
ggplot2::geom_point(size = 2) + | |
ggplot2::geom_line(ggplot2::aes(group = school_name), linewidth = 1.05) + | |
ggplot2::scale_color_viridis_d(end = 0.8) + | |
ggplot2::labs( | |
color = "School", | |
x = "Year", | |
y = "Enrollment" | |
) | |
} | |
plot_theme <- | |
hrbrthemes::theme_ipsum_rc( | |
base_size = 18, | |
strip_text_size = 20, | |
strip_text_face = "bold", | |
plot_margin = ggplot2::margin(5, 5, 5, 5) | |
) | |
prep_baltimore_enrollment(params$program_number) |> | |
plot_enrollment_year() + | |
plot_theme + | |
maplayer::theme_legend("bottomright") + | |
ggplot2::scale_y_continuous(expand = expansion(add = c(400, 100))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment