From e4b64475865d885639ef0b049705b2576ed7c3a6 Mon Sep 17 00:00:00 2001 From: sbuis Date: Thu, 26 Feb 2026 11:06:47 +0100 Subject: [PATCH 1/2] feat(dominance): compute Dominance levels from input data instead of hard coded - Levels of the 'Dominance' column are now derived from the input data frames `sim` and `obs`. - If 'Dominance' is absent or contains NA values, these are replaced by the hard-coded value "Single Crop". --- R/cropr_formatting.R | 6 +++--- R/generic_formatting.R | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/R/cropr_formatting.R b/R/cropr_formatting.R index 3ac9493..9a383fd 100644 --- a/R/cropr_formatting.R +++ b/R/cropr_formatting.R @@ -104,7 +104,7 @@ format_cropr <- function(sim, obs = NULL, obs_sd = NULL, if (is_mixture_sim) { sim$Dominance <- factor( sim$Dominance, - levels = c("Principal", "Associated") + levels = unique(sim$Dominance) ) } @@ -174,11 +174,11 @@ format_cropr <- function(sim, obs = NULL, obs_sd = NULL, } } else { obs$Dominance <- factor(obs$Dominance, - levels = c("Principal", "Associated") + levels = unique(obs$Dominance) ) if (is_obs_sd) { obs_sd$Dominance <- factor(obs_sd$Dominance, - levels = c("Principal", "Associated") + levels = unique(obs_sd$Dominance) ) } } diff --git a/R/generic_formatting.R b/R/generic_formatting.R index 7b5b1ee..2f9c7c1 100644 --- a/R/generic_formatting.R +++ b/R/generic_formatting.R @@ -37,8 +37,10 @@ generic_formatting <- function( # Replace NAs with "Single-crop" in Dominance in order to make # the legend understandable if ("Dominance" %in% colnames(df)) { - levels(df$Dominance) <- c("Principal", "Associated", "Single crop") - df$Dominance[which(is.na(df$Dominance))] <- "Single crop" + if ((!"Single crop" %in% levels(df$Dominance)) & any(is.na(df$Dominance))) { + levels(df$Dominance) <- c(levels(df$Dominance), "Single crop") + df$Dominance[is.na(df$Dominance)] <- "Single crop" + } } # Add group_var column to data frame if overlap != null From 228e8533ef9a1b3fa16d02f23c38522ab91271fb Mon Sep 17 00:00:00 2001 From: sbuis Date: Thu, 26 Feb 2026 13:35:55 +0100 Subject: [PATCH 2/2] fix: preserve factor levels when converting Dominance columns Add conditional checks in `format_cropr` so `sim$Dominance`, `obs$Dominance`, and `obs_sd$Dominance` are only converted to factors if not already. This ensures existing factor levels and their order are preserved, preventing unnecessary warnings and unintended changes. --- R/cropr_formatting.R | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/R/cropr_formatting.R b/R/cropr_formatting.R index 9a383fd..aaa3025 100644 --- a/R/cropr_formatting.R +++ b/R/cropr_formatting.R @@ -102,10 +102,12 @@ format_cropr <- function(sim, obs = NULL, obs_sd = NULL, # Treating Dominance as a factor if any (for plotting reasons): if (is_mixture_sim) { - sim$Dominance <- factor( - sim$Dominance, - levels = unique(sim$Dominance) - ) + if (!is.factor(sim$Dominance)) { + sim$Dominance <- factor( + sim$Dominance, + levels = unique(sim$Dominance) + ) + } } # Check variable names: @@ -173,10 +175,12 @@ format_cropr <- function(sim, obs = NULL, obs_sd = NULL, obs_sd <- dplyr::full_join(obs_sd, corresp_table, by = "Plant") } } else { - obs$Dominance <- factor(obs$Dominance, - levels = unique(obs$Dominance) - ) - if (is_obs_sd) { + if (!is.factor(obs$Dominance)) { + obs$Dominance <- factor(obs$Dominance, + levels = unique(obs$Dominance) + ) + } + if (is_obs_sd & !is.factor(obs_sd$Dominance)) { obs_sd$Dominance <- factor(obs_sd$Dominance, levels = unique(obs_sd$Dominance) )