Skip to content

Commit

Permalink
isolate formatting function
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Oct 4, 2024
1 parent 9215dc1 commit a299074
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
LazyLoad: yes
Roxygen: list(markdown = TRUE, r6 = FALSE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export(exp_trans)
export(expand_range)
export(extended_breaks)
export(format_format)
export(format_log)
export(fullseq)
export(gradient_n_pal)
export(grey_pal)
Expand Down
57 changes: 36 additions & 21 deletions R/label-log.R
Original file line number Diff line number Diff line change
@@ -1,45 +1,60 @@
#' Label numbers in log format (10^3, 10^6, etc)
#'
#' `label_log()` displays numbers as base^exponent, using superscript formatting.
#' `label_log()` and `format_log()` display numbers as base^exponent, using
#' superscript formatting. `label_log()` returns expressions suitable for
#' labelling in scales, whereas `format_log()` returns deparsed text.
#'
#' @param base Base of logarithm to use
#' @param digits Number of significant digits to show for the exponent. Argument
#' is passed on to [base::format()].
#' @param signed Should a `+` or `-` be displayed as a prefix? The
#' default, `NULL`, displays signs if there are zeroes or negative numbers
#' present.
#' @param ... Passed on to `format()`.
#' @inherit label_number return
#' @seealso [breaks_log()] for the related breaks algorithm.
#' @export
#' @family labels for log scales
#' @examples
#' demo_log10(c(1, 1e5), labels = label_log())
#' demo_log10(c(1, 1e5), breaks = breaks_log(base = 2), labels = label_log(base = 2))
#' format_log(c(0.1, 1, 10))
label_log <- function(base = 10, digits = 3, signed = NULL) {
function(x) {
if (length(x) == 0) {
return(expression())
}
prefix <- rep("", length(x))
xfinite <- x[is.finite(x)]
signed <- signed %||% any(xfinite <= 0)
if (signed) {
sign <- sign(x)
prefix[sign == +1] <- "+"
prefix[sign == -1] <- "-"
x <- abs(x)
}

exponent <- format(zapsmall(log(x, base = base)), digits = digits)
text <- paste0(prefix, base, "^", exponent)
if (signed) {
text[x == 0] <- "0"
}
text <- format_log(x, base = base, signed = signed, digits = digits)
ret <- parse_safe(text)

# restore NAs from input vector
ret[is.na(x)] <- NA

ret
}
}

#' @export
#' @rdname label_log
format_log <- function(x, base = 10, signed = NULL, ...) {

if (length(x) == 0) {
return(character())
}
prefix <- rep("", length(x))
finites <- x[is.finite(x)]

signed <- signed %||% any(finites <= 0)
if (signed) {
sign <- sign(x)
prefix[sign == +1] <- "+"
prefix[sign == -1] <- "-"
x <- abs(x)
x[x == 0] <- 1
}

exponent <- format(zapsmall(log(x, base = base)), ...)
text <- paste0(prefix, base, "^", exponent)

if (signed) {
text[sign == 0] <- "0"
}
text[is.na(x)] <- NA

text
}
9 changes: 8 additions & 1 deletion man/label_log.Rd

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

0 comments on commit a299074

Please sign in to comment.