Newer
Older
# Map census data
#'
#' @param census_data, dataframe of census data for specified variable of interest
#' @param conus_sf, sf of conus states outline
#' @param leg_title, character string for legend title
#' @param outfile_path, outfile path for pngs
#' @param viz_config_df `data.frame` width, height, counties outline color, conus outline color, background color, font nam, and font size
#' @param viz_config_pal `data.frame` assign colors for postively and negatively correlated dimensions for census maps

Azadpour, Elmera
committed
#' @param percent_leg if else statement where if TRUE, apply 0-100 legend, otherwise retain 0 - max of variable name
plot_census_map <- function(census_data, conus_sf, leg_title, outfile_path, font_size, var, percent_leg, viz_config_df, viz_config_pal){
font_legend <- viz_config_df$load_font
font_add_google(font_legend)
showtext_opts(dpi = 300, regular.wt = 200, bold.wt = 700)
showtext_auto(enable = TRUE)

Azadpour, Elmera
committed
census_map <- census_data |>
ggplot(aes(fill = .data[[var]])) +
geom_sf(color = viz_config_df$counties_outline_col,
linewidth = 0.05) +
geom_sf(data = conus_sf,
fill = NA,
color = viz_config_df$conus_outline_col,
linewidth = 0.2,
linetype = "solid") +
theme_void() +
theme(text = element_text(family = viz_config_df$font_legend, size = viz_config_df$font_size),
legend.margin = margin(t = 5, b = 5), legend.position = 'bottom', legend.title.align = 0.5) +
guides(fill = guide_colorbar(
title.position = "top",
title.theme = element_text(face = 'bold'),
direction = "horizontal",
position = "bottom",

Azadpour, Elmera
committed
if (percent_leg == FALSE) {
census_map <- census_map +
scale_fill_distiller(

Azadpour, Elmera
committed
direction = 1,
name = leg_title,
limits = c(0, max(census_data[[var]], na.rm = TRUE)),
labels = scales::comma)
} else {
census_map <- census_map +
scale_fill_distiller(

Azadpour, Elmera
committed
direction = 1,
name = leg_title,
limits = c(0, 100),
breaks = c(0, 25, 50, 75, 100)
)
}
ggsave(outfile_path, census_map, width = viz_config_df$width, height = viz_config_df$height, dpi = viz_config_df$dpi, bg = viz_config_df$bg_col)