# Map census data 
#'
#' @param census_data, dataframe of census data for specified variable of interest 
#' @param conus_sf, sf of conus states outline
#' @param pal, assign map palette 
#' @param leg_title, character string for legend title
#' @param outfile_path, outfile path for pngs 
#' @param width, set png width
#' @param height, set png height 
#' @param dpi, set png dots per inches 
#' @param counties_outline_col, assign color to counties outline
#' @param conus_outline_col, assign color to conus outline
#' @param bg_col, assign background color for maps
#' @param load_font, assign font name
#' @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, pal, leg_title, outfile_path, width, height, dpi, 
                            counties_outline_col, conus_outline_col, bg_col, load_font, font_size, var, percent_leg){
  
  font_legend <- load_font
  font_add_google(font_legend)
  showtext_opts(dpi = 300, regular.wt = 200, bold.wt = 700)
  showtext_auto(enable = TRUE)
  
  census_map <- census_data |> 
    ggplot(aes(fill = .data[[var]])) + 
    geom_sf(color = counties_outline_col,
            linewidth = 0.05) +
    geom_sf(data = conus_sf,
            fill = NA,
            color = conus_outline_col,
            linewidth = 0.2,
            linetype = "solid") + 
    theme_void() +
    theme(text = element_text(family = font_legend, size = font_size),
          legend.margin = margin(r = 10))
  
  if (percent_leg == FALSE) {
  census_map <- census_map +     
    scale_fill_distiller(
    palette = pal, 
    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(
       palette = pal, 
       direction = 1,
       name = leg_title,
       limits = c(0, 100),
       breaks = c(0, 25, 50, 75, 100)
     )
 }
  ggsave(outfile_path, census_map, width = width, height = height, dpi = dpi, bg = bg_col)
  
  return(outfile_path)
}