From 75ae4cb4a73b919ccf4bad9a822b030099eebc1e Mon Sep 17 00:00:00 2001 From: casanchez Date: Tue, 19 Dec 2023 13:49:30 -0500 Subject: [PATCH] Updates to figures based on latest data --- R/build_sdm.R | 4 +- R/get_iucn.R | 20 ++---- R/map_predicted_distributions.R | 48 ++++++++------- _targets.R | 48 ++++++--------- renv.lock | 106 +++++++++++++++++++++----------- reports/.gitkeep | 1 - 6 files changed, 122 insertions(+), 105 deletions(-) delete mode 100644 reports/.gitkeep diff --git a/R/build_sdm.R b/R/build_sdm.R index 75b7e8e..244be80 100644 --- a/R/build_sdm.R +++ b/R/build_sdm.R @@ -21,8 +21,8 @@ build_sdm <- function(species, occs, iucn_ranges, env_stack, n_bg, seed = 42){ # subset all occurrence points to those of the focal species occs_species <- occs %>% filter(name == species) - occs_sf <- st_as_sf(occs_species, coords = c("longitude", "latitude"), - crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0") + occs_sf <- sf::st_as_sf(occs_species, coords = c("longitude", "latitude"), + crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0") occs_xy <- occs_species[, c("longitude", "latitude")] # build convex hull around focal species points diff --git a/R/get_iucn.R b/R/get_iucn.R index 55d6891..176c9d3 100644 --- a/R/get_iucn.R +++ b/R/get_iucn.R @@ -3,7 +3,7 @@ #' #' @title get_iucn #' @param IUCN_data mammal distribution data from IUCN -#' @param species character, species of interest +#' @param species character, names of species of interest #' @return #' @author Cecilia Sanchez #' @example @@ -13,20 +13,10 @@ get_iucn <- function(IUCN_data, species){ mammals <- terra::vect(IUCN_data) - # tedious but only way I could get this to work - iucn_ranges <- subset(mammals, mammals[[2]] == species[1] | - mammals[[2]] == species[2] | - mammals[[2]] == species[3] | - mammals[[2]] == species[4] | - mammals[[2]] == species[5] | - mammals[[2]] == species[6] | - mammals[[2]] == species[7] | - mammals[[2]] == species[8] | - mammals[[2]] == species[9] | - mammals[[2]] == species[10] | - mammals[[2]] == species[11] | - mammals[[2]] == species[12] - ) + iucn_ranges <- terra::subset(mammals, mammals$sci_name %in% species) + + # free memory + gc() return(iucn_ranges) diff --git a/R/map_predicted_distributions.R b/R/map_predicted_distributions.R index d5765fd..2fee878 100644 --- a/R/map_predicted_distributions.R +++ b/R/map_predicted_distributions.R @@ -23,7 +23,7 @@ map_predicted_distributions <- function(){ # subset the full countries map to just WA countries WA_countries <- countries[countries$GID_0 %in% WA_codes] # make an sf object for later plotting - WA_countries_sf <- st_as_sf(WA_countries) + WA_countries_sf <- sf::st_as_sf(WA_countries) # get list of all generated predictions rasters bat_ras_names <- list.files(path = "outputs/", pattern = "full", @@ -41,16 +41,17 @@ map_predicted_distributions <- function(){ # crop/mask to WA countries bat_ras_cropped <- terra::crop(bat_ras, WA_countries, mask = T) # align CRS - crs(bat_ras_cropped) <- crs(WA_countries) + terra::crs(bat_ras_cropped) <- terra::crs(WA_countries) # plot using tidyterra functionality maps_list[[i]] <- ggplot() + - geom_spatvector(data = WA_countries, fill = "gray95") + - geom_spatraster(data = bat_ras_cropped) + + tidyterra::geom_spatvector(data = WA_countries, fill = "gray95") + + tidyterra::geom_spatraster(data = bat_ras_cropped) + scale_fill_viridis_c(option = "D", na.value = "transparent", name = "Predicted probability \nof species presence", breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1)) + - geom_spatvector(data = WA_countries, fill = "transparent", lwd = 0.5) + + tidyterra::geom_spatvector(data = WA_countries, fill = "transparent", + lwd = 0.5) + labs(x = "Longitude", y = "Latitude") + ggtitle(species_name) + theme_bw() + @@ -58,27 +59,30 @@ map_predicted_distributions <- function(){ } # create initial grid with no legends - plot_grid(maps_list[[1]] + theme(legend.position = "none"), - maps_list[[2]] + theme(legend.position = "none"), - maps_list[[3]] + theme(legend.position = "none"), - maps_list[[4]] + theme(legend.position = "none"), - maps_list[[5]] + theme(legend.position = "none"), - maps_list[[6]] + theme(legend.position = "none"), - maps_list[[7]] + theme(legend.position = "none"), - maps_list[[8]] + theme(legend.position = "none"), - maps_list[[9]] + theme(legend.position = "none"), - maps_list[[10]] + theme(legend.position = "none"), - maps_list[[11]] + theme(legend.position = "none"), - maps_list[[12]] + theme(legend.position = "none"), - ncol = 3) -> my_grid + cowplot::plot_grid(maps_list[[1]] + theme(legend.position = "none"), + maps_list[[2]] + theme(legend.position = "none"), + maps_list[[3]] + theme(legend.position = "none"), + maps_list[[4]] + theme(legend.position = "none"), + maps_list[[5]] + theme(legend.position = "none"), + maps_list[[6]] + theme(legend.position = "none"), + maps_list[[7]] + theme(legend.position = "none"), + maps_list[[8]] + theme(legend.position = "none"), + maps_list[[9]] + theme(legend.position = "none"), + maps_list[[10]] + theme(legend.position = "none"), + maps_list[[11]] + theme(legend.position = "none"), + maps_list[[12]] + theme(legend.position = "none"), + maps_list[[13]] + theme(legend.position = "none"), + maps_list[[14]] + theme(legend.position = "none"), + maps_list[[15]] + theme(legend.position = "none"), + ncol = 3) -> my_grid # make one legend to go at the bottom - legend_below <- get_legend(maps_list[[1]] + - guides(color = guide_legend(nrow = 1)) + - theme(legend.position = "bottom")) + legend_below <- cowplot::get_legend(maps_list[[1]] + + guides(color = guide_legend(nrow = 1)) + + theme(legend.position = "bottom")) # add initial grid plus the single legend - plot_grid(my_grid, legend_below, ncol = 1, rel_heights = c(1, 0.1)) + cowplot::plot_grid(my_grid, legend_below, ncol = 1, rel_heights = c(1, 0.1)) rm(list = "bat_ras", "bat_ras_cropped", "countries", "WA_countries", diff --git a/_targets.R b/_targets.R index 0946166..99e540f 100644 --- a/_targets.R +++ b/_targets.R @@ -19,7 +19,7 @@ tar_option_set( ## Data input data_input_targets <- tar_plan( - tar_file(CoV_species_file, "data/Habitat info - CoV+ Western Asia bat species - updated 29 June 2023.csv"), + tar_file(CoV_species_file, "data/Habitat info - CoV+ Western Asia bat species - updated 11 Dec 2023.csv"), tar_file(WABNet_coords_file, "data/GPS coordinates for WABNet captured species.csv"), # https://figshare.com/articles/dataset/Metadata_for_DarkCideS_1_0_a_global_database_for_bats_in_karsts_and_caves/16413405?file=34091939 tar_file(darkcides_file, "data/DarkCideS_v4_dataset 2.csv"), @@ -57,6 +57,7 @@ data_processing_targets <- tar_plan( karst_file, ntl_file)), CoV_species_names = CoV_species$Species, + # takes a couple hours occs_df = get_occs(species_names = CoV_species_names, sources = c("gbif", "vertnet"), limit = 30000), @@ -82,30 +83,35 @@ data_processing_targets <- tar_plan( ## Analysis analysis_targets <- tar_plan( - + e_Hysa = build_sdm(species = "Hypsugo savii", occs = occs_ENM, + iucn_ranges, env_stack, n_bg = 10000), e_Misc = build_sdm(species = "Miniopterus schreibersii", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Mybl = build_sdm(species = "Myotis blythii", occs = occs_ENM, iucn_ranges, + e_Mybl = build_sdm(species = "Myotis blythii", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Myem = build_sdm(species = "Myotis emarginatus", occs = occs_ENM, + e_Myem = build_sdm(species = "Myotis emarginatus", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Mymy = build_sdm(species = "Myotis myotis", occs = occs_ENM, iucn_ranges, + e_Mymy = build_sdm(species = "Myotis myotis", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Piku = build_sdm(species = "Pipistrellus kuhlii", occs = occs_ENM, + e_Pija = build_sdm(species = "Pipistrellus javanicus", occs = occs_ENM, + iucn_ranges, env_stack, n_bg = 10000), + e_Piku = build_sdm(species = "Pipistrellus kuhlii", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Rhbl = build_sdm(species = "Rhinolophus blasii", occs = occs_ENM, + e_Plma = build_sdm(species = "Plecotus macrobullaris", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Rheu = build_sdm(species = "Rhinolophus euryale", occs = occs_ENM, + e_Rhbl = build_sdm(species = "Rhinolophus blasii", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Rhfe = build_sdm(species = "Rhinolophus ferrumequinum", occs = occs_ENM, + e_Rheu = build_sdm(species = "Rhinolophus euryale", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Rhmi = build_sdm(species = "Rhinopoma microphyllum", occs = occs_ENM, + e_Rhfe = build_sdm(species = "Rhinolophus ferrumequinum", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Roae = build_sdm(species = "Rousettus aegyptiacus", occs = occs_ENM, + e_Rhmi = build_sdm(species = "Rhinopoma microphyllum", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Role = build_sdm(species = "Rousettus leschenaultii", occs = occs_ENM, + e_Roae = build_sdm(species = "Rousettus aegyptiacus", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000), - e_Sche = build_sdm(species = "Scotophilus heathii", occs = occs_ENM, + e_Role = build_sdm(species = "Rousettus leschenaultii", occs = occs_ENM, + iucn_ranges, env_stack, n_bg = 10000), + e_Sche = build_sdm(species = "Scotophilus heathii", occs = occs_ENM, iucn_ranges, env_stack, n_bg = 10000) ) @@ -116,25 +122,11 @@ outputs_targets <- tar_plan( quarterly_heatmap = plot_quarterly_heatmap(heat_data) ) - -## Report -report_targets <- tar_plan( - ## Example Rmarkdown report target/s; delete and replace with your own - ## Rmarkdown report target/s - - # tar_render( - # example_report, path = "reports/example_report.Rmd", - # output_dir = "outputs", knit_root_dir = here::here() - # ) -) - - # List targets ----------------------------------------------------------------- list( data_input_targets, data_processing_targets, analysis_targets, - outputs_targets, - report_targets + outputs_targets ) diff --git a/renv.lock b/renv.lock index d8719e5..d42f7e9 100644 --- a/renv.lock +++ b/renv.lock @@ -64,26 +64,26 @@ }, "KernSmooth": { "Package": "KernSmooth", - "Version": "2.23-20", + "Version": "2.23-22", "Source": "Repository", "Repository": "CRAN", - "Hash": "8dcfa99b14c296bc9f1fd64d52fd3ce7", + "Hash": "2fecebc3047322fa5930f74fae5de70f", "Requirements": [] }, "MASS": { "Package": "MASS", - "Version": "7.3-57", + "Version": "7.3-60", "Source": "Repository", "Repository": "CRAN", - "Hash": "71476c1d88d1ebdf31580e5a257d5d31", + "Hash": "a56a6365b3fa73293ea8d084be0d9bb0", "Requirements": [] }, "Matrix": { "Package": "Matrix", - "Version": "1.4-1", + "Version": "1.6-4", "Source": "Repository", "Repository": "CRAN", - "Hash": "699c47c606293bdfbc9fd78a93c9c8fe", + "Hash": "d9c655b30a2edc6bb2244c1d1e8d549d", "Requirements": [ "lattice" ] @@ -292,10 +292,10 @@ }, "class": { "Package": "class", - "Version": "7.3-20", + "Version": "7.3-22", "Source": "Repository", "Repository": "CRAN", - "Hash": "da09d82223e669d270e47ed24ac8686e", + "Hash": "f91f6b29f38b8c280f2b9477787d4bb2", "Requirements": [ "MASS" ] @@ -330,10 +330,10 @@ }, "codetools": { "Package": "codetools", - "Version": "0.2-18", + "Version": "0.2-19", "Source": "Repository", "Repository": "CRAN", - "Hash": "019388fc48e48b3da0d3a76ff94608a8", + "Hash": "c089a619a7fae175d149d89164f8c7d8", "Requirements": [] }, "colorspace": { @@ -458,10 +458,10 @@ }, "digest": { "Package": "digest", - "Version": "0.6.31", + "Version": "0.6.33", "Source": "Repository", - "Repository": "RSPM", - "Hash": "8b708f296afd9ae69f450f9640be8990", + "Repository": "CRAN", + "Hash": "b18a9cf3c003977b0cc49d5e76ebe48d", "Requirements": [] }, "dismo": { @@ -569,10 +569,10 @@ }, "evaluate": { "Package": "evaluate", - "Version": "0.19", + "Version": "0.23", "Source": "Repository", - "Repository": "RSPM", - "Hash": "5aac3cd0a3ccb1a738941796b28c26fe", + "Repository": "CRAN", + "Hash": "daf4a1246be12c1fa8c7705a0935c1a0", "Requirements": [] }, "fansi": { @@ -930,10 +930,10 @@ }, "htmltools": { "Package": "htmltools", - "Version": "0.5.4", + "Version": "0.5.7", "Source": "Repository", - "Repository": "RSPM", - "Hash": "9d27e99cc90bd701c0a7a63e5923f9b7", + "Repository": "CRAN", + "Hash": "2d7b3857980e0e0d0a1fd6f11928ab0f", "Requirements": [ "base64enc", "digest", @@ -1017,6 +1017,27 @@ "Hash": "8954069286b4b2b0d023d1b288dce978", "Requirements": [] }, + "janitor": { + "Package": "janitor", + "Version": "2.2.0", + "Source": "Repository", + "Repository": "CRAN", + "Hash": "5baae149f1082f466df9d1442ba7aa65", + "Requirements": [ + "dplyr", + "hms", + "lifecycle", + "lubridate", + "magrittr", + "purrr", + "rlang", + "snakecase", + "stringi", + "stringr", + "tidyr", + "tidyselect" + ] + }, "jquerylib": { "Package": "jquerylib", "Version": "0.1.4", @@ -1070,10 +1091,10 @@ }, "lattice": { "Package": "lattice", - "Version": "0.20-45", + "Version": "0.22-5", "Source": "Repository", "Repository": "CRAN", - "Hash": "b64cdbb2b340437c4ee047a1f4c4377b", + "Hash": "7c5e89f04e72d6611c77451f6331a091", "Requirements": [] }, "lazyeval": { @@ -1154,10 +1175,10 @@ }, "mgcv": { "Package": "mgcv", - "Version": "1.8-40", + "Version": "1.9-0", "Source": "Repository", "Repository": "CRAN", - "Hash": "c6b2fdb18cf68ab613bd564363e1ba0d", + "Hash": "086028ca0460d0c368028d3bda58f31b", "Requirements": [ "Matrix", "nlme" @@ -1200,10 +1221,10 @@ }, "nlme": { "Package": "nlme", - "Version": "3.1-157", + "Version": "3.1-164", "Source": "Repository", "Repository": "CRAN", - "Hash": "dbca60742be0c9eddc5205e5c7ca1f44", + "Hash": "a623a2239e642806158bc4dc3f51565d", "Requirements": [ "lattice" ] @@ -1292,10 +1313,10 @@ }, "processx": { "Package": "processx", - "Version": "3.8.0", + "Version": "3.8.3", "Source": "Repository", - "Repository": "RSPM", - "Hash": "a33ee2d9bf07564efb888ad98410da84", + "Repository": "CRAN", + "Hash": "82d48b1aec56084d9438dbf98087a7e9", "Requirements": [ "R6", "ps" @@ -1339,10 +1360,10 @@ }, "ps": { "Package": "ps", - "Version": "1.7.2", + "Version": "1.7.5", "Source": "Repository", - "Repository": "RSPM", - "Hash": "68dd03d98a5efd1eb3012436de45ba83", + "Repository": "CRAN", + "Hash": "709d852d33178db54b17c722e5b1e594", "Requirements": [] }, "purrr": { @@ -1608,10 +1629,10 @@ }, "rpart": { "Package": "rpart", - "Version": "4.1.16", + "Version": "4.1.23", "Source": "Repository", "Repository": "CRAN", - "Hash": "ea3ca1d9473daabb3cd0f1b4f974c1ed", + "Hash": "b3d390424f41d04174cccf84d49676c2", "Requirements": [] }, "rprojroot": { @@ -1769,6 +1790,17 @@ "xtable" ] }, + "snakecase": { + "Package": "snakecase", + "Version": "0.11.1", + "Source": "Repository", + "Repository": "CRAN", + "Hash": "58767e44739b76965332e8a4fe3f91f1", + "Requirements": [ + "stringi", + "stringr" + ] + }, "snow": { "Package": "snow", "Version": "0.4-4", @@ -2010,10 +2042,10 @@ }, "survival": { "Package": "survival", - "Version": "3.3-1", + "Version": "3.5-7", "Source": "Repository", "Repository": "CRAN", - "Hash": "f6189c70451d3d68e0d571235576e833", + "Hash": "b8e943d262c3da0b0febd3e04517c197", "Requirements": [ "Matrix" ] @@ -2350,10 +2382,10 @@ }, "wk": { "Package": "wk", - "Version": "0.7.3", + "Version": "0.9.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "68a7ab6ec1afb5f076172b983c069313", + "Hash": "5d4545e140e36476f35f20d0ca87963e", "Requirements": [] }, "xfun": { diff --git a/reports/.gitkeep b/reports/.gitkeep deleted file mode 100644 index 8b13789..0000000 --- a/reports/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -