From aa216b2f3243dc6d277d5acb558acf7074c90c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sch=C3=BCrz?= <christoph.schuerz@ufz.de> Date: Wed, 7 Dec 2022 12:18:45 +0100 Subject: [PATCH] Generalize plot at harvest kill function --- NAMESPACE | 2 +- R/print_mgt.R | 53 +++++++++++++++++++++++++------- man/plot_phuplant_harvest.Rd | 20 ------------ man/plot_variable_at_harvkill.Rd | 25 +++++++++++++++ 4 files changed, 68 insertions(+), 32 deletions(-) delete mode 100644 man/plot_phuplant_harvest.Rd create mode 100644 man/plot_variable_at_harvkill.Rd diff --git a/NAMESPACE b/NAMESPACE index d166238..5756d7e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,7 +3,7 @@ export(get_hru_id_by_attribute) export(plot_climate_annual) export(plot_hru_pw_day) -export(plot_phuplant_harvest) +export(plot_variable_at_harvkill) export(print_triggered_mgt) export(run_swat_verification) import(ggplot2) diff --git a/R/print_mgt.R b/R/print_mgt.R index f3b4a74..51b5101 100644 --- a/R/print_mgt.R +++ b/R/print_mgt.R @@ -32,33 +32,64 @@ print_triggered_mgt <- function(sim_verify, hru_id, start_year = 1900, end_year print(., n = Inf) } -#' Boxplot of heat unit fractions for crops at harvest-kill operation +#' Boxplot for relevant variables at harvest-kill #' -#' plot_phuplant_harvest plots boxplots of the collected heat unit fractions +#' plot_variable_at_harvkill plots boxplots of one of the variables crop heat unit +#' fractions ('phu'), crop yields ('yield'), or plant biomass ('bioms') for crops #' at harvest-kill of a crop separated for all identified crops. #' #' @param sim_verify Simulation output of the function \code{run_swat_verification()}. #' To plot the heat units at least the output option \code{outputs = 'mgt'} must #' be set in \code{run_swat_verification()} +#' @param variable Selected variable to be plotted. Must be one of: 'phu', 'yield', 'bioms' +#' @param years Simulated years which are aggregated in the boxplot #' -#' @return ggplot boxplot with crop heat unit fractions at harvest-kill. +#' @return ggplot boxplot the selected variable at harvest-kill. #' #' @importFrom dplyr filter group_by mutate rename select ungroup %>% #' @importFrom ggplot2 aes ggplot geom_boxplot geom_hline labs theme_bw +#' @importFrom purrr set_names #' @export #' -plot_phuplant_harvest <- function(sim_verify) { - hu_harv <- sim_verify$mgt_out %>% +plot_variable_at_harvkill <- function(sim_verify, variable, years = 1900:2100) { + tbl_harv <- sim_verify$mgt_out %>% + filter(year %in% years) %>% filter(operation %in% c('HARVEST', 'KILL')) %>% group_by(hru,year, mon, day) %>% mutate(n = n()) %>% ungroup() %>% - filter(n == 2, operation == 'HARVEST') %>% - select(op_typ, phuplant) + filter(n == 2, operation == 'HARVEST') - ggplot(data = hu_harv) + - geom_boxplot(aes(x = op_typ, y = phuplant)) + - geom_hline(yintercept = 1, lty = 2) + - labs(x = 'Crops', y = 'Crop HUs at harvest') + + if(variable == 'phu') { + tbl_harv <- tbl_harv %>% + select(op_typ, phuplant) %>% + set_names(c('crop', 'var')) + + y_lbl <- 'Crop HUs at harvest/kill' + }else if(variable == 'yield') { + tbl_harv <- tbl_harv %>% + select(op_typ, op_var) %>% + set_names(c('crop', 'var')) + + y_lbl <- 'Crop yield at harvest/kill' + } else if(variable == 'bioms') { + tbl_harv <- tbl_harv %>% + select(op_typ, plant_bioms) %>% + set_names(c('crop', 'var')) + + y_lbl <- 'Biomass at harvest/kill' + } else { + stop("Variable must be one of: 'phu', 'yield', 'bioms'") + } + + gg <- ggplot(data = tbl_harv) + + geom_boxplot(aes(x = crop, y = var)) + + labs(x = 'Crops', y = 'Crop HUs at harvest/kill') + theme_bw() + + if(variable == 'phu') { + gg + geom_hline(yintercept = 1, lty = 2) + } + + return(gg) } diff --git a/man/plot_phuplant_harvest.Rd b/man/plot_phuplant_harvest.Rd deleted file mode 100644 index 6585635..0000000 --- a/man/plot_phuplant_harvest.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/print_mgt.R -\name{plot_phuplant_harvest} -\alias{plot_phuplant_harvest} -\title{Boxplot of heat unit fractions for crops at harvest-kill operation} -\usage{ -plot_phuplant_harvest(sim_verify) -} -\arguments{ -\item{sim_verify}{Simulation output of the function \code{run_swat_verification()}. -To plot the heat units at least the output option \code{outputs = 'mgt'} must -be set in \code{run_swat_verification()}} -} -\value{ -ggplot boxplot with crop heat unit fractions at harvest-kill. -} -\description{ -plot_phuplant_harvest plots boxplots of the collected heat unit fractions -at harvest-kill of a crop separated for all identified crops. -} diff --git a/man/plot_variable_at_harvkill.Rd b/man/plot_variable_at_harvkill.Rd new file mode 100644 index 0000000..8ca29ba --- /dev/null +++ b/man/plot_variable_at_harvkill.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/print_mgt.R +\name{plot_variable_at_harvkill} +\alias{plot_variable_at_harvkill} +\title{Boxplot for relevant variables at harvest-kill} +\usage{ +plot_variable_at_harvkill(sim_verify, variable, years = 1900:2100) +} +\arguments{ +\item{sim_verify}{Simulation output of the function \code{run_swat_verification()}. +To plot the heat units at least the output option \code{outputs = 'mgt'} must +be set in \code{run_swat_verification()}} + +\item{variable}{Selected variable to be plotted. Must be one of: 'phu', 'yield', 'bioms'} + +\item{years}{Simulated years which are aggregated in the boxplot} +} +\value{ +ggplot boxplot the selected variable at harvest-kill. +} +\description{ +plot_variable_at_harvkill plots boxplots of one of the variables crop heat unit +fractions ('phu'), crop yields ('yield'), or plant biomass ('bioms') for crops +at harvest-kill of a crop separated for all identified crops. +} -- GitLab