-
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.
Showing
10 changed files
with
100 additions
and
10 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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#' Label numbers in log format (10^3, 10^6, etc) | ||
#' | ||
#' `label_log()` displays numbers as base^exponent, using superscript formatting. | ||
#' | ||
#' @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()]. | ||
#' @inherit number_format 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)) | ||
label_log <- function(base = 10, digits = 3) { | ||
function(x) { | ||
if (length(x) == 0) { | ||
return(expression()) | ||
} | ||
|
||
exponent <- format(log(x, base = base), digits = digits) | ||
text <- paste0(base, "^", exponent) | ||
ret <- parse_safe(text) | ||
|
||
# restore NAs from input vector | ||
ret[is.na(x)] <- NA | ||
|
||
ret | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
test_that("label_log() returns expression", { | ||
expect_identical(label_log()(numeric()), expression()) | ||
expect_identical(label_log()(NA_real_), expression(NA)) | ||
|
||
expect_equal(label_log()(c(0.1, 10)), expression(10^-1, 10^1)) | ||
expect_equal(label_log(base = 2)(8), expression(2^3)) | ||
expect_equal(label_log(base = 2, digits = 3)(7), expression(2^2.81)) | ||
}) |