diff --git a/findex/_targets.R b/findex/_targets.R index 9b74b444bf73ee14d142e22987ac721400d8a4c5..d52d08f1e1e472113571e6024468bb610e4dcc13 100644 --- a/findex/_targets.R +++ b/findex/_targets.R @@ -277,6 +277,19 @@ p3 <- list( knitr::plot_crop("../src/assets/images/all_threat_by_basin.png") }, format = "file" + ), + tar_target( + p3_top_threat_thumbnail, + top_threat_thumbnail(in_dat = p2_mean_weighted_threats, + threat_pal = p2_viz_config, + hybas_habitat_types = p2_hybas_habitat_types_sf, + proj = p1_proj, + threat_category = "none", + height = 6, + width = 6, + dpi = 300, + out_file = "../src/assets/images/threat_by_basin_thumbnail.png"), + format = "file" ) # tar_target( # p3_top_threat_legend_png, diff --git a/findex/src/plot_utils.R b/findex/src/plot_utils.R index 150c1aedf68bd42f37026f8a591ffb64daacb199..841ab1851aa979cc306e2ef3c7c0b4aa5bf5003d 100644 --- a/findex/src/plot_utils.R +++ b/findex/src/plot_utils.R @@ -313,3 +313,42 @@ save_top_threat_legend <- function(plot, dpi, out_file){ knitr::plot_crop(out_file) } + +top_threat_thumbnail <- function(in_dat, threat_pal, hybas_habitat_types, proj, threat_category, height, width, dpi, out_file){ + + # these coordinates are NAD83 coordinates that correspond to "EPSG:4269", so will only work with the other data if they are also in "EPSG:4269" + bbox <- st_bbox(c(xmin = -25, xmax = 60, ymin = -35, ymax = 40)) + + # get bbox of projected area of interest + if (!(proj == "EPSG:4269")) { + bbox <- sf::st_as_sfc(bbox) |> + st_as_sf(crs = "EPSG:4269") |> + sf::st_transform(crs = proj) |> + sf::st_bbox() + } + + # call custom plotting function + threat_map <- top_threat_plot(in_dat = in_dat, + threat_pal = threat_pal, + hybas_habitat_types = hybas_habitat_types, + proj = proj, + threat_category = threat_category) + + # crop for thumbnail + coord_sf(xlim = c(bbox['xmin'], bbox['xmax']), + ylim = c(bbox['ymin'], bbox['ymax']), + expand = F) + + # white background and add margin + theme(legend.position = "none", + plot.background = element_rect(fill = "white", color = NA), + panel.background = element_rect(fill = "white", color = NA), + plot.margin = margin(t = 2, + r = 2, + b = 2, + l = 2, + unit = "cm")) + + # save in square ratio + ggsave(out_file, + threat_map, height = height, width = width, dpi = dpi) + +} diff --git a/src/assets/images/threat_by_basin_thumbnail.png b/src/assets/images/threat_by_basin_thumbnail.png new file mode 100644 index 0000000000000000000000000000000000000000..67dc6675bee1cb48c3f53b8451d61aaac9d5d932 Binary files /dev/null and b/src/assets/images/threat_by_basin_thumbnail.png differ