diff --git a/3_visualize.R b/3_visualize.R index b456eedd2e2530fae79b5f813e6c806612937864..66ce1ca5f57ed89aa827af62821cd2a378490f20 100644 --- a/3_visualize.R +++ b/3_visualize.R @@ -4,7 +4,8 @@ p3_targets <- list( tar_target( p3_med_income_png, plot_census_map( - census_data = p2_perc_census_layers_sf[[2]], + census_data = p2_perc_census_acs5_layers_sf[[2]], + percent_leg = FALSE, var = 'estimate', conus_sf = p1_conus_sf, outfile_path = "3_visualize/out/med_income_census_2022.png", @@ -25,7 +26,8 @@ p3_targets <- list( tar_target( p3_tot_black_png, plot_census_map( - census_data = p2_perc_census_layers_sf[[3]], + census_data = p2_perc_census_acs5_layers_sf[[3]], + percent_leg = FALSE, var = 'estimate', conus_sf = p1_conus_sf, outfile_path = "3_visualize/out/tot_black_census_2022.png", @@ -47,7 +49,8 @@ p3_targets <- list( tar_target( p3_perc_black_png, plot_census_map( - census_data = p2_perc_census_layers_sf[[3]], + census_data = p2_perc_census_acs5_layers_sf[[3]], + percent_leg = TRUE, var = 'percent', conus_sf = p1_conus_sf, outfile_path = "3_visualize/out/perc_black_census_2022.png", @@ -68,7 +71,8 @@ p3_targets <- list( tar_target( p3_tot_latino_png, plot_census_map( - census_data = p2_perc_census_layers_sf[[4]], + census_data = p2_perc_census_acs5_layers_sf[[4]], + percent_leg = FALSE, var = 'estimate', conus_sf = p1_conus_sf, outfile_path = "3_visualize/out/tot_latino_census_2022.png", @@ -89,7 +93,8 @@ p3_targets <- list( tar_target( p3_perc_latino_png, plot_census_map( - census_data = p2_perc_census_layers_sf[[4]], + census_data = p2_perc_census_acs5_layers_sf[[4]], + percent_leg = TRUE, var = 'percent', conus_sf = p1_conus_sf, outfile_path = "3_visualize/out/perc_latino_census_2022.png", @@ -110,7 +115,8 @@ p3_targets <- list( tar_target( p3_tot_male_png, plot_census_map( - census_data = p2_perc_census_layers_sf[[5]], + census_data = p2_perc_census_acs5_layers_sf[[5]], + percent_leg = FALSE, var = 'estimate', conus_sf = p1_conus_sf, outfile_path = "3_visualize/out/tot_male_census_2022.png", @@ -131,7 +137,8 @@ p3_targets <- list( tar_target( p3_tot_female_png, plot_census_map( - census_data = p2_perc_census_layers_sf[[6]], + census_data = p2_perc_census_acs5_layers_sf[[6]], + percent_leg = FALSE, var = 'estimate', conus_sf = p1_conus_sf, outfile_path = "3_visualize/out/tot_female_census_2022.png", @@ -147,5 +154,201 @@ p3_targets <- list( font_size = 10 ), format = "file" + ), + + # Disaggregated maps + # percent age maps + tar_target( + p3_perc_under_18_map_png, + plot_census_map( + census_data = p2_census_acs5sub_age_data[[1]], + percent_leg = TRUE, + var = 'percent', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_pop_under_18_2022.png", + pal = "OrRd", + leg_title = "Percent of Total Poulation\nUnder 18, 2022", + width = 6, + height = 4, + dpi = 300, + counties_outline_col = "grey80", + conus_outline_col = 'grey50', + bg_col = "white", + load_font = 'Source Sans Pro', + font_size = 10 + ), + format = "file" + ), + tar_target( + p3_perc_18_24_map_png, + plot_census_map( + census_data = p2_census_acs5sub_age_data[[2]], + percent_leg = TRUE, + var = 'percent', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_pop_under_18-24_2022.png", + pal = "OrRd", + leg_title = "Percent of Total Poulation\n18-24 years, 2022", + width = 6, + height = 4, + dpi = 300, + counties_outline_col = "grey80", + conus_outline_col = 'grey50', + bg_col = "white", + load_font = 'Source Sans Pro', + font_size = 10 + ), + format = "file" + ), + tar_target( + p3_perc_15_44_map_png, + plot_census_map( + census_data = p2_census_acs5sub_age_data[[3]], + percent_leg = TRUE, + var = 'percent', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_pop_under_15-44_2022.png", + pal = "OrRd", + leg_title = "Percent of Total Poulation\n15-44 years, 2022", + width = 6, + height = 4, + dpi = 300, + counties_outline_col = "grey80", + conus_outline_col = 'grey50', + bg_col = "white", + load_font = 'Source Sans Pro', + font_size = 10 + ), + format = "file" + ), + tar_target( + p3_perc_60_plus_map_png, + plot_census_map( + census_data = p2_census_acs5sub_age_data[[4]], + percent_leg = TRUE, + var = 'percent', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_pop_under_60_plus_2022.png", + pal = "OrRd", + leg_title = "Percent of Total Poulation\n60 years & plus, 2022", + width = 6, + height = 4, + dpi = 300, + counties_outline_col = "grey80", + conus_outline_col = 'grey50', + bg_col = "white", + load_font = 'Source Sans Pro', + font_size = 10 + ), + format = "file" + ), + # percent income maps + tar_target( + p3_perc_household_income_map_png, + plot_census_map( + census_data = p2_census_acs5sub_income_data[[1]], + percent_leg = TRUE, + var = 'percent', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_household_income_2022.png", + pal = "OrRd", + leg_title = "Percent allocated\nhousehold income\nin the past 12 months, 2022", + width = 6, + height = 4, + dpi = 300, + counties_outline_col = "grey80", + conus_outline_col = 'grey50', + bg_col = "white", + load_font = 'Source Sans Pro', + font_size = 10 + ), + format = "file" + ), + tar_target( + # empty map + p3_perc_family_income_map_png, + plot_census_map( + census_data = p2_census_acs5sub_income_data[[2]], + percent_leg = TRUE, + var = 'percent', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_family_income_2022.png", + pal = "OrRd", + leg_title = "Percent allocated\family income\nin the past 12 months, 2022", + width = 6, + height = 4, + dpi = 300, + counties_outline_col = "grey80", + conus_outline_col = 'grey50', + bg_col = "white", + load_font = 'Source Sans Pro', + font_size = 10 + ), + format = "file" + ), + tar_target( + # empty map + p3_perc_nonfamily_income_map_png, + plot_census_map( + census_data = p2_census_acs5sub_income_data[[3]], + percent_leg = TRUE, + var = 'percent', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_nonfamily_income_2022.png", + pal = "OrRd", + leg_title = "Percent allocated\nonfamily income\nin the past 12 months, 2022", + width = 6, + height = 4, + dpi = 300, + counties_outline_col = "grey80", + conus_outline_col = 'grey50', + bg_col = "white", + load_font = 'Source Sans Pro', + font_size = 10 + ), + format = "file" + ), + # education related map + tar_target( + p3_tot_18_24_highschool_education_map_png, + plot_census_map( + census_data = p2_census_acs5sub_education_data[[1]], + percent_leg = FALSE, + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/tot_education_18-24_highschool_2022.png", + pal = "OrRd", + leg_title = "Total population 18-24 years\nHigh school graduate (includes equivalency), 2022", + width = 6, + height = 4, + dpi = 300, + counties_outline_col = "grey80", + conus_outline_col = 'grey50', + bg_col = "white", + load_font = 'Source Sans Pro', + font_size = 10 + ), + format = "file" + ), + tar_target( + p3_tot_25_plus_highschool_education_map_png, + plot_census_map( + census_data = p2_census_acs5sub_education_data[[2]], + percent_leg = FALSE, + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/tot_education_25-plus_highschool_2022.png", + pal = "OrRd", + leg_title = "Total population 25 and older\nHigh school graduate (includes equivalency), 2022", + width = 6, + height = 4, + dpi = 300, + counties_outline_col = "grey80", + conus_outline_col = 'grey50', + bg_col = "white", + load_font = 'Source Sans Pro', + font_size = 10 + ), + format = "file" ) ) \ No newline at end of file diff --git a/3_visualize/src/plot_utils.R b/3_visualize/src/plot_utils.R index 89e1440f41be5ae1dfd6086944c9b7fdfd95f6bd..b62a0f7aa5746d6aae25d50c26192e5fab63d69f 100644 --- a/3_visualize/src/plot_utils.R +++ b/3_visualize/src/plot_utils.R @@ -12,14 +12,15 @@ #' @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){ + 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, @@ -30,15 +31,28 @@ plot_census_map <- function(census_data, conus_sf, pal, leg_title, outfile_path, linewidth = 0.2, linetype = "solid") + theme_void() + - scale_fill_distiller( - palette = pal, - direction = 1, - name = leg_title, - limits = c(0, max(census_data[[var]], na.rm = TRUE)), - labels = scales::comma) + 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)