diff --git a/NEWS.md b/NEWS.md index 67f6ae9..6e37a7c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,7 @@ editor_options: - Define function `check_poly_formula()` and use it in `stat_poly_eq()` and `stat_quant_eq()` to validate that the user-supplied model formula is a polynomial with terms in order of increasing powers. If not validated, `NA` is returned as formatted equation label. - Statistics `stat_poly_eq()`, `stat_ma_eq()`, `stat_quant_eq()`, `stat_correlation()`, and `stat_multcomp()` were in part rewritten, introducing small visual changes in the formatting of labels, including changes in the default number of digits. - Export new utility functions, used internally for formatting parameter estimates into character strings. +- Implement R options `ggpmisc.small.p`, `ggpmisc.small.r` and `ggpmisc.decreasing.poly.eq`. # ggpmisc 0.5.6 diff --git a/R/stat-correlation.R b/R/stat-correlation.R index 53ab15c..fceea42 100644 --- a/R/stat-correlation.R +++ b/R/stat-correlation.R @@ -267,8 +267,8 @@ stat_correlation <- r.conf.level = ifelse(method == "pearson", 0.95, NA), continuity = FALSE, - small.r = FALSE, - small.p = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), + small.p = getOption("ggpmisc.small.p", default = FALSE), coef.keep.zeros = TRUE, r.digits = 2, t.digits = 3, diff --git a/R/stat-ma-eq.R b/R/stat-ma-eq.R index 76e0c42..87591b1 100644 --- a/R/stat-ma-eq.R +++ b/R/stat-ma-eq.R @@ -310,11 +310,11 @@ stat_ma_eq <- function(mapping = NULL, data = NULL, nperm = 99, eq.with.lhs = TRUE, eq.x.rhs = NULL, - small.r = FALSE, - small.p = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), + small.p = getOption("ggpmisc.small.p", default = FALSE), coef.digits = 3, coef.keep.zeros = TRUE, - decreasing = FALSE, + decreasing = getOption("ggpmisc.decreasing.poly.eq", FALSE), rr.digits = 2, theta.digits = 2, p.digits = max(1, ceiling(log10(nperm))), diff --git a/R/stat-multcomp.R b/R/stat-multcomp.R index affd5c5..750c2fd 100644 --- a/R/stat-multcomp.R +++ b/R/stat-multcomp.R @@ -332,7 +332,7 @@ stat_multcomp <- function(mapping = NULL, data = NULL, method.args = list(), contrasts = "Tukey", p.adjust.method = NULL, - small.p = FALSE, + small.p = getOption("ggpmisc.small.p", default = FALSE), adj.method.tag = 4, p.digits = 3, label.type = "bars", diff --git a/R/stat-poly-eq.R b/R/stat-poly-eq.R index e868e56..eeae274 100644 --- a/R/stat-poly-eq.R +++ b/R/stat-poly-eq.R @@ -484,13 +484,13 @@ stat_poly_eq <- function(mapping = NULL, data = NULL, n.min = 2L, eq.with.lhs = TRUE, eq.x.rhs = NULL, - small.r = FALSE, - small.p = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), + small.p = getOption("ggpmisc.small.p", default = FALSE), CI.brackets = c("[", "]"), rsquared.conf.level = 0.95, coef.digits = 3, coef.keep.zeros = TRUE, - decreasing = FALSE, + decreasing = getOption("ggpmisc.decreasing.poly.eq", FALSE), rr.digits = 2, f.digits = 3, p.digits = 3, diff --git a/R/stat-quant-eq.R b/R/stat-quant-eq.R index a9986dc..16284f2 100644 --- a/R/stat-quant-eq.R +++ b/R/stat-quant-eq.R @@ -440,7 +440,7 @@ stat_quant_eq <- function(mapping = NULL, data = NULL, eq.x.rhs = NULL, coef.digits = 3, coef.keep.zeros = TRUE, - decreasing = FALSE, + decreasing = getOption("ggpmisc.decreasing.poly.eq", FALSE), rho.digits = 4, label.x = "left", label.y = "top", label.x.npc = NULL, label.y.npc = NULL, diff --git a/R/utilities-eq-label.R b/R/utilities-eq-label.R index 0471806..c8f81bb 100644 --- a/R/utilities-eq-label.R +++ b/R/utilities-eq-label.R @@ -80,7 +80,7 @@ build_lhs <- function(output.type = "expression", #' @export #' poly2character <- function (x, - decreasing = FALSE, + decreasing = getOption("ggpmisc.decreasing.poly.eq", FALSE), digits = 3, keep.zeros = TRUE) { if (keep.zeros) { @@ -177,7 +177,7 @@ typeset_numbers <- function(eq.char, output.type) { coefs2poly_eq <- function(coefs, coef.digits = 3L, coef.keep.zeros = TRUE, - decreasing = FALSE, + decreasing = getOption("ggpmisc.decreasing.poly.eq", FALSE), eq.x.rhs = "x", lhs = "y~`=`~", output.type = "expression", diff --git a/R/utilities-labels.R b/R/utilities-labels.R index cf77619..86d48f1 100644 --- a/R/utilities-labels.R +++ b/R/utilities-labels.R @@ -262,7 +262,7 @@ bold_label <- function(value, #' @export #' p_value_label <- function(value, - small.p = FALSE, + small.p = getOption("ggpmisc.small.p", default = FALSE), subscript = "", superscript = "", digits = 4, @@ -660,7 +660,7 @@ se_value_label <- function(value, #' r_label <- function(value, method = "pearson", - small.r = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), digits = 3, fixed = TRUE, output.type = "expression", @@ -725,7 +725,7 @@ r_label <- function(value, digits, 10^(-digits), decimal.mark = decimal.mark), sep = "") } else { - paste(r.symbol, "~`=`~", r.value.char) + paste(r.symbol, "~`=`~", r.value.char, sep = "") } } else if (output.type %in% c("latex", "tex", "text", "tikz")) { @@ -747,7 +747,7 @@ r_label <- function(value, digits, 10^(-digits), decimal.mark = decimal.mark), sep = "") } else { - paste(r.symbol, " = ", r.value.char) + paste(r.symbol, " = ", r.value.char, sep = "") } } else if (output.type == "markdown") { @@ -769,7 +769,7 @@ r_label <- function(value, digits, 10^(-digits), decimal.mark = decimal.mark), sep = "") } else { - paste(r.symbol, " = ", r.value.char) + paste(r.symbol, " = ", r.value.char, sep = "") } } @@ -785,7 +785,7 @@ r_label <- function(value, #' @export #' rr_label <- function(value, - small.r = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), digits = 3, fixed = TRUE, output.type = "expression", @@ -866,7 +866,7 @@ rr_label <- function(value, #' @export #' adj_rr_label <- function(value, - small.r = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), digits = 3, fixed = TRUE, output.type = "expression", @@ -1022,7 +1022,7 @@ rr_ci_label <- function(value, #' r_ci_label <- function(value, conf.level, - small.r = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), range.brackets = c("[", "]"), range.sep = NULL, digits = 2, diff --git a/man/coefs2poly_eq.Rd b/man/coefs2poly_eq.Rd index 919ba9c..25a7ecb 100644 --- a/man/coefs2poly_eq.Rd +++ b/man/coefs2poly_eq.Rd @@ -8,7 +8,7 @@ coefs2poly_eq( coefs, coef.digits = 3L, coef.keep.zeros = TRUE, - decreasing = FALSE, + decreasing = getOption("ggpmisc.decreasing.poly.eq", FALSE), eq.x.rhs = "x", lhs = "y~`=`~", output.type = "expression", diff --git a/man/plain_label.Rd b/man/plain_label.Rd index ee53ed7..5939ac4 100644 --- a/man/plain_label.Rd +++ b/man/plain_label.Rd @@ -49,7 +49,7 @@ bold_label( p_value_label( value, - small.p = FALSE, + small.p = getOption("ggpmisc.small.p", default = FALSE), subscript = "", superscript = "", digits = 4, @@ -128,7 +128,7 @@ se_value_label( r_label( value, method = "pearson", - small.r = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), digits = 3, fixed = TRUE, output.type = "expression", @@ -137,7 +137,7 @@ r_label( rr_label( value, - small.r = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), digits = 3, fixed = TRUE, output.type = "expression", @@ -146,7 +146,7 @@ rr_label( adj_rr_label( value, - small.r = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), digits = 3, fixed = TRUE, output.type = "expression", @@ -167,7 +167,7 @@ rr_ci_label( r_ci_label( value, conf.level, - small.r = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), range.brackets = c("[", "]"), range.sep = NULL, digits = 2, diff --git a/man/poly2character.Rd b/man/poly2character.Rd index 0b3e071..0eb6f58 100644 --- a/man/poly2character.Rd +++ b/man/poly2character.Rd @@ -4,7 +4,12 @@ \alias{poly2character} \title{Convert a polynomial into character string} \usage{ -poly2character(x, decreasing = FALSE, digits = 3, keep.zeros = TRUE) +poly2character( + x, + decreasing = getOption("ggpmisc.decreasing.poly.eq", FALSE), + digits = 3, + keep.zeros = TRUE +) } \arguments{ \item{x}{a \code{polynomial} object.} diff --git a/man/stat_correlation.Rd b/man/stat_correlation.Rd index 8f6f2e2..750088a 100644 --- a/man/stat_correlation.Rd +++ b/man/stat_correlation.Rd @@ -16,8 +16,8 @@ stat_correlation( exact = NULL, r.conf.level = ifelse(method == "pearson", 0.95, NA), continuity = FALSE, - small.r = FALSE, - small.p = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), + small.p = getOption("ggpmisc.small.p", default = FALSE), coef.keep.zeros = TRUE, r.digits = 2, t.digits = 3, diff --git a/man/stat_ma_eq.Rd b/man/stat_ma_eq.Rd index a03d4ae..842f393 100644 --- a/man/stat_ma_eq.Rd +++ b/man/stat_ma_eq.Rd @@ -19,11 +19,11 @@ stat_ma_eq( nperm = 99, eq.with.lhs = TRUE, eq.x.rhs = NULL, - small.r = FALSE, - small.p = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), + small.p = getOption("ggpmisc.small.p", default = FALSE), coef.digits = 3, coef.keep.zeros = TRUE, - decreasing = FALSE, + decreasing = getOption("ggpmisc.decreasing.poly.eq", FALSE), rr.digits = 2, theta.digits = 2, p.digits = max(1, ceiling(log10(nperm))), diff --git a/man/stat_multcomp.Rd b/man/stat_multcomp.Rd index cb714d3..5a6c1dd 100644 --- a/man/stat_multcomp.Rd +++ b/man/stat_multcomp.Rd @@ -15,7 +15,7 @@ stat_multcomp( method.args = list(), contrasts = "Tukey", p.adjust.method = NULL, - small.p = FALSE, + small.p = getOption("ggpmisc.small.p", default = FALSE), adj.method.tag = 4, p.digits = 3, label.type = "bars", diff --git a/man/stat_poly_eq.Rd b/man/stat_poly_eq.Rd index ead52cd..6c1f754 100644 --- a/man/stat_poly_eq.Rd +++ b/man/stat_poly_eq.Rd @@ -16,13 +16,13 @@ stat_poly_eq( n.min = 2L, eq.with.lhs = TRUE, eq.x.rhs = NULL, - small.r = FALSE, - small.p = FALSE, + small.r = getOption("ggpmisc.small.r", default = FALSE), + small.p = getOption("ggpmisc.small.p", default = FALSE), CI.brackets = c("[", "]"), rsquared.conf.level = 0.95, coef.digits = 3, coef.keep.zeros = TRUE, - decreasing = FALSE, + decreasing = getOption("ggpmisc.decreasing.poly.eq", FALSE), rr.digits = 2, f.digits = 3, p.digits = 3, diff --git a/man/stat_quant_eq.Rd b/man/stat_quant_eq.Rd index 045d057..3f75dc0 100644 --- a/man/stat_quant_eq.Rd +++ b/man/stat_quant_eq.Rd @@ -19,7 +19,7 @@ stat_quant_eq( eq.x.rhs = NULL, coef.digits = 3, coef.keep.zeros = TRUE, - decreasing = FALSE, + decreasing = getOption("ggpmisc.decreasing.poly.eq", FALSE), rho.digits = 4, label.x = "left", label.y = "top", diff --git a/tests/testthat/test-label-utils.R b/tests/testthat/test-label-utils.R new file mode 100644 index 0000000..d5a403e --- /dev/null +++ b/tests/testthat/test-label-utils.R @@ -0,0 +1,169 @@ +context("label utilities") + +test_that("well-formatted simple labels", { + expect_equal(plain_label(value = 123, value.name = "n", output.type = "expression"), + "plain(n)~`=`~\"123.\"") + expect_equal(plain_label(value = 123, value.name = "n", output.type = "markdown"), + "n = 123.") + expect_equal(plain_label(value = 123, value.name = "n", output.type = "latex"), + "\\mathrm{n} = 123.") + expect_equal(plain_label(value = 123, value.name = "n", output.type = "tex"), + "\\mathrm{n} = 123.") + expect_equal(plain_label(value = 123, value.name = "n", output.type = "tikz"), + "\\mathrm{n} = 123.") + expect_equal(italic_label(value = 123, value.name = "n", output.type = "expression"), + "italic(n)~`=`~\"123.\"") + expect_equal(italic_label(value = 123, value.name = "n", output.type = "markdown"), + "_n_ = 123.") + expect_equal(italic_label(value = 123, value.name = "n", output.type = "latex"), + "\\mathit{n} = 123.") + expect_equal(italic_label(value = 123, value.name = "n", output.type = "tex"), + "\\mathit{n} = 123.") + expect_equal(italic_label(value = 123, value.name = "n", output.type = "tikz"), + "\\mathit{n} = 123.") + expect_equal(bold_label(value = 123, value.name = "n", output.type = "expression"), + "bold(n)~`=`~\"123.\"") + expect_equal(bold_label(value = 123, value.name = "n", output.type = "markdown"), + "**n** = 123.") + expect_equal(bold_label(value = 123, value.name = "n", output.type = "latex"), + "\\mathbf{n} = 123.") + expect_equal(bold_label(value = 123, value.name = "n", output.type = "tex"), + "\\mathbf{n} = 123.") + expect_equal(bold_label(value = 123, value.name = "n", output.type = "tikz"), + "\\mathbf{n} = 123.") +}) + +test_that("well-formatted P-value labels", { + expect_equal(p_value_label(value = 0.5), + "italic(P)~`=`~\"0.5000\"") + expect_equal(p_value_label(value = 0.5, output.type = "expression"), + "italic(P)~`=`~\"0.5000\"") + expect_equal(p_value_label(value = 0.5, digits = 2, output.type = "expression"), + "italic(P)~`=`~\"0.50\"") + expect_equal(p_value_label(value = 0.5, digits = 5, output.type = "expression"), + "italic(P)~`=`~\"5.00000\" %*% 10^{\"−01\"}") + expect_equal(p_value_label(value = 0.5, digits = Inf, output.type = "expression"), + "italic(P)~`=`~\"0.500\"") + expect_equal(p_value_label(value = 0.5, output.type = "expression", small.p = TRUE), + "italic(p)~`=`~\"0.5000\"") + expect_warning(p_value_label(value = 0.5, output.type = "expression", digits = 1)) + expect_warning(p_value_label(value = 1.1)) +}) + +test_that("well-formatted F-value labels", { + expect_equal(f_value_label(value = 1e2), + "italic(F)~`=`~\"100.0\"") + expect_equal(f_value_label(value = 100, output.type = "expression"), + "italic(F)~`=`~\"100.0\"") + expect_equal(f_value_label(value = 100, digits = 2, output.type = "expression"), + "italic(F)~`=`~\"1.0\" %*% 10^{\"+02\"}") + expect_equal(f_value_label(value = 100, digits = 5, output.type = "expression"), + "italic(F)~`=`~\"100.00\"") + expect_equal(f_value_label(value = 100, digits = Inf, output.type = "expression"), + "italic(F)~`=`~\"1.0\" %*% 10^{\"+02\"}") +}) + +test_that("well-formatted t-value labels", { + expect_equal(t_value_label(value = 12), + "italic(t)~`=`~\"12.00\"") + expect_equal(t_value_label(value = 12, output.type = "expression"), + "italic(t)~`=`~\"12.00\"") + expect_equal(t_value_label(value = 100, df = 10, output.type = "expression"), + "italic(t)[10]~`=`~\"100.0\"") + expect_equal(t_value_label(value = 100, digits = 2, output.type = "expression"), + "italic(t)~`=`~\"1.0\" %*% 10^{\"+02\"}") + expect_equal(t_value_label(value = 100, digits = 5, output.type = "expression"), + "italic(t)~`=`~\"100.00\"") + expect_equal(t_value_label(value = 100, digits = Inf, output.type = "expression"), + "italic(t)~`=`~\"1.0\" %*% 10^{\"+02\"}") + expect_equal(t_value_label(value = 100, df = 10, digits = Inf, output.type = "expression"), + "italic(t)[10]~`=`~\"1.0\" %*% 10^{\"+02\"}") +}) + +test_that("well-formatted sd labels", { + expect_equal(sd_value_label(value = 12), + "italic(sigma)~`=`~\"12.00\"") +}) + +test_that("well-formatted var labels", { + expect_equal(var_value_label(value = 12), + "italic(sigma^2)~`=`~\"12.00\"") +}) + +test_that("well-formatted mean labels", { + expect_equal(mean_value_label(value = 12), + "italic(bar(x))~`=`~\"12.00\"") +}) + +test_that("well-formatted R labels", { + expect_equal(r_label(value = 0.5), + "italic(R)~`=`~\"0.500\"") + expect_equal(r_label(value = 0.5, method = "pearson"), + "italic(R)~`=`~\"0.500\"") + expect_equal(r_label(value = 0.5, method = "kendall"), + "italic(tau)~`=`~\"0.500\"") + expect_equal(r_label(value = 0.5, method = "spearman"), + "italic(rho)~`=`~\"0.500\"") + expect_equal(r_label(value = -0.5), + "italic(R)~`=`~\"-0.500\"") + expect_equal(r_label(value = 0.5, output.type = "expression"), + "italic(R)~`=`~\"0.500\"") + expect_equal(r_label(value = 0.5, digits = 2, output.type = "expression"), + "italic(R)~`=`~\"0.50\"") + expect_equal(r_label(value = 0.5, digits = 5, output.type = "expression"), + "italic(R)~`=`~\"0.50000\"") + expect_equal(r_label(value = 0.5, digits = Inf, output.type = "expression"), + "italic(R)~`=`~\"0.50\"") + expect_equal(r_label(value = 0.5, output.type = "expression", small.r = TRUE), + "italic(r)~`=`~\"0.500\"") + expect_warning(r_label(value = 0.5, output.type = "expression", digits = 1)) + expect_warning(r_label(value = 1.1)) + expect_true(is.na(suppressWarnings(r_label(value = 1.1)))) +}) + +test_that("well-formatted R2 labels", { + expect_equal(rr_label(value = 0.5), + "italic(R)^2~`=`~\"0.500\"") + expect_equal(rr_label(value = 0.5, output.type = "expression"), + "italic(R)^2~`=`~\"0.500\"") + expect_equal(rr_label(value = 0.5, digits = 2, output.type = "expression"), + "italic(R)^2~`=`~\"0.50\"") + expect_equal(rr_label(value = 0.5, digits = 5, output.type = "expression"), + "italic(R)^2~`=`~\"0.50000\"") + expect_equal(rr_label(value = 0.5, digits = Inf, output.type = "expression"), + "italic(R)^2~`=`~\"0.50\"") + expect_equal(rr_label(value = 0.5, output.type = "expression", small.r = TRUE), + "italic(r)^2~`=`~\"0.500\"") + expect_warning(rr_label(value = 0.5, output.type = "expression", digits = 1)) + expect_warning(rr_label(value = 1.1)) + expect_warning(rr_label(value = -0.1)) + expect_true(is.na(suppressWarnings(rr_label(value = 1.1)))) + expect_true(is.na(suppressWarnings(rr_label(value = -0.1)))) +}) + +test_that("well-formatted adjusted R2 labels", { + expect_equal(adj_rr_label(value = 0.5), + "italic(R)[adj]^2~`=`~\"0.500\"") + expect_equal(adj_rr_label(value = 0.5, output.type = "expression"), + "italic(R)[adj]^2~`=`~\"0.500\"") + expect_warning(adj_rr_label(value = 0.5, output.type = "expression", digits = 1)) + expect_warning(adj_rr_label(value = 1.1)) + expect_no_warning(adj_rr_label(value = -0.1)) + expect_true(is.na(suppressWarnings(adj_rr_label(value = 1.1)))) + expect_false(is.na(suppressWarnings(adj_rr_label(value = -0.1)))) +}) + +test_that("well-formatted R2 CI labels", { + expect_equal(rr_ci_label(value = c(0.5, 0.7), conf.level = .95), + "\"95% CI [0.50, 0.70]\"") + expect_equal(rr_ci_label(value = c(0.5, 0.7), conf.level = .95, range.brackets = c("(", ")")), + "\"95% CI (0.50, 0.70)\"") +}) + +test_that("well-formatted R CI labels", { + expect_equal(r_ci_label(value = c(0.5, 0.7), conf.level = .95), + "\"95% CI [0.50, 0.70]\"") + expect_equal(r_ci_label(value = c(0.5, 0.7), conf.level = .95, range.brackets = c("(", ")")), + "\"95% CI (0.50, 0.70)\"") +}) +