From 7776214eed33f7208e94f7fe58df153692152838 Mon Sep 17 00:00:00 2001
From: Kaysa Vaarre-Lamoureux <kvaarre-lamoureux@usgs.gov>
Date: Wed, 12 Feb 2025 16:08:23 -0500
Subject: [PATCH] add file names and palettes to viz_config target

---
 findex/_targets.R       | 95 +++++++++++++++++++++++++++--------------
 findex/src/plot_utils.R | 89 +++++++++++---------------------------
 2 files changed, 89 insertions(+), 95 deletions(-)

diff --git a/findex/_targets.R b/findex/_targets.R
index 81e686d..f39c355 100644
--- a/findex/_targets.R
+++ b/findex/_targets.R
@@ -129,44 +129,78 @@ p2 <- list(
       filter(MajorCat == "Climate and weather") |> 
       pull(ThreatCategory) |>
       unique()
+  ),
+  tar_target(p2_viz_config,
+             {
+               p2_mean_weighted_subThreats |>
+                 select(MajorCat, ThreatCategory) |>
+                 mutate(
+                   # map color ramp
+                   pal = case_when(
+                     MajorCat == "Habitat" ~ list(c("#7A562B", "#C7985F", "#E1C8AA")), 
+                     MajorCat == "Exploitation" ~ list(c("#B74F49", "#E2B8B6")), 
+                     MajorCat == "Invasive species" ~ list(c("#4E6D6E", "#C9D8D9")), 
+                     MajorCat == "Pollution" ~ list(c("#002D5E", "#B2C0CE")), 
+                     MajorCat == "Climate and weather" ~ list(c("#835192", "#DDCCE2"))
+                   ),
+                   # legend and map file name template strings
+                   threat_legend_raw = "out/%s_legend_raw.png",
+                   threat_legend = "../src/assets/images/%s_legend.png",
+                   threat_map = "../src/assets/images/%s_map.png",
+                   subThreat_legend_raw = case_when(
+                     MajorCat %in% "Habitat" ~ "out/H_%s_legend_raw.png",
+                     MajorCat %in% "Exploitation" ~ "out/E_%s_legend_raw.png",
+                     MajorCat %in% "Invasive species" ~ "out/IS_%s_legend_raw.png",
+                     MajorCat %in% "Pollution" ~ "out/P_%s_legend_raw.png",
+                     MajorCat %in% "Climate and weather" ~ "out/CW_%s_legend_raw.png"
+                   ),
+                   subThreat_legend = case_when(
+                     MajorCat %in% "Habitat" ~ "../src/assets/images/H_%s_legend.png",
+                     MajorCat %in% "Exploitation" ~ "../src/assets/images/E_%s_legend.png",
+                     MajorCat %in% "Invasive species" ~ "../src/assets/images/IS_%s_legend.png",
+                     MajorCat %in% "Pollution" ~ "../src/assets/images/P_%s_legend.png",
+                     MajorCat %in% "Climate and weather" ~ "../src/assets/images/CW_%s_legend.png"
+                   ),
+                   subThreat_map = case_when(
+                     MajorCat %in% "Habitat" ~ "../src/assets/images/H_%s_map.png",
+                     MajorCat %in% "Exploitation" ~ "../src/assets/images/E_%s_map.png",
+                     MajorCat %in% "Invasive species" ~ "../src/assets/images/IS_%s_map.png",
+                     MajorCat %in% "Pollution" ~ "../src/assets/images/P_%s_map.png",
+                     MajorCat %in% "Climate and weather" ~ "../src/assets/images/CW_%s_map.png"
+                   )
+                 )
+             }
   )
 )
 
 p3 <- list(
-  tar_target(p3_color_pal,
-             tibble(
-               Habitat_pal = list(c("#7A562B", "#C7985F", "#E1C8AA")), #A07138
-               Pollution_pal = list(c("#002D5E", "#B2C0CE")),
-               Exploitation_pal = list(c("#B74F49", "#E2B8B6")),
-               Invasive_pal = list(c("#4E6D6E", "#C9D8D9")),
-               Climate_pal = list(c("#835192", "#DDCCE2")) #9D6AAC
-             )),
     tar_target( 
       p3_threat_map_png,
       {
         final_plot <- threat_map(in_dat = p2_mean_weighted_threats, 
                           threat_category = p2_threat_categories, 
-                          threat_pal = p3_color_pal,
+                          threat_pal = p2_viz_config,
                           hybas_habitat_types = p2_hybas_habitat_types_sf,
                           proj = p1_proj)  + 
           theme(legend.position = "none")
         
         save_map(type = "threat", plot = final_plot, 
-                    threat_category = p2_threat_categories, 
-                    subcat_habitat = NA, 
-                    subcat_pollution = NA, 
-                    subcat_climate = NA 
-                 )
+                 threat_category = p2_threat_categories, 
+                 subcat_habitat = NA, 
+                 subcat_pollution = NA, 
+                 subcat_climate = NA,
+                 config_df = p2_viz_config
+        )
       },
       format = "file",
       pattern = p2_threat_categories
     ),
-    tar_target( # will turn this into a function
+    tar_target(
       p3_legend_png,
       {
         final_plot <- threat_map(in_dat = p2_mean_weighted_threats, 
                                  threat_category = p2_threat_categories, 
-                                 threat_pal = p3_color_pal,
+                                 threat_pal = p2_viz_config,
                                  proj = p1_proj,
                                  hybas_habitat_types = p2_hybas_habitat_types_sf)
         
@@ -175,7 +209,8 @@ p3 <- list(
                     subcat_habitat = NA, 
                     subcat_pollution = NA, 
                     subcat_climate = NA, 
-                    in_dat = p2_mean_weighted_threats)
+                    in_dat = p2_mean_weighted_threats,
+                    config_df = p2_viz_config)
       },
       pattern = p2_threat_categories
     ),
@@ -184,32 +219,27 @@ p3 <- list(
       {
         final_plot <- subThreat_map(in_dat = p2_mean_weighted_subThreats, 
                       threat_category = p2_threat_subcategories, 
-                      subcat_habitat = p2_habitat_subthreats,
-                      subcat_pollution = p2_pollution_subthreats,
-                      subcat_climate = p2_climate_subthreats,
-                      threat_pal = p3_color_pal,
+                      threat_pal = p2_viz_config,
                       proj = p1_proj,
                       hybas_habitat_types = p2_hybas_habitat_types_sf) + 
           theme(legend.position = "none")
         
-        save_map(type = "threat", plot = final_plot, 
-                    threat_category = p2_threat_subcategories, 
-                    subcat_habitat = p2_habitat_subthreats,
-                    subcat_pollution = p2_pollution_subthreats,
-                    subcat_climate = p2_climate_subthreats)
+        save_map(type = "subThreat", plot = final_plot, 
+                 threat_category = p2_threat_subcategories, 
+                 subcat_habitat = p2_habitat_subthreats,
+                 subcat_pollution = p2_pollution_subthreats,
+                 subcat_climate = p2_climate_subthreats,
+                 config_df = p2_viz_config)
       },
       format = "file",
       pattern = p2_threat_subcategories
     ),
-    tar_target( # will turn this into a function
+    tar_target( 
       p3_sub_threat_legend_png,
       {
         final_plot <- subThreat_map(in_dat = p2_mean_weighted_subThreats, 
                                     threat_category = p2_threat_subcategories, 
-                                    subcat_habitat = p2_habitat_subthreats,
-                                    subcat_pollution = p2_pollution_subthreats,
-                                    subcat_climate = p2_climate_subthreats,
-                                    threat_pal = p3_color_pal,
+                                    threat_pal = p2_viz_config,
                                     proj = p1_proj,
                                     hybas_habitat_types = p2_hybas_habitat_types_sf)
         
@@ -218,7 +248,8 @@ p3 <- list(
                     subcat_habitat = p2_habitat_subthreats,
                     subcat_pollution = p2_pollution_subthreats,
                     subcat_climate = p2_climate_subthreats,
-                    in_dat = p2_mean_weighted_subThreats)
+                    in_dat = p2_mean_weighted_subThreats,
+                    config_df = p2_viz_config)
       },
       format = "file",
       pattern = p2_threat_subcategories
diff --git a/findex/src/plot_utils.R b/findex/src/plot_utils.R
index 187a58e..c0944a1 100644
--- a/findex/src/plot_utils.R
+++ b/findex/src/plot_utils.R
@@ -42,17 +42,10 @@ general_threat_map <- function(in_dat, threat_category, threat_pal, hybas_habita
 
 threat_map <- function(in_dat, threat_category, threat_pal, hybas_habitat_types, proj){
   
-  if(threat_category == "Habitat"){
-    pal <- threat_pal$Habitat_pal
-  } else if(threat_category == "Pollution"){
-    pal <- threat_pal$Pollution_pal
-  } else if(threat_category == "Exploitation"){
-    pal <- threat_pal$Exploitation_pal
-  } else if(threat_category == "Invasive species"){
-    pal <- threat_pal$Invasive_pal
-  } else if(threat_category == "Climate and weather"){
-    pal <- threat_pal$Climate_pal
-  }
+    pal <- threat_pal |> 
+      filter(MajorCat == threat_category) |> 
+      select(pal) |> 
+      unique()
   
   final_plot <- general_threat_map(in_dat = in_dat, 
                                    threat_category = threat_category, 
@@ -62,20 +55,12 @@ threat_map <- function(in_dat, threat_category, threat_pal, hybas_habitat_types,
 
 }
 
-subThreat_map <- function(in_dat, threat_category, threat_pal, subcat_habitat, subcat_pollution, subcat_climate, proj, hybas_habitat_types){
-  
-  # establish palette
-  if(threat_category %in% subcat_habitat){
-    pal <- threat_pal$Habitat_pal
-  } else if(threat_category %in% subcat_pollution){
-    pal <- threat_pal$Pollution_pal
-  } else if(threat_category == "Overfishing"){
-    pal <- threat_pal$Exploitation_pal
-  } else if(threat_category == "Invasive non-native species"){
-    pal <- threat_pal$Invasive_pal
-  } else if(threat_category %in% subcat_climate){
-    pal <- threat_pal$Climate_pal
-  }
+subThreat_map <- function(in_dat, threat_category, threat_pal, proj, hybas_habitat_types){ 
+  
+  pal <- threat_pal |> 
+    filter(ThreatCategory == threat_category) |> 
+    select(pal) |> 
+    unique()
   
   final_plot <- general_threat_map(in_dat = in_dat, 
                                    threat_category = threat_category, 
@@ -145,55 +130,39 @@ cowplot_legend <- function(in_dat, legend_png, threat_category, out_file){
 }
 
 # plot = final_plot, in_dat = p2_mean_weighted_threats
-save_legend <- function(type, plot, threat_category, subcat_habitat, subcat_pollution, subcat_climate, in_dat){
+save_legend <- function(type, plot, threat_category, subcat_habitat, subcat_pollution, subcat_climate, in_dat, config_df){
   
   if(type == "threat"){
+    name_conv <- config_df |> 
+      filter(MajorCat == threat_category)
     
     plot_legend <- get_plot_component(plot, "guide-box-right", return_all = T)
     
-    out_file <- paste0("out/", str_replace_all(threat_category, " ", "_"), "_legend_raw.png")
+    out_file <- sprintf(unique(name_conv$threat_legend_raw), str_replace_all(threat_category, " ", "_"))
     
     ggsave(out_file, 
            plot_legend, dpi = 300, bg = "transparent")
     knitr::plot_crop(out_file)
     
-    out_file_final <- paste0("../src/assets/images/", str_replace_all(threat_category, " ", "_"), "_legend.png")
+    out_file_final <- sprintf(unique(name_conv$threat_legend), str_replace_all(threat_category, " ", "_"))
     
     cowplot_legend(in_dat = in_dat, legend_png = out_file, threat_category = threat_category, out_file = out_file_final)
     
     return(out_file_final)
     
   } else if(type == "subThreat"){
+    name_conv <- config_df |> 
+      filter(ThreatCategory == threat_category)
     
     plot_legend <- get_plot_component(plot, "guide-box-right", return_all = T)
     
-    if(threat_category %in% subcat_habitat){
-      out_file <- paste0("out/H_", str_replace_all(threat_category, " ", "_"), "_legend_raw.png")
-    } else if(threat_category %in% subcat_pollution){
-      out_file <- paste0("out/P_", str_replace_all(threat_category, " ", "_"), "_legend_raw.png")
-    } else if(threat_category == "Overfishing"){
-      out_file <- paste0("out/E_", str_replace_all(threat_category, " ", "_"), "_legend_raw.png")
-    } else if(threat_category == "Invasive non-native species"){
-      out_file <- paste0("out/IS_", str_replace_all(threat_category, " ", "_"), "_legend_raw.png")
-    } else if(threat_category %in% subcat_climate){
-      out_file <- paste0("out/CW_", str_replace_all(threat_category, " ", "_"), "_legend_raw.png")
-    }
+    out_file <- sprintf(unique(name_conv$subThreat_legend_raw), str_replace_all(threat_category, " ", "_"))
     
     ggsave(out_file, 
            plot_legend, dpi = 300, bg = "transparent")
     knitr::plot_crop(out_file)
     
-    if(threat_category %in% subcat_habitat){
-      out_file_final <- paste0("../src/assets/images/H_", str_replace_all(threat_category, " ", "_"), "_legend.png")
-    } else if(threat_category %in% subcat_pollution){
-      out_file_final <- paste0("../src/assets/images/P_", str_replace_all(threat_category, " ", "_"), "_legend.png")
-    } else if(threat_category == "Overfishing"){
-      out_file_final <- paste0("../src/assets/images/E_", str_replace_all(threat_category, " ", "_"), "_legend.png")
-    } else if(threat_category == "Invasive non-native species"){
-      out_file_final <- paste0("../src/assets/images/IS_", str_replace_all(threat_category, " ", "_"), "_legend.png")
-    } else if(threat_category %in% subcat_climate){
-      out_file_final <- paste0("../src/assets/images/CW_", str_replace_all(threat_category, " ", "_"), "_legend.png")
-    }
+    out_file_final <- sprintf(unique(name_conv$subThreat_legend), str_replace_all(threat_category, " ", "_"))
     
     cowplot_legend(in_dat = in_dat, legend_png = out_file, threat_category = threat_category, out_file = out_file_final)
     
@@ -202,28 +171,22 @@ save_legend <- function(type, plot, threat_category, subcat_habitat, subcat_poll
   }
 }
 
-save_map <- function(type, plot, threat_category, subcat_habitat, subcat_pollution, subcat_climate){
+save_map <- function(type, plot, threat_category, subcat_habitat, subcat_pollution, subcat_climate, config_df){
   
   if(type == "threat"){
+    name_conv <- config_df |> 
+      filter(MajorCat == threat_category)
     
-    out_file <-  paste0("../src/assets/images/", str_replace_all(threat_category, " ", "_"), "_map.png")
+    out_file <-  sprintf(unique(name_conv$threat_map), str_replace_all(threat_category, " ", "_"))
     
     ggsave(out_file, 
            plot, height = 6, width = 10, dpi = 300)
     
   } else if(type == "subThreat"){
+    name_conv <- config_df |> 
+      filter(ThreatCategory == threat_category)
     
-    if(threat_category %in% subcat_habitat){
-      out_file <- paste0("../src/assets/images/H_", str_replace_all(threat_category, " ", "_"), "_map.png")
-    } else if(threat_category %in% subcat_pollution){
-      out_file <- paste0("../src/assets/images/P_", str_replace_all(threat_category, " ", "_"), "_map.png")
-    } else if(threat_category == "Overfishing"){
-      out_file <- paste0("../src/assets/images/E_", str_replace_all(threat_category, " ", "_"), "_map.png")
-    } else if(threat_category == "Invasive non-native species"){
-      out_file <- paste0("../src/assets/images/IS_", str_replace_all(threat_category, " ", "_"), "_map.png")
-    } else if(threat_category %in% subcat_climate){
-      out_file <- paste0("../src/assets/images/CW_", str_replace_all(threat_category, " ", "_"), "_map.png")
-    }
+    out_file <- sprintf(unique(name_conv$subThreat_map), str_replace_all(threat_category, " ", "_"))
     
     ggsave(out_file, 
            plot, height = 6, width = 10, dpi = 300)
-- 
GitLab