diff --git a/3_visualize.R b/3_visualize.R index 6a4f6c4b1961a808fdd9d4ffdb5b894936e3a9cf..401d42c5efabe5049fbecfbdf87d8593f73fe767 100644 --- a/3_visualize.R +++ b/3_visualize.R @@ -1,6 +1,151 @@ -# source('1_fetch/src/file_utils.R') -# source('3_visualize/src/plot_utils.R') +source('3_visualize/src/plot_utils.R') p3_targets <- list( + tar_target( + p3_med_income_png, + plot_census_map( + census_data = p2_med_income_sf, + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/med_income_census_2022.png", + pal = "OrRd", + leg_title = "Median household income\n2022", + 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_black_png, + plot_census_map( + census_data = p2_tot_black_sf, + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/tot_black_census_2022.png", + pal = "OrRd", + leg_title = "Total Black\n2022", + 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_black_png, + plot_census_map( + census_data = p2_perc_black_sf, + var = 'percent', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_black_census_2022.png", + pal = "OrRd", + leg_title = "Percent Black\n2022", + 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_latino_png, + plot_census_map( + census_data = p2_tot_latino_sf, + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/tot_latino_census_2022.png", + pal = "OrRd", + leg_title = "Total Latino\n2022", + 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_latino_png, + plot_census_map( + census_data = p2_perc_latino_sf, + var = 'percent', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_latino_census_2022.png", + pal = "OrRd", + leg_title = "Percent Latino\n2022", + 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_male_png, + plot_census_map( + census_data = p2_tot_male_sf, + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/tot_male_census_2022.png", + pal = "OrRd", + leg_title = "Total Male\n2022", + 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_female_png, + plot_census_map( + census_data = p2_tot_female_sf, + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/tot_female_census_2022.png", + pal = "OrRd", + leg_title = "Total Female\n2022", + 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 new file mode 100644 index 0000000000000000000000000000000000000000..89e1440f41be5ae1dfd6086944c9b7fdfd95f6bd --- /dev/null +++ b/3_visualize/src/plot_utils.R @@ -0,0 +1,45 @@ +# 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 +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){ + + 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() + + 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)) + + ggsave(outfile_path, census_map, width = width, height = height, dpi = dpi, bg = bg_col) + + return(outfile_path) +}