diff --git a/R/cropr_formatting.R b/R/cropr_formatting.R index 3ac9493..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 = c("Principal", "Associated") - ) + if (!is.factor(sim$Dominance)) { + sim$Dominance <- factor( + sim$Dominance, + levels = unique(sim$Dominance) + ) + } } # Check variable names: @@ -173,12 +175,14 @@ 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 = c("Principal", "Associated") - ) - 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 = 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