From 6a05516df969bcdc1fda84a3871741e834413823 Mon Sep 17 00:00:00 2001 From: ehwenk Date: Thu, 14 Nov 2024 12:00:15 +1100 Subject: [PATCH] function fixes * bug fix for `extract_data` so it works even after you have joined other columns * edit `plot_trait_distribution_beeswarm` to work with a single table and for its message to accurately reflect which grouping variables are allowed --- R/extract_data.R | 4 ++- R/plot_trait_distribution_beeswarm.R | 37 ++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/R/extract_data.R b/R/extract_data.R index e202310..3093a8c 100644 --- a/R/extract_data.R +++ b/R/extract_data.R @@ -168,9 +168,11 @@ extract_data <- function(database, table = NA, col, col_value) { ret_tmp[["traits"]] <- database[["traits"]]%>% dplyr::semi_join(cc_traits, by = columns_to_select) + columns_to_select_excluded <- intersect(setdiff(names(database$excluded_data), "value"), names(database[[table[[i]]]])) + # Use same filtering join to trim excluded data ret_tmp[["excluded_data"]] <- database[["excluded_data"]] %>% - dplyr::semi_join(cc_traits, by = columns_to_select) + dplyr::semi_join(cc_traits, by = columns_to_select_excluded) for (j in seq_along(tables_tmp$tables_to_cut)) { diff --git a/R/plot_trait_distribution_beeswarm.R b/R/plot_trait_distribution_beeswarm.R index 7c5eea6..4e5c75e 100644 --- a/R/plot_trait_distribution_beeswarm.R +++ b/R/plot_trait_distribution_beeswarm.R @@ -24,8 +24,8 @@ plot_trait_distribution_beeswarm <- function(database, hide_ids = FALSE) { # Check compatability - status <- check_compatibility(database) - + status <- check_compatibility(database, single_table_allowed = TRUE) + # If compatible if(!status) { function_not_supported(database) @@ -44,17 +44,27 @@ plot_trait_distribution_beeswarm <- function(database, factor(p, levels=names(my_shapes)) } - tax_info <- database_trait$taxa %>% dplyr::select(taxon_name, family) + if (is.null(dim(database_trait))) { + + tax_info <- database_trait$taxa %>% dplyr::select(taxon_name, family, genus) + + data <- + database_trait$traits %>% + dplyr::left_join(by = "taxon_name", tax_info) + + } else { + + data <- database_trait - data <- - database_trait$traits %>% + } + + data <- data %>% dplyr::mutate(shapes = as_shape(value_type)) %>% - dplyr::left_join(by = "taxon_name", tax_info) %>% dplyr::mutate(value = as.numeric(value)) # Define grouping variables and derivatives if(!y_axis_category %in% names(data)) { - stop("Incorrect grouping variable! Currently implemented for `family` or `dataset_id`") + stop("Incorrect grouping variable! Grouping variable must be a variable in or joined to the traits table. Family and genus are supported if your input is a complete traits.build database.") } # define grouping variable, ordered by group-level by mean values @@ -77,10 +87,17 @@ plot_trait_distribution_beeswarm <- function(database, if(!is.na(highlight) & highlight %in% data$Group) { data <- dplyr::mutate(data, colour = ifelse(Group %in% highlight, "c", colour)) } - - vals <- list(minimum = purrr::pluck(database_trait, "definitions", trait_name, "allowed_values_min"), - maximum = purrr::pluck(database_trait, "definitions", trait_name, "allowed_values_max")) + if (is.null(dim(database))) { + + vals <- list(minimum = purrr::pluck(database_trait, "definitions", trait_name, "allowed_values_min"), + maximum = purrr::pluck(database_trait, "definitions", trait_name, "allowed_values_max")) + + } else { + + vals <- list(minimum = 0.8*min(data$value), + maximum = 1.2*max(data$value)) + } range <- (vals$maximum/vals$minimum)