-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
46 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.