From 2f0a4007da0609d52a97a6e8852a3857032ec83f Mon Sep 17 00:00:00 2001 From: Kaysa Vaarre-Lamoureux <kvaarre-lamoureux@usgs.gov> Date: Wed, 22 Jan 2025 15:00:46 -0500 Subject: [PATCH] separate map and legend --- findex/_targets.R | 42 ++++++++++++++++++++++++++++++++--------- findex/src/plot_utils.R | 20 ++++++++++++-------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/findex/_targets.R b/findex/_targets.R index 2953981..328396a 100644 --- a/findex/_targets.R +++ b/findex/_targets.R @@ -4,7 +4,8 @@ library(tarchetypes) options(tidyverse.quiet = TRUE) tar_option_set(packages = c("tidyverse", "sf", - "readxl")) + "readxl", + "cowplot")) source("src/data_utils.R") source("src/plot_utils.R") @@ -96,14 +97,37 @@ p3 <- list( values = tibble::tibble(threat_cat = c("Habitat", "Exploitation", "Invasive species", "Pollution", "Climate and weather")), - tar_target( - p3_threat_map, - threat_map(in_dat = p2_mean_weighted_threats, - threat_category = threat_cat, - threat_pal = p3_color_pal, - out_file = paste0("out/", str_replace_all(threat_cat, " ", "_"), "_map.png")), - format = "file" - )) + tar_target( + # change all out file paths to the earth in flux directroy: src/assets/images + p3_threat_map, + threat_map(in_dat = p2_mean_weighted_threats, + threat_category = threat_cat, + threat_pal = p3_color_pal + ) + ), + tar_target( + p3_threat_map_png, + { + final_plot <- p3_threat_map + + theme(legend.position = "none") + + ggsave(paste0("out/", str_replace_all(threat_cat, " ", "_"), "_map.png"), + final_plot, height = 6, width = 10, dpi = 300) + }, + format = "file" + ), + tar_target( + p3_legend_png, + { + plot_legend <- get_plot_component(p3_threat_map, "guide-box-right", return_all = T) + + out_file <- paste0("out/", str_replace_all(threat_cat, " ", "_"), "_legend.png") + + ggsave(out_file, + plot_legend, dpi = 300, bg = "transparent") + knitr::plot_crop(out_file) + } + )) ) c(p1, p2, p3) diff --git a/findex/src/plot_utils.R b/findex/src/plot_utils.R index b8a41e8..e0e9f04 100644 --- a/findex/src/plot_utils.R +++ b/findex/src/plot_utils.R @@ -1,7 +1,11 @@ -threat_map <- function(in_dat, threat_category, threat_pal, out_file){ +threat_map <- function(in_dat, threat_category, threat_pal){ filtered_df <- st_as_sf(in_dat) |> - dplyr::filter(ThreatCategory == threat_category) + dplyr::filter(ThreatCategory == threat_category) |> + # remove visual bug with robinson reprojection + st_wrap_dateline() + +proj_df <- st_transform(filtered_df, crs = st_crs("ESRI:54030")) if(threat_category == "Habitat"){ pal <- threat_pal$Habitat_pal @@ -16,14 +20,14 @@ if(threat_category == "Habitat"){ } threat_map <- ggplot()+ - geom_sf(data = filtered_df, aes(geometry = Shape, fill = MeanWeightedThreatMetric, color = MeanWeightedThreatMetric))+ + geom_sf(data = proj_df, aes(geometry = Shape, fill = MeanWeightedThreatMetric, color = MeanWeightedThreatMetric))+ scale_fill_gradientn( colors = colorRampPalette(c(rev(unlist(pal))))(100), - limits = c(0, max(filtered_df$MeanWeightedThreatMetric, na.rm = T)), + limits = c(0, max(proj_df$MeanWeightedThreatMetric, na.rm = T)), na.value = "gray80", - breaks = c(0 + max(filtered_df$MeanWeightedThreatMetric, na.rm = T)/10, + breaks = c(0 + max(proj_df$MeanWeightedThreatMetric, na.rm = T)/10, #max(habitat_data$MeanWeightedThreatMetric)/2, - max(filtered_df$MeanWeightedThreatMetric, na.rm = T) - max(filtered_df$MeanWeightedThreatMetric, na.rm = T)/10), + max(proj_df$MeanWeightedThreatMetric, na.rm = T) - max(proj_df$MeanWeightedThreatMetric, na.rm = T)/10), labels = c("Lower", "Higher") )+ scale_color_gradientn( @@ -38,10 +42,10 @@ threat_map <- ggplot()+ barheight = 1))+ theme_void()+ theme( - legend.position = c(0.1, 0.21), + #legend.position = c(0.1, 0.21), legend.ticks = element_blank() ) -ggsave(out_file, height = 6, width = 10, dpi = 300) +return(threat_map) } \ No newline at end of file -- GitLab