From 001b17b89de9ddb0aaa03778fb1ff2c3c3f809db Mon Sep 17 00:00:00 2001
From: elmeraa <eazadpour@usgs.gov>
Date: Fri, 22 Mar 2024 09:56:07 -0700
Subject: [PATCH] update maps

---
 3_visualize.R                | 311 ++++++++++++++++++++---------------
 3_visualize/src/plot_utils.R |  10 +-
 2 files changed, 185 insertions(+), 136 deletions(-)

diff --git a/3_visualize.R b/3_visualize.R
index 5047d30..d0bcfb3 100644
--- a/3_visualize.R
+++ b/3_visualize.R
@@ -1,48 +1,63 @@
 source('3_visualize/src/plot_utils.R')
-
+# For case studies approach: select one demographic characteristic (hispanic/latino), 
 p3_targets <- list(
   tar_target(
     p3_med_income_png,
     plot_census_map(
       census_data = p2_perc_census_acs5_layers_sf[[2]],
       percent_leg = FALSE,
-      var = 'estimate', 
-      conus_sf = p1_conus_sf, 
+      var = 'estimate',
+      conus_sf = p1_conus_sf,
       outfile_path = "3_visualize/out/med_income_census_2022.png",
       leg_title = "Median household income, 2022",
-      viz_config_df = viz_config_df
-    ),
-    format = "file"
-  ),
-  
-  tar_target(
-    p3_tot_black_png,
-    plot_census_map(
-      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",
-      leg_title = "Total Black, 2022",
-      viz_config_df = viz_config_df
+      viz_config_df = viz_config_df,
+      viz_config_pal = viz_config_pal$pal_positive
     ),
     format = "file"
   ),
-  
-  
   tar_target(
-    p3_perc_black_png,
+    p3_perc_household_income_map_png,
     plot_census_map(
-      census_data = p2_perc_census_acs5_layers_sf[[3]],
+      census_data = p2_census_acs5sub_income_data[[1]],
       percent_leg = TRUE,
-      var = 'percent',
+      var = 'percent', 
       conus_sf = p1_conus_sf, 
-      outfile_path = "3_visualize/out/perc_black_census_2022.png",
-      leg_title = "Percent Black, 2022",
-      viz_config_df = viz_config_df
+      outfile_path = "3_visualize/out/perc_household_income_2022.png",
+      leg_title =  "Percent allocated household income in the past 12 months, 2022",
+      viz_config_df = viz_config_df,
+      viz_config_pal = viz_config_pal$pal_positive
     ),
     format = "file"
   ),
+  # 
+  # tar_target(
+  #   p3_tot_black_png,
+  #   plot_census_map(
+  #     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",
+  #     leg_title = "Total Black, 2022",
+  #     viz_config_df = viz_config_df,
+  #     viz_config_pal = viz_config_pal$pal_positive
+  #   ),
+  #   format = "file"
+  # ),
+  # tar_target(
+  #   p3_perc_black_png,
+  #   plot_census_map(
+  #     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",
+  #     leg_title = "Percent Black, 2022",
+  #     viz_config_df = viz_config_df,
+  #     viz_config_pal = viz_config_pal$pal_positive
+  #   ),
+  #   format = "file"
+  # ),
   
   tar_target(
     p3_tot_latino_png,
@@ -53,7 +68,8 @@ p3_targets <- list(
       conus_sf = p1_conus_sf, 
       outfile_path = "3_visualize/out/tot_latino_census_2022.png",
       leg_title =  "Total Latino, 2022",
-      viz_config_df = viz_config_df
+      viz_config_df = viz_config_df,
+      viz_config_pal = viz_config_pal$pal_positive
     ),
     format = "file"
   ),
@@ -67,131 +83,162 @@ p3_targets <- list(
       conus_sf = p1_conus_sf, 
       outfile_path = "3_visualize/out/perc_latino_census_2022.png",
       leg_title = "Percent Latino, 2022",
-      viz_config_df = viz_config_df
+      viz_config_df = viz_config_df,
+      viz_config_pal = viz_config_pal$pal_positive
     ),
     format = "file"
   ),
   
-  tar_target(
-    p3_tot_male_png,
-    plot_census_map(
-      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",
-      leg_title =  "Total Male, 2022",
-      viz_config_df = viz_config_df
-    ),
-    format = "file"
-  ),
+  # tar_target(
+  #   p3_tot_male_png,
+  #   plot_census_map(
+  #     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",
+  #     leg_title =  "Total Male, 2022",
+  #     viz_config_df = viz_config_df
+  #   ),
+  #   format = "file"
+  # ),
+  # 
+  # tar_target(
+  #   p3_tot_female_png,
+  #   plot_census_map(
+  #     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",
+  #     leg_title =  "Total Female, 2022",
+  #     viz_config_df = viz_config_df,
+  #     viz_config_pal = viz_config_pal$pal_positive
+  #   ),
+  #   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",
+  #     leg_title =  "Percent of Total Poulation Under 18, 2022",
+  #     viz_config_df = viz_config_df
+  #   ),
+  #   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",
+  #     leg_title =  "Percent of Total Poulation 18-24 years, 2022",
+  #     viz_config_df = viz_config_df
+  #   ),
+  #   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",
+  #     leg_title =  "Percent of Total Poulation 15-44 years, 2022",
+  #     viz_config_df = viz_config_df
+  #   ),
+  #   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",
+  #     leg_title =  "Percent of Total Poulation 60 years & plus, 2022",
+  #     viz_config_df = viz_config_df
+  #   ),
+  #   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",
+  #     leg_title =  "Total population 18-24 years\nhigh school graduate (includes equivalency), 2022",
+  #     viz_config_df = viz_config_df
+  #   ),
+  #   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",
+  #     leg_title =  "Total population 25 and older\nhigh school graduate (includes equivalency), 2022",
+  #     viz_config_df = viz_config_df
+  #   ),
+  #   format = "file"
+  # ),
+  # Household related maps
   tar_target(
-    p3_tot_female_png,
+    p3_total_households_png,
     plot_census_map(
-      census_data = p2_perc_census_acs5_layers_sf[[6]],
+      census_data = p2_census_acs5sub_household_data[[1]],
       percent_leg = FALSE,
-      var = 'estimate', 
-      conus_sf = p1_conus_sf, 
-      outfile_path = "3_visualize/out/tot_female_census_2022.png",
-      leg_title =  "Total Female, 2022",
-      viz_config_df = viz_config_df
-    ),
-    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",
-      leg_title =  "Percent of Total Poulation Under 18, 2022",
-      viz_config_df = viz_config_df
-    ),
-    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",
-      leg_title =  "Percent of Total Poulation 18-24 years, 2022",
-      viz_config_df = viz_config_df
-    ),
-    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",
-      leg_title =  "Percent of Total Poulation 15-44 years, 2022",
-      viz_config_df = viz_config_df
-    ),
-    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",
-      leg_title =  "Percent of Total Poulation 60 years & plus, 2022",
-      viz_config_df = viz_config_df
-    ),
-    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",
-      leg_title =  "Percent allocated household income in the past 12 months, 2022",
-      viz_config_df = viz_config_df
+      var = 'estimate',
+      conus_sf = p1_conus_sf,
+      outfile_path = "3_visualize/out/tot_households_2022.png",
+      leg_title = "Total households, 2022",
+      viz_config_df = viz_config_df,
+      viz_config_pal = viz_config_pal$pal_positive
     ),
     format = "file"
   ),
-  # education related map
   tar_target(
-    p3_tot_18_24_highschool_education_map_png,
+    p3_female_households_png,
     plot_census_map(
-      census_data = p2_census_acs5sub_education_data[[1]],
+      census_data = p2_census_acs5sub_household_data[[2]],
       percent_leg = FALSE,
-      var = 'estimate', 
-      conus_sf = p1_conus_sf, 
-      outfile_path = "3_visualize/out/tot_education_18-24_highschool_2022.png",
-      leg_title =  "Total population 18-24 years\nhigh school graduate (includes equivalency), 2022",
-      viz_config_df = viz_config_df
+      var = 'estimate',
+      conus_sf = p1_conus_sf,
+      outfile_path = "3_visualize/out/tot_female_households_2022.png",
+      leg_title = "Total female households, 2022",
+      viz_config_df = viz_config_df,
+      viz_config_pal = viz_config_pal$pal_positive
     ),
     format = "file"
   ),
   tar_target(
-    p3_tot_25_plus_highschool_education_map_png,
+    p3_median_rent_png,
     plot_census_map(
-      census_data = p2_census_acs5sub_education_data[[2]],
+      census_data = p2_census_acs5sub_household_data[[3]],
       percent_leg = FALSE,
-      var = 'estimate', 
-      conus_sf = p1_conus_sf, 
-      outfile_path = "3_visualize/out/tot_education_25-plus_highschool_2022.png",
-      leg_title =  "Total population 25 and older\nhigh school graduate (includes equivalency), 2022",
-      viz_config_df = viz_config_df
+      var = 'estimate',
+      conus_sf = p1_conus_sf,
+      outfile_path = "3_visualize/out/median_rent_2022.png",
+      leg_title = "Median gross rent, 2022",
+      viz_config_df = viz_config_df,
+      viz_config_pal = viz_config_pal$pal_neg
     ),
     format = "file"
   )
diff --git a/3_visualize/src/plot_utils.R b/3_visualize/src/plot_utils.R
index 301efbf..3a41230 100644
--- a/3_visualize/src/plot_utils.R
+++ b/3_visualize/src/plot_utils.R
@@ -4,9 +4,11 @@
 #' @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` with palette, width, height, counties outline color, conus outline color, background color, font nam, and font size 
+#' @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 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){
+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)
@@ -37,7 +39,7 @@ plot_census_map <- function(census_data, conus_sf, leg_title, outfile_path, font
   if (percent_leg == FALSE) {
   census_map <- census_map +     
     scale_fill_distiller(
-    palette = viz_config_df$pal, 
+    palette = viz_config_pal, 
     direction = 1,
     name = leg_title,
     limits = c(0, max(census_data[[var]], na.rm = TRUE)),
@@ -46,7 +48,7 @@ plot_census_map <- function(census_data, conus_sf, leg_title, outfile_path, font
  } else {
    census_map <- census_map +     
      scale_fill_distiller(
-       palette = viz_config_df$pal, 
+       palette = viz_config_pal, 
        direction = 1,
        name = leg_title,
        limits = c(0, 100),
-- 
GitLab