Skip to content

Instantly share code, notes, and snippets.

@ivelasq
Last active March 25, 2021 16:58
Show Gist options
  • Save ivelasq/144b857c118af746c0baac30b8fe696d to your computer and use it in GitHub Desktop.
Save ivelasq/144b857c118af746c0baac30b8fe696d to your computer and use it in GitHub Desktop.
K-12 Enrollment Plot
# Enrollment Demographics Stacked Bar Plot
# Libraries ---------------------------------------------------------------
library(tidyverse)
library(geofacet)
library(showtext)
library(cowplot)
font_add_google("Noto Sans", "noto-sans")
font_add_google("Noto Serif", "noto-serif")
showtext_auto()
quartz()
# Function ----------------------------------------------------------------
plot_theme <- function(dat){
dat %>%
ggplot(aes(state, value, fill = variable)) +
geom_col(aes(fill = variable),
position = position_stack(),
color = "white",
size = 0.3,
width = 0.4) +
coord_flip() +
theme_minimal() +
theme(text = element_text(size = 9,
family = "noto-sans"),
plot.subtitle = element_text(size = 14,
family = "noto-serif"),
legend.position = "bottom",
plot.title = element_text(size = 18,
face = "bold",
family = "noto-serif"),
plot.caption = element_text(family = "noto-sans"),
legend.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank()) +
guides(fill = guide_legend(nrow = 1)) +
scale_y_continuous(labels = scales::percent) +
scale_fill_manual(labels = c("american_indian_alaska_native_percent_2018" = "Am. Indian/Alaska Native",
"asian_percent_2018" = "Asian",
"black_percent_2018" = "Black",
"latino_percent_2018" = "Latino",
"pacific_islander_percent_2018" = "Pacific Is.",
"two_or_more_races_percent_2018" = "Two or More Races",
"white_percent_2018" = "White"),
values = c("american_indian_alaska_native_percent_2018" = "#e96c3e",
"asian_percent_2018" = "#69a89d",
"black_percent_2018" = "#afdbf6",
"latino_percent_2018" = "#c3e3d4",
"pacific_islander_percent_2018" = "#355759",
"two_or_more_races_percent_2018" = "#631c48",
"white_percent_2018" = "#14256b"))
}
# Data --------------------------------------------------------------------
dat <-
read_csv(here::here("data", "processed", "state_enroll_long_2018.csv"))
# Plots -------------------------------------------------------------------
p1 <-
dat %>%
filter(str_detect(variable, "percent_2018"),
state != "United States") %>%
arrange(fct_relevel(variable, "white_percent_2018"), value) %>%
mutate(state = fct_inorder(state)) %>%
plot_theme() +
labs(caption = "Source: National Center for Education Statistics")
p2 <-
dat %>%
filter(str_detect(variable, "percent_2018"),
state == "United States") %>%
arrange(fct_relevel(variable, "white_percent_2018"), value) %>%
mutate(state = fct_inorder(state)) %>%
plot_theme() +
theme(legend.position = "none",
plot.margin = unit(c(0,0.2,0,1), "cm"),
axis.text.x = element_blank()) +
labs(title = "Percentage distribution of enrollment in public elementary and secondary schools",
subtitle = "By race/ethnicity and state 2018") +
geom_text(data = dat %>%
filter(variable %in% c("white_percent_2018"),
state == "United States"),
aes(label = paste0("White: ",
scales::percent(round(value, 2)))),
position = position_stack(vjust = 0.5),
color = "white",
size = 4) +
geom_text(data = dat %>%
filter(variable %in% c("black_percent_2018"),
state == "United States"),
aes(label = paste0("Black: ",
scales::percent(round(value, 2)))),
position = position_stack(vjust = 5.75),
color = "black",
size = 4) +
geom_text(data = dat %>%
filter(variable %in% c("latino_percent_2018"),
state == "United States"),
aes(label = paste0("Latino: ",
scales::percent(round(value, 2)))),
position = position_stack(vjust = 2.4),
color = "black",
size = 4)
plot_grid(p2, p1,
rel_heights = c(1, 4),
ncol = 1)
# Save Plots --------------------------------------------------------------
ggsave(here::here("plot1.png"), dpi = "retina", width = 16, height = 9, units = "in")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment