[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add species categories where it's missing #115

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", {
)
)
)

})