From 22d23dbb2eba19f4d6dd8ba392599c5eecd4a2aa Mon Sep 17 00:00:00 2001 From: jrmuirhead Date: Mon, 25 Mar 2024 15:01:31 -0400 Subject: [PATCH] Proposed fix for issue 146 - missing data and calculation of secondary axis scale Jmuirhead/issue146 (#147) * Update help_secondary.R Add na.rm = TRUE in calculations of range of to and from * Propagate na.rm top-down * Fix plumbing * Add news bullet --------- Co-authored-by: Teun van den Brand --- NEWS.md | 2 ++ R/help_secondary.R | 20 ++++++++++++-------- man/help_secondary.Rd | 6 ++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index d35b350..2579a76 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggh4x (development version) +* `help_secondary()` has a new `na.rm` argument (#146, thanks @jrmuirhead!) + # ggh4x 0.2.8 This is a small release fixing a NOTE by request. In addition, there are two diff --git a/R/help_secondary.R b/R/help_secondary.R index 268b794..364f0d5 100644 --- a/R/help_secondary.R +++ b/R/help_secondary.R @@ -20,6 +20,9 @@ #' \item{`"sortfit"`}{Sorts the both `primary` and `secondary` independently #' before passing these on to the `"fit"` method.} #' } +#' @param na.rm A `logical(1)`: whether to remove missing values (`TRUE`) or +#' propagate missing values (`FALSE`). Applies to the `method = "range"` and +#' `method = "max"` methods. #' @inheritDotParams ggplot2::sec_axis -trans #' #' @details The intent is to run this function before starting a plot. The @@ -69,6 +72,7 @@ help_secondary <- function( primary = c(0, 1), secondary = c(0, 1), method = c("range", "max", "fit", "ccf", "sortfit"), + na.rm = TRUE, ... ) { primary <- enquo(primary) @@ -82,8 +86,8 @@ help_secondary <- function( help <- switch( method, - "range" = help_sec_range(primary, secondary), - "max" = help_sec_max(primary, secondary), + "range" = help_sec_range(primary, secondary, na.rm = na.rm), + "max" = help_sec_max(primary, secondary, na.rm = na.rm), "fit" = help_sec_fit(primary, secondary), "ccf" = help_sec_ccf(primary, secondary), "sortfit" = help_sec_sortfit(primary, secondary) @@ -112,9 +116,9 @@ new_sec_axis <- function(trans = NULL, ...) { # Methods ----------------------------------------------------------------- -help_sec_range <- function(from, to) { - from <- range(from) - to <- range(to) +help_sec_range <- function(from, to, na.rm = TRUE) { + from <- range(from, na.rm = na.rm) + to <- range(to, na.rm = na.rm) forward <- function(x) { rescale(x, from = to, to = from) @@ -125,9 +129,9 @@ help_sec_range <- function(from, to) { list(forward = forward, reverse = reverse) } -help_sec_max <- function(from, to) { - from <- range(from) - to <- range(to) +help_sec_max <- function(from, to, na.rm = TRUE) { + from <- range(from, na.rm = na.rm) + to <- range(to, na.rm = na.rm) forward <- function(x) { rescale_max(x, from = to, to = from) diff --git a/man/help_secondary.Rd b/man/help_secondary.Rd index f816aaf..a91f3fa 100644 --- a/man/help_secondary.Rd +++ b/man/help_secondary.Rd @@ -9,6 +9,7 @@ help_secondary( primary = c(0, 1), secondary = c(0, 1), method = c("range", "max", "fit", "ccf", "sortfit"), + na.rm = TRUE, ... ) } @@ -33,9 +34,14 @@ then align the data by truncation. The aligned data is then passed to the before passing these on to the \code{"fit"} method.} }} +\item{na.rm}{A \code{logical(1)}: whether to remove missing values (\code{TRUE}) or +propagate missing values (\code{FALSE}). Applies to the \code{method = "range"} and +\code{method = "max"} methods.} + \item{...}{ Arguments passed on to \code{\link[ggplot2:sec_axis]{ggplot2::sec_axis}} \describe{ + \item{\code{transform}}{A formula or function of transformation} \item{\code{name}}{The name of the secondary axis} \item{\code{breaks}}{One of: \itemize{