Skip to content
Snippets Groups Projects
plot_utils.R 2.75 KiB
Newer Older
  • Learn to ignore specific revisions
  • Azadpour, Elmera's avatar
    Azadpour, Elmera committed
    # 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 
    
    Azadpour, Elmera's avatar
    Azadpour, Elmera committed
    #' @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
    
    #' @param width, set figure width dimension
    #' @param height, set figure height dimension
    
    #' @param percent_leg if else statement where if TRUE, apply 0-100 legend, otherwise retain 0 - max of variable name
    
    #' @param font_size, set font size 
    #' @param barheight, set colorbar bar height
    #' @param barwidth, set colorbar bar width 
    plot_census_map <- function(census_data, conus_sf, leg_title, outfile_path, var, percent_leg, viz_config_df, viz_config_pal, width, height, font_size,
                                barwidth, barheight){
    
      font_legend <- viz_config_df$load_font
    
    Azadpour, Elmera's avatar
    Azadpour, Elmera committed
      font_add_google(font_legend)
      showtext_opts(dpi = 300, regular.wt = 200, bold.wt = 700)
      showtext_auto(enable = TRUE)
    
    Azadpour, Elmera's avatar
    Azadpour, Elmera committed
      census_map <- census_data |> 
        ggplot(aes(fill = .data[[var]])) + 
    
        geom_sf(color = viz_config_df$counties_outline_col,
    
    Azadpour, Elmera's avatar
    Azadpour, Elmera committed
                linewidth = 0.05) +
        geom_sf(data = conus_sf,
                fill = NA,
    
                color = viz_config_df$conus_outline_col,
    
                linewidth = 0.5,
    
    Azadpour, Elmera's avatar
    Azadpour, Elmera committed
                linetype = "solid") + 
        theme_void() +
    
        theme(text = element_text(family = viz_config_df$font_legend, size = font_size),
    
              legend.margin = margin(t = 5, b = 5), legend.position = 'bottom', legend.title.align = 0.5) +
    
    Cee Nell's avatar
    Cee Nell committed
        guides(fill = guide_colorbar(
               title.position = "top",
    
               title.theme = element_text(face = 'bold', family = viz_config_df$font_legend, size = font_size),
    
    Cee Nell's avatar
    Cee Nell committed
               direction = "horizontal",
               position = "bottom",
    
               barwidth = barwidth, #25 
               barheight = barheight #1
               )) 
        
    
      if (percent_leg == FALSE) {
      census_map <- census_map +     
    
        scale_fill_gradientn(
        colors = colorRampPalette(c("#F8F9FF", viz_config_pal))(100), 
    
        name = leg_title,
        limits = c(0, max(census_data[[var]], na.rm = TRUE)),
    
        na.value="#F5F5F5") 
    
         scale_fill_gradientn(
           colors = colorRampPalette(c("#F8F9FF", viz_config_pal))(100), 
    
           na.value="#F5F5F5"
    
      ggsave(outfile_path, census_map, width = width, height = height, dpi = viz_config_df$dpi, bg = viz_config_df$bg_col, units = "in")
    
    Azadpour, Elmera's avatar
    Azadpour, Elmera committed
      
      return(outfile_path)