Skip to content

Commit

Permalink
Merge branch 'add_species_categories' of https://github.com/FRBCesab/…
Browse files Browse the repository at this point in the history
…funbiogeo into add_species_categories
  • Loading branch information
Matthias Grenié committed Jul 25, 2024
2 parents c328d89 + 450411f commit 8c32909
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
50 changes: 46 additions & 4 deletions R/fb_plot_number_sites_by_species.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,49 @@
#' fb_plot_number_sites_by_species(site_species)
#'
#' # Add a vertical cutoff line (40% of sites)
#' fb_plot_number_sites_by_species(site_species, 0.4)
#' fb_plot_number_sites_by_species(site_species, threshold = 0.4)
fb_plot_number_sites_by_species <- function(
site_species, threshold_sites_proportion = NULL
site_species, species_categories = NULL, threshold_sites_proportion = NULL
) {

# Check ----------------------------------------------------------------------
check_site_species(site_species)
check_species_categories(species_categories)


if (!is.null(threshold_sites_proportion)) {
check_threshold_proportion(threshold_sites_proportion, "site")
}


# Splitting species by category
species_split <- list(single_cat = species_traits[["species"]])
category_name <- "single_cat"

if (!is.null(species_categories)) {

category_name <- colnames(species_categories)[2]

species_split <- split(
species_categories[, 1], species_categories[, 2]
)

}


# Split sites according to species' categories
site_species_categories <- lapply(
species_split,
function(x) site_species[, c("site", x), drop = FALSE]
)

# Get the numbers
number_sites_by_species <- fb_count_sites_by_species(site_species)
n_species <- nrow(site_species)
number_sites_by_species <- lapply(
site_species_categories, fb_count_sites_by_species
)
n_species <- lapply(site_species_categories, nrow)



# Construct y-axis breaks
# Under 25 observation, label everyone of them
Expand Down Expand Up @@ -68,6 +96,20 @@ fb_plot_number_sites_by_species <- function(
levels = rev(number_sites_by_species$species)
)

# Manage conditional faceting
if (is.null(species_categories)) {

category_facet <- NULL

} else {

category_facet <- ggplot2::facet_wrap(
ggplot2::vars(
!!rlang::sym(category_name)), scales = "free"
)

}

# Clean environment
rm(site_species)

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-fb_plot_site_traits_completeness.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ test_that("fb_plot_site_traits_completeness works", {
)
)
)

})

0 comments on commit 8c32909

Please sign in to comment.