Skip to content
Snippets Groups Projects
Commit 7019de28 authored by Christoph Schürz's avatar Christoph Schürz
Browse files

Merge branch 'dev_add_kill' of git.ufz.de:schuerz/swatdoctr into dev_add_kill

parents 7899a777 09026583
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,8 @@ Imports:
stringr,
tibble,
tidyr,
tidyselect
tidyselect,
plotly
Suggests:
datasets,
stats
......
......@@ -2,8 +2,11 @@
export(add_kill_op)
export(get_hru_id_by_attribute)
export(plot_basin_var)
export(plot_climate_annual)
export(plot_hru_pw_day)
export(plot_hru_var)
export(plot_hru_var_aa)
export(plot_monthly_snow)
export(plot_variable_at_harvkill)
export(print_avannual_qtile)
......@@ -18,6 +21,7 @@ importFrom(dplyr,"%>%")
importFrom(dplyr,arrange)
importFrom(dplyr,filter)
importFrom(dplyr,group_by)
importFrom(dplyr,group_map)
importFrom(dplyr,group_split)
importFrom(dplyr,lead)
importFrom(dplyr,left_join)
......@@ -29,7 +33,6 @@ importFrom(dplyr,select)
importFrom(dplyr,slice)
importFrom(dplyr,slice_sample)
importFrom(dplyr,summarise)
importFrom(dplyr,tibble)
importFrom(dplyr,ungroup)
importFrom(ggplot2,aes)
importFrom(ggplot2,geom_boxplot)
......@@ -40,6 +43,7 @@ importFrom(ggplot2,labs)
importFrom(ggplot2,lims)
importFrom(ggplot2,theme_bw)
importFrom(ggplot2,theme_void)
importFrom(lubridate,floor_date)
importFrom(lubridate,int_end)
importFrom(lubridate,int_start)
importFrom(lubridate,interval)
......@@ -47,6 +51,10 @@ importFrom(lubridate,month)
importFrom(lubridate,yday)
importFrom(lubridate,year)
importFrom(lubridate,ymd)
importFrom(plotly,layout)
importFrom(plotly,plot_ly)
importFrom(plotly,plotly_build)
importFrom(plotly,subplot)
importFrom(processx,run)
importFrom(purrr,map)
importFrom(purrr,map2)
......
......@@ -5,4 +5,4 @@ utils::globalVariables(c("%&&%", "%//%", ".", "crop", "day", "ecanopy", "eplant"
"rhum rm_skp", "schedule", "snofall", "snomlt", "soil", "solarad", "starts_with",
"tile", "time_out", "tmn", "tmpav", "tmx", "topo", "val_max", "val_mean",
"val_min", "value", "value_sum", "var", "var1", "var2", "var3", "var4", "var5",
"wndspd", "yr", "rhum", "rm_skp"))
"wndspd", "yr", "rhum", "rm_skp", "Date", "Values"))
......@@ -47,3 +47,29 @@ insert_line_at <- function(dat, txt, insert_after){
post <- dat[(insert_after+1):length(dat)]
return(c(pre, txt, post))
}
#' Function to put option remove hide or show all lines in chart and this function also print chart.
#'
#' @param graph plotly graph object
#' @importFrom plotly plotly_build layout
#' @return plotly graph object with option to remove or show all lines
#' @keywords internal
#'
#' @examples
#' \dontrun{
#' hide_show(fig)
#' }
hide_show <- function(graph){
plotly_build(graph) %>%
layout(updatemenus = list(
list(type = "buttons", direction = "right", xanchor = "center", yanchor = "top",
showactive = FALSE, x = 0.3, y = 1.0,
buttons = list(
list(method = "restyle",
args = list("visible", "all"),
label = "show all"),
list(method = "restyle",
args = list("visible", "legendonly"),
label = "hide all")))))
}
......@@ -237,3 +237,44 @@ plot_monthly_snow <- function(sim_verify) {
facet_wrap(.~process, ncol = 1) +
theme_bw()
}
#' Plot selected basin variable aggregated by defined time step
#'
#' @param sim_verify Simulation output of the function \code{run_swat_verification()}.
#' To plot the climate outputs at least the output option \code{outputs = 'wb'} must
#' be set in \code{run_swat_verification()}.
#' @param var Character which defines the variable to be printed
#' @param period (optional) character describing, which time interval to display (default is "day",
#' other examples are "week", "month", etc). \code{Default = "day"}
#' @param fn_summarize (optional) function to recalculate to time interval (default is "mean", other examples
#' are "median", "sum", etc). \code{Default = "mean"}
#' @importFrom lubridate floor_date
#' @importFrom plotly plot_ly layout
#' @importFrom dplyr %>% rename summarize mutate group_by arrange
#' @return plotly figure object
#' @export
#'
#' @examples
#' \dontrun{
#' plot_basin_var(sim_nostress, "percn")
#' }
plot_basin_var <- function(sim_verify, var, period = "day", fn_summarize = "mean"){
if(var %in% names(sim_verify$basin_wb_day)){
df <- sim_verify$basin_wb_day[,c("yr","mon","day",var)]
} else if (var %in% names(sim_verify$basin_pw_day)){
df <- sim_verify$basin_pw_day[,c("yr","mon","day",var)]
}
##Aggregating data by time step
df$Date<- floor_date(ISOdate(df$yr, df$mon, df$day), period)
df <- df[c("Date", var)] %>%
rename(Values = 2) %>%
group_by(Date) %>%
summarize(Values = get(fn_summarize)(Values))
##Plotting
plot_ly(df %>% arrange(Date), x=~Date, y=~Values, name = var, type = 'scatter', mode = 'lines',
connectgaps = FALSE) %>% layout(showlegend = FALSE) %>%
layout(title = paste(var, "variable"), yaxis = list(title = "Values"))
}
......@@ -44,6 +44,7 @@ get_hru_id_by_attribute <- function(sim_verify, lum = NULL, mgt = NULL, soil = N
#' be set in \code{run_swat_verification()}
#' @param hru_id Numeric vector with HRU ids for which variables should be plotted
#' @param var Character vector that defines the variable names that are plotted
#' @param title Character for title to be put in the figure.
#' @param years Years of the simulated data for which varaibles are plotted.
#'
#' @importFrom dplyr filter mutate select %>%
......@@ -56,7 +57,7 @@ get_hru_id_by_attribute <- function(sim_verify, lum = NULL, mgt = NULL, soil = N
#'
#' @export
#'
plot_hru_pw_day <- function(sim_verify, hru_id, var, years = 1900:2100) {
plot_hru_pw_day <- function(sim_verify, hru_id, var, title = "", years = 1900:2100) {
plot_data <- sim_verify$hru_pw_day %>%
filter(unit %in% hru_id) %>%
filter(yr %in% years) %>%
......@@ -67,7 +68,7 @@ plot_hru_pw_day <- function(sim_verify, hru_id, var, years = 1900:2100) {
ggplot(plot_data) +
geom_line(aes(x = date, y = value, color = unit, lty = unit)) +
labs(x = 'Date', color = 'HRU', lty = 'HRU') +
labs(x = 'Date', color = 'HRU', lty = 'HRU', title=title) +
scale_x_date(date_labels = '%Y-%m-%d') +
facet_grid(rows = vars(all_of(var)), scales = 'free_y', switch = 'y') +
theme_bw() +
......@@ -78,7 +79,6 @@ plot_hru_pw_day <- function(sim_verify, hru_id, var, years = 1900:2100) {
axis.title.y = element_blank())
}
#' Print the average annual qtile for HRUs
#'
#' print_avannual_qtile prints a table with the average annual qtile in mm
......@@ -112,3 +112,77 @@ print_avannual_qtile <- function(sim_verify,
arrange(qtile, id)
}
#' Aggregate and plot simulated variables saved in hru_pw_day
#'
#' @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 hru_id Numeric vector with HRU ids for which variables should be plotted.
#' @param var Character vector that defines the variable names that are plotted.
#' @param period (optional) character describing, which time interval to display (default is "day",
#' other examples are "week", "month", etc). \code{Default = "day"}
#' @param fn_summarize (optional) function to recalculate to time interval (default is "mean", other examples
#' are "median", "sum", etc). \code{Default = "mean"}
#' @return plotly figure object
#' @importFrom lubridate floor_date
#' @importFrom plotly plot_ly layout
#' @importFrom dplyr %>% rename summarize mutate group_by arrange
#' @export
#'
#' @examples
#' \dontrun{
#' id <- get_hru_id_by_attribute(sim_nostress, "wwht_lum")
#' plot_hru_var(sim_nostress, sample(id$id, 10), "lai")
#' }
plot_hru_var <- function(sim_verify, hru_id, var, period = "day", fn_summarize = "mean"){
options(dplyr.summarise.inform = FALSE)
df <- sim_verify$hru_pw_day[sim_verify$hru_pw_day$unit %in% hru_id, c("unit", "yr", "mon", "day", var)]
##Aggregating data by time step
df$Date<- floor_date(ISOdate(df$yr, df$mon, df$day), period)
df <- df[c("Date", "unit", var)] %>%
rename(Values = 3) %>%
mutate(unit = paste('hru:', unit)) %>%
group_by(unit, Date) %>%
summarize(Values = get(fn_summarize)(Values))
##Plotting
plot_ly(df %>% arrange(Date), x=~Date, y=~Values, color = ~factor(unit), colors = "Set2", type = 'scatter', mode = 'lines',
connectgaps = FALSE) %>% layout(showlegend = TRUE) %>%
layout(title = paste(var, "parameter"), xaxis = list(title = "Date"), yaxis = list(title = "Values")) %>%
hide_show()
}
#' Plot simulated variables of filtered HRUs saved in hru_wb_aa with boxplots
#'
#' @param sim_verify Simulation output of the function \code{run_swat_verification()}.
#' To plot the heat units at least the output option \code{outputs = 'wb'} must
#' be set in \code{run_swat_verification()}
#' @param lum Optional character vector with landuse.lum labels
#' @param mgt Optional character vector with management labels as defined in management.sch.
#' @param soil Optional character vector with soil type labels as defined in the soil data.
#' @return plot_ly multi boxplot figure for all available variables, which are not 0.
#' @importFrom dplyr %>% mutate group_by group_map
#' @importFrom tidyr pivot_longer
#' @importFrom plotly plot_ly layout subplot
#' @export
#'
#' @examples
#' \dontrun{
#' plot_hru_var_aa(sim_nostress, "wwht_lum")
#' }
plot_hru_var_aa <- function(sim_verify, lum = NULL, mgt = NULL, soil = NULL){
p <- paste(lum, mgt, soil)
id <- get_hru_id_by_attribute(sim_verify, lum, mgt, soil)
fig <- sim_verify$hru_wb_aa[sim_verify$hru_wb_aa$unit %in% id$id, -c(1:7)] %>%
.[, colSums(.!= 0) > 0] %>%
mutate(p = p) %>%
pivot_longer(-p,names_to = 'var', values_to = 'Values') %>%
group_by(p, var) %>%
group_map(~plot_ly(., y=~Values, color = ~var, colors = "cyan4", type = 'box'), keep = TRUE) %>%
subplot(nrows = 5) %>%
layout(title = paste("HRUs selected by", p))
return(fig)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/helper.R
\name{hide_show}
\alias{hide_show}
\title{Function to put option remove hide or show all lines in chart and this function also print chart.}
\usage{
hide_show(graph)
}
\arguments{
\item{graph}{plotly graph object}
}
\value{
plotly graph object with option to remove or show all lines
}
\description{
Function to put option remove hide or show all lines in chart and this function also print chart.
}
\examples{
\dontrun{
hide_show(fig)
}
}
\keyword{internal}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot_climate.R
\name{plot_basin_var}
\alias{plot_basin_var}
\title{Plot selected basin variable aggregated by defined time step}
\usage{
plot_basin_var(sim_verify, var, period = "day", fn_summarize = "mean")
}
\arguments{
\item{sim_verify}{Simulation output of the function \code{run_swat_verification()}.
To plot the climate outputs at least the output option \code{outputs = 'wb'} must
be set in \code{run_swat_verification()}.}
\item{var}{Character which defines the variable to be printed}
\item{period}{(optional) character describing, which time interval to display (default is "day",
other examples are "week", "month", etc). \code{Default = "day"}}
\item{fn_summarize}{(optional) function to recalculate to time interval (default is "mean", other examples
are "median", "sum", etc). \code{Default = "mean"}}
}
\value{
plotly figure object
}
\description{
Plot selected basin variable aggregated by defined time step
}
\examples{
\dontrun{
plot_basin_var(sim_nostress, "percn")
}
}
......@@ -4,7 +4,7 @@
\alias{plot_hru_pw_day}
\title{Plot daily simulated variables which are saved in hru_pw_day}
\usage{
plot_hru_pw_day(sim_verify, hru_id, var, years = 1900:2100)
plot_hru_pw_day(sim_verify, hru_id, var, title = "", years = 1900:2100)
}
\arguments{
\item{sim_verify}{Simulation output of the function \code{run_swat_verification()}.
......@@ -15,6 +15,8 @@ be set in \code{run_swat_verification()}}
\item{var}{Character vector that defines the variable names that are plotted}
\item{title}{Character for title to be put in the figure.}
\item{years}{Years of the simulated data for which varaibles are plotted.}
}
\value{
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot_hru_pw.R
\name{plot_hru_var}
\alias{plot_hru_var}
\title{Aggregate and plot simulated variables saved in hru_pw_day}
\usage{
plot_hru_var(sim_verify, hru_id, var, period = "day", fn_summarize = "mean")
}
\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{hru_id}{Numeric vector with HRU ids for which variables should be plotted.}
\item{var}{Character vector that defines the variable names that are plotted.}
\item{period}{(optional) character describing, which time interval to display (default is "day",
other examples are "week", "month", etc). \code{Default = "day"}}
\item{fn_summarize}{(optional) function to recalculate to time interval (default is "mean", other examples
are "median", "sum", etc). \code{Default = "mean"}}
}
\value{
plotly figure object
}
\description{
Aggregate and plot simulated variables saved in hru_pw_day
}
\examples{
\dontrun{
id <- get_hru_id_by_attribute(sim_nostress, "wwht_lum")
plot_hru_var(sim_nostress, sample(id$id, 10), "lai")
}
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot_hru_pw.R
\name{plot_hru_var_aa}
\alias{plot_hru_var_aa}
\title{Plot simulated variables of filtered HRUs saved in hru_wb_aa with boxplots}
\usage{
plot_hru_var_aa(sim_verify, lum = NULL, mgt = NULL, soil = NULL)
}
\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 = 'wb'} must
be set in \code{run_swat_verification()}}
\item{lum}{Optional character vector with landuse.lum labels}
\item{mgt}{Optional character vector with management labels as defined in management.sch.}
\item{soil}{Optional character vector with soil type labels as defined in the soil data.}
}
\value{
plot_ly multi boxplot figure for all available variables, which are not 0.
}
\description{
Plot simulated variables of filtered HRUs saved in hru_wb_aa with boxplots
}
\examples{
\dontrun{
plot_hru_var_aa(sim_nostress, "wwht_lum")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment