Skip to content

Commit

Permalink
add nms, fils, and cols arg to show_trndseason, rename cols to fils f…
Browse files Browse the repository at this point in the history
…or show_mettrndseason
  • Loading branch information
fawda123 committed Mar 7, 2024
1 parent 620182a commit 3092f60
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 44 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: wqtrends
Title: Assess Water Quality Trends with Generalized Additive Models
Version: 1.4.2
Date: 2024-03-03
Date: 2024-03-07
Authors@R: c(
person(given = "Marcus",
family = "Beck",
Expand Down
42 changes: 21 additions & 21 deletions R/show_mettrndseason.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
#' @param width numeric for width of error bars
#' @param size numeric for point size
#' @param nms optional character vector for trend names, see details
#' @param cols optional character vector for point colors, see details
#' @param cmbn logical indicating if the no trend and on estimate colors should be combined, see details
#' @param fils optional character vector for the fill of interior point colors, see details
#' @param cmbn logical indicating if the no trend and no estimate colors should be combined, see details
#' @param base_size numeric indicating base font size, passed to \code{\link[ggplot2]{theme_bw}}
#' @param xlim optional numeric vector of length two for x-axis limits
#' @param ylim optional numeric vector of length two for y-axis limits
#'
#' @details
#' The plot is the same as that returned by \code{\link{show_metseason}} with the addition of points for the seasonal metrics colored by the trends estimated from \code{\link{anlz_trndseason}} for the specified window and justification.
#'
#' Four colors are used to define increasing, decreasing, no trend, or no estimate (i.e., too few points for the window). The names and the colors can be changed using the \code{nms} and \code{cols} arguments, respectively. The \code{cmbn} argument can be used to combine the no trend and no estimate colors into one color and label. Although this may be desired for aesthetic reasons, the colors and labels may be misleading with the default names since no trend is shown for points where no estimates were made.
#' Four colors are used to define increasing, decreasing, no trend, or no estimate (i.e., too few points for the window). The names and the colors can be changed using the \code{nms} and \code{fils} arguments, respectively. The \code{cmbn} argument can be used to combine the no trend and no estimate colors into one color and label. Although this may be desired for aesthetic reasons, the colors and labels may be misleading with the default names since no trend is shown for points where no estimates were made.
#'
#' The optional \code{yromit} vector can be used to omit years from the plot and trend assessment. This may be preferred if seasonal estimates for a given year have very wide confidence intervals likely due to limited data, which can skew the trend assessments.
#'
Expand All @@ -38,57 +38,57 @@
#' mod <- anlz_gam(tomod, trans = 'log10')
#' show_mettrndseason(mod, metfun = mean, doystr = 90, doyend = 180, justify = 'center',
#' win = 4, ylab = 'Chlorophyll-a (ug/L)')
show_mettrndseason <- function(mod, metfun = mean, doystr = 1, doyend = 364, justify = c('center', 'left', 'right'), win = 5, nsim = 1e4, useave = FALSE, yromit = NULL, ylab, width = 0.9, size = 3, nms = NULL, cols = NULL, cmbn = F, base_size = 11, xlim = NULL, ylim = NULL, ...){
show_mettrndseason <- function(mod, metfun = mean, doystr = 1, doyend = 364, justify = c('center', 'left', 'right'), win = 5, nsim = 1e4, useave = FALSE, yromit = NULL, ylab, width = 0.9, size = 3, nms = NULL, fils = NULL, cmbn = F, base_size = 11, xlim = NULL, ylim = NULL, ...){

# get seasonal metrics and trends
trndseason <- anlz_trndseason(mod = mod, metfun, doystr = doystr, doyend = doyend, justify = justify, win = win, useave = useave, yromit = yromit)

# handle nms and cols args if not combine (keep no trend and no estimate)
# handle nms and fils args if not combine (keep no trend and no estimate)
if(!cmbn){

if(is.null(nms)) nms <- c("Increasing", "Decreasing", "No trend", "No estimate")
if(is.null(cols)) cols <- c('tomato1', 'deepskyblue3', 'white', 'darkgrey')
if(is.null(fils)) fils <- c('tomato1', 'deepskyblue3', 'white', 'darkgrey')

if(length(cols) != 4 | length(nms) != 4)
if(length(fils) != 4 | length(nms) != 4)
stop('Four names or colors must be provided')

# plot objects, add column for trend
trndseason <- trndseason %>%
dplyr::mutate(
trnd = dplyr::case_when(
yrcoef > 0 & pval < 0.05 ~ cols[1],
yrcoef < 0 & pval < 0.05 ~ cols[2],
pval >= 0.05 ~ cols[3],
is.na(yrcoef) ~ cols[4]
yrcoef > 0 & pval < 0.05 ~ fils[1],
yrcoef < 0 & pval < 0.05 ~ fils[2],
pval >= 0.05 ~ fils[3],
is.na(yrcoef) ~ fils[4]
),
trnd = factor(trnd, levels = cols, labels = nms)
trnd = factor(trnd, levels = fils, labels = nms)
)

}

# handle nms and cols args if combine (combine no trend and no estimate)
# handle nms and fils args if combine (combine no trend and no estimate)
if(cmbn){

if(is.null(nms)) nms <- c("Increasing", "Decreasing", "No trend")
if(is.null(cols)) cols <- c('tomato1', 'deepskyblue3', 'white')
if(is.null(fils)) fils <- c('tomato1', 'deepskyblue3', 'white')

if(length(cols) != 3 | length(nms) != 3)
if(length(fils) != 3 | length(nms) != 3)
stop('Three names or colors must be provided')

# plot objects, add column for trend
trndseason <- trndseason %>%
dplyr::mutate(
trnd = dplyr::case_when(
yrcoef > 0 & pval < 0.05 ~ cols[1],
yrcoef < 0 & pval < 0.05 ~ cols[2],
pval >= 0.05 | is.na(yrcoef) ~ cols[3]
yrcoef > 0 & pval < 0.05 ~ fils[1],
yrcoef < 0 & pval < 0.05 ~ fils[2],
pval >= 0.05 | is.na(yrcoef) ~ fils[3]
),
trnd = factor(trnd, levels = cols, labels = nms)
trnd = factor(trnd, levels = fils, labels = nms)
)

}

names(cols) <- nms
names(fils) <- nms

# title
dts <- as.Date(c(doystr, doyend), origin = as.Date("2000-12-31"))
Expand All @@ -107,7 +107,7 @@ show_mettrndseason <- function(mod, metfun = mean, doystr = 1, doyend = 364, jus
ggplot2::geom_errorbar(ggplot2::aes(ymin = bt_lwr, ymax = bt_upr), colour = 'black', width = width, na.rm = TRUE) +
ggplot2::geom_point(ggplot2::aes(fill = trnd), pch = 21, color = 'black', size = size, na.rm = TRUE) +
ggplot2::theme_bw(base_size = base_size) +
ggplot2::scale_fill_manual(values = cols, drop = F) +
ggplot2::scale_fill_manual(values = fils, drop = F) +
ggplot2::theme(
axis.title.x = ggplot2::element_blank(),
legend.position = 'top'
Expand Down
42 changes: 25 additions & 17 deletions R/show_trndseason.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#' @inheritParams anlz_trndseason
#' @param type chr string indicating if log slopes are shown (if applicable)
#' @param ylab chr string for y-axis label
#' @param nms optional character vector for trend names
#' @param fils optional character vector for the fill of interior point colors
#' @param cols optional character vector for confidence interval colors
#' @param base_size numeric indicating base font size, passed to \code{\link[ggplot2]{theme_bw}}
#' @param xlim optional numeric vector of length two for x-axis limits
#' @param ylim optional numeric vector of length two for y-axis limits
Expand All @@ -28,7 +31,8 @@
#' ylab = 'Slope Chlorophyll-a (ug/L/yr)')
show_trndseason <- function(mod, metfun = mean, doystr = 1, doyend = 364, type = c('log10', 'approx'),
justify = c('left', 'right', 'center'), win = 5, ylab, nsim = 1e4,
yromit = NULL, useave = FALSE, base_size = 11, xlim = NULL, ylim = NULL, ...) {
yromit = NULL, useave = FALSE, base_size = 11, nms = NULL, fils = NULL,
cols = NULL, xlim = NULL, ylim = NULL, ...) {

justify <- match.arg(justify)
type <- match.arg(type)
Expand All @@ -47,24 +51,28 @@ show_trndseason <- function(mod, metfun = mean, doystr = 1, doyend = 364, type =
# year range
yrrng <- range(trndseason$yr, na.rm = T)

# shape and factor vectors
pfct <- c('Increasing', 'Decreasing', 'No trend')
pshp <- c(21, 21, 21)
pcol <- c('tomato1', 'deepskyblue3', 'black')
pfil <- c('tomato1', 'deepskyblue3', 'white')
names(pcol) <- pfct
names(pfil) <- pfct
names(pshp) <- pfct
# setup names, colors, and fills
if(is.null(nms)) nms <- c("Increasing", "Decreasing", "No trend")
if(is.null(fils)) fils <- c('tomato1', 'deepskyblue3', 'white')
if(is.null(cols)) cols <- c('tomato1', 'deepskyblue3', 'black')

if(length(nms) != 3 | length(fils) != 3 | length(cols) != 3)
stop('Three names, fills, or colors must be provided')

shps <- c(21, 21, 21)
names(fils) <- nms
names(cols) <- nms
names(shps) <- nms

# to plot, no NA
toplo <- trndseason %>%
dplyr::mutate(
pval = dplyr::case_when(
pval < 0.05 & yrcoef > 0 ~ pfct[1],
pval < 0.05 & yrcoef < 0 ~ pfct[2],
T ~ pfct[3]
pval < 0.05 & yrcoef > 0 ~ nms[1],
pval < 0.05 & yrcoef < 0 ~ nms[2],
T ~ nms[3]
),
pval = factor(pval, levels = pfct)
pval = factor(pval, levels = nms)
) %>%
na.omit()

Expand All @@ -75,7 +83,7 @@ show_trndseason <- function(mod, metfun = mean, doystr = 1, doyend = 364, type =
p <- ggplot2::ggplot(data = toplo, ggplot2::aes(x = yr, y = yrcoef, fill = pval)) +
ggplot2::geom_hline(yintercept = 0) +
ggplot2::geom_errorbar(ggplot2::aes(ymin = yrcoef_lwr, ymax = yrcoef_upr, color = pval), width = 0, na.rm = TRUE) +
ggplot2::scale_color_manual(values = pcol, drop = FALSE)
ggplot2::scale_color_manual(values = cols, drop = FALSE)

}

Expand All @@ -100,14 +108,14 @@ show_trndseason <- function(mod, metfun = mean, doystr = 1, doyend = 364, type =
p <- ggplot2::ggplot(data = toplo, ggplot2::aes(x = yr, y = yrcoef, fill = pval)) +
ggplot2::geom_hline(yintercept = 0) +
ggplot2::geom_errorbar(ggplot2::aes(ymin = yrcoef_lwr, ymax = yrcoef_upr, color = pval), width = 0, na.rm = TRUE) +
ggplot2::scale_color_manual(values = pcol, drop = FALSE)
ggplot2::scale_color_manual(values = cols, drop = FALSE)

}

p <- p +
ggplot2::geom_point(ggplot2::aes(shape = pval), size = 3, na.rm = TRUE) +
ggplot2::scale_fill_manual(values = pfil, drop = FALSE) +
ggplot2::scale_shape_manual(values = pshp, drop = FALSE) +
ggplot2::scale_fill_manual(values = fils, drop = FALSE) +
ggplot2::scale_shape_manual(values = shps, drop = FALSE) +
ggplot2::scale_x_continuous(limits = yrrng) +
ggplot2::theme_bw(base_size = base_size) +
ggplot2::theme(
Expand Down
8 changes: 4 additions & 4 deletions man/show_mettrndseason.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions man/show_trndseason.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion tests/testthat/test-show_trndseason.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ test_that("Checking show_trndseason class, model ident", {

expect_is(result, 'ggplot')

})
})

test_that("Checking show_trndseason nms inputs", {

expect_error(show_trndseason(modident, doystr = 90, doyend = 180, justify = 'left', win = 5, ylab = 'Slope chlorophyll-a (ug/L)', nms = 'error'), 'Three names, fills, or colors must be provided')

})

0 comments on commit 3092f60

Please sign in to comment.