From 18dc6facf41ef03d775456a498e132ab3669533b Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 7 Sep 2025 15:43:51 -0400 Subject: [PATCH 01/11] sanitize_axes --- R/sanitize.R | 28 ++++++++++++++++++++++++++++ R/tinyplot.R | 24 ++---------------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/R/sanitize.R b/R/sanitize.R index d51be159..5d4aa811 100644 --- a/R/sanitize.R +++ b/R/sanitize.R @@ -109,3 +109,31 @@ sanitize_type = function(type, x, y, dots) { out = list(draw = NULL, data = NULL, name = type) return(out) } + + + +sanitize_axes = function(axes, xaxt, yaxt, frame.plot) { + ## handle defaults of axes, xaxt, yaxt, frame.plot + ## - convert axes to character if necessary + ## - set defaults of xaxt/yaxt (if these are NULL) based on axes + ## - set logical axes based on xaxt/yaxt + ## - set frame.plot default based on xaxt/yaxt + if (isFALSE(axes)) { + axes = xaxt = yaxt = "none" + } else if (isTRUE(axes)) { + axes = "standard" + if (is.null(xaxt)) xaxt = get_tpar("xaxt", default = "standard") + if (is.null(yaxt)) yaxt = get_tpar("yaxt", default = "standard") + } else { + xaxt = yaxt = axes + } + axis_types = c("standard", "none", "labels", "ticks", "axis") + axes = match.arg(axes, axis_types) + xaxt = match.arg(xaxt, axis_types) + yaxt = match.arg(yaxt, axis_types) + xaxt = substr(match.arg(xaxt, axis_types), 1L, 1L) + yaxt = substr(match.arg(yaxt, axis_types), 1L, 1L) + axes = any(c(xaxt, yaxt) != "n") + if (is.null(frame.plot) || !is.logical(frame.plot)) frame.plot = all(c(xaxt, yaxt) %in% c("s", "a")) + return(list(axes = axes, xaxt = xaxt, yaxt = yaxt, frame.plot = frame.plot)) +} diff --git a/R/tinyplot.R b/R/tinyplot.R index aeade2ba..c32ee013 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -697,28 +697,8 @@ tinyplot.default = function( # will be overwritten by some type_data() functions and ignored by others ribbon.alpha = sanitize_ribbon.alpha(NULL) - ## handle defaults of axes, xaxt, yaxt, frame.plot - ## - convert axes to character if necessary - ## - set defaults of xaxt/yaxt (if these are NULL) based on axes - ## - set logical axes based on xaxt/yaxt - ## - set frame.plot default based on xaxt/yaxt - if (isFALSE(axes)) { - axes = xaxt = yaxt = "none" - } else if (isTRUE(axes)) { - axes = "standard" - if (is.null(xaxt)) xaxt = get_tpar("xaxt", default = "standard") - if (is.null(yaxt)) yaxt = get_tpar("yaxt", default = "standard") - } else { - xaxt = yaxt = axes - } - axis_types = c("standard", "none", "labels", "ticks", "axis") - axes = match.arg(axes, axis_types) - xaxt = match.arg(xaxt, axis_types) - yaxt = match.arg(yaxt, axis_types) - xaxt = substr(match.arg(xaxt, axis_types), 1L, 1L) - yaxt = substr(match.arg(yaxt, axis_types), 1L, 1L) - axes = any(c(xaxt, yaxt) != "n") - if (is.null(frame.plot) || !is.logical(frame.plot)) frame.plot = all(c(xaxt, yaxt) %in% c("s", "a")) + tmp = sanitize_axes(axes, xaxt, yaxt, frame.plot) + list2env(tmp, environment()) # Write plot to output file or window with fixed dimensions setup_device(file = file, width = width, height = height) From aeac9f3822db57516abfee523908526c2053404d Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 7 Sep 2025 17:05:08 -0400 Subject: [PATCH 02/11] sanitize_xlab() --- R/sanitize_xlab.R | 23 +++++++++++++++++++++++ R/tinyplot.R | 14 ++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 R/sanitize_xlab.R diff --git a/R/sanitize_xlab.R b/R/sanitize_xlab.R new file mode 100644 index 00000000..37bf5020 --- /dev/null +++ b/R/sanitize_xlab.R @@ -0,0 +1,23 @@ +sanitize_xlab = function(xlab = NULL, x_dep = NULL, xmin_dep = NULL, xmax_dep = NULL, type = NULL, y = NULL) { + out = NULL + + if (!is.null(xlab)) { + out = xlab + } else { + out = x_dep + } + + if (is.null(out)) { + if (!is.null(xmin_dep) && !is.null(xmax_dep)) { + out = sprintf("[%s, %s]", xmin_dep, xmax_dep) + } else if (is.null(y)) { + if (identical(type, "boxplot")) { + out = "" + } else if (!type %in% c("histogram", "barplot")) { + out = "Index" + } + } + } + + return(out) +} diff --git a/R/tinyplot.R b/R/tinyplot.R index c32ee013..307163fd 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -736,6 +736,8 @@ tinyplot.default = function( } else { deparse1(substitute(y)) } + xmin_dep = deparse1(substitute(xmin)) + xmax_dep = deparse1(substitute(xmax)) by_dep = deparse1(substitute(by)) null_by = is.null(by) cex_dep = if (!is.null(cex)) deparse1(substitute(cex)) else NULL @@ -764,12 +766,15 @@ tinyplot.default = function( facet_attr = attributes(facet) ## TODO: better solution for restoring facet attributes? null_facet = is.null(facet) + xlab = sanitize_xlab( + xlab = xlab, type = type, y = y, + x_dep = x_dep, xmin_dep, xmax_dep) + + # ylab = sanitize_ylab(ylab = ylab, y_dep = y_dep, type = type, y = y) + if (is.null(x)) { ## Special catch for rect and segment plots without a specified y-var if (type %in% c("rect", "segments")) { - xmin_dep = deparse(substitute(xmin)) - xmax_dep = deparse(substitute(xmax)) - x_dep = paste0("[", xmin_dep, ", ", xmax_dep, "]") x = rep(NA, length(x)) } } @@ -787,16 +792,13 @@ tinyplot.default = function( } else if (type == "boxplot") { y = x x = rep.int("", length(y)) - xlab = "" xaxt = "a" } else if (!(type %in% c("histogram", "barplot"))) { y = x x = seq_along(x) - if (is.null(xlab)) xlab = "Index" } } - if (is.null(xlab)) xlab = x_dep if (is.null(ylab) && type != "histogram") ylab = y_dep # flag(s) indicating whether x/ylim was set by the user (needed later for From 5d108cf298df3f1d198beadfa670109095277d8d Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 7 Sep 2025 18:05:58 -0400 Subject: [PATCH 03/11] xmin_dep + xmax_dep --- R/tinyplot.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/tinyplot.R b/R/tinyplot.R index 307163fd..76b32a57 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -736,8 +736,8 @@ tinyplot.default = function( } else { deparse1(substitute(y)) } - xmin_dep = deparse1(substitute(xmin)) - xmax_dep = deparse1(substitute(xmax)) + xmin_dep = if (is.null(xmin)) NULL else deparse1(substitute(xmin)) + xmax_dep = if (is.null(xmax)) NULL else deparse1(substitute(xmax)) by_dep = deparse1(substitute(by)) null_by = is.null(by) cex_dep = if (!is.null(cex)) deparse1(substitute(cex)) else NULL From 1252bf9abe61dacf2bd5bb5669ad24f1dc251812 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 7 Sep 2025 20:53:33 -0400 Subject: [PATCH 04/11] sanitize_xylab.R --- R/sanitize_xlab.R | 23 ---------------------- R/sanitize_xylab.R | 49 ++++++++++++++++++++++++++++++++++++++++++++++ R/tinyplot.R | 38 ++++++++++++----------------------- 3 files changed, 61 insertions(+), 49 deletions(-) delete mode 100644 R/sanitize_xlab.R create mode 100644 R/sanitize_xylab.R diff --git a/R/sanitize_xlab.R b/R/sanitize_xlab.R deleted file mode 100644 index 37bf5020..00000000 --- a/R/sanitize_xlab.R +++ /dev/null @@ -1,23 +0,0 @@ -sanitize_xlab = function(xlab = NULL, x_dep = NULL, xmin_dep = NULL, xmax_dep = NULL, type = NULL, y = NULL) { - out = NULL - - if (!is.null(xlab)) { - out = xlab - } else { - out = x_dep - } - - if (is.null(out)) { - if (!is.null(xmin_dep) && !is.null(xmax_dep)) { - out = sprintf("[%s, %s]", xmin_dep, xmax_dep) - } else if (is.null(y)) { - if (identical(type, "boxplot")) { - out = "" - } else if (!type %in% c("histogram", "barplot")) { - out = "Index" - } - } - } - - return(out) -} diff --git a/R/sanitize_xylab.R b/R/sanitize_xylab.R new file mode 100644 index 00000000..c59dd898 --- /dev/null +++ b/R/sanitize_xylab.R @@ -0,0 +1,49 @@ +sanitize_xylab <- function( + x, xlab = NULL, x_dep = NULL, xmin_dep = NULL, xmax_dep = NULL, + y, ylab = NULL, y_dep = NULL, ymin_dep = NULL, ymax_dep = NULL, + type = NULL) { + out_xlab = NULL + out_ylab = NULL + + ##### xlab + if (!is.null(xlab)) { + out_xlab = xlab + } else { + out_xlab = x_dep + } + + if (is.null(out_xlab)) { + if (!is.null(xmin_dep) && !is.null(xmax_dep)) { + out_xlab = sprintf("[%s, %s]", xmin_dep, xmax_dep) + } else if (is.null(y)) { + if (identical(type, "boxplot")) { + out_xlab = "" + } else if (!type %in% c("histogram", "barplot")) { + out_xlab = "Index" + } + } + } + + ##### ylab + is_density = type %in% c("density") + is_frequency = type %in% c("function", "histogram", "barplot") + is_range = type %in% c("rect", "segments", "pointrange", "errorbar", "ribbon") + if (!is.null(ylab)) { + out_ylab = ylab + } else if (is_frequency && is.null(y) && !is.null(x)) { + out_ylab = "Frequency" + } else if (is_density && is.null(y) && !is.null(x)) { + out_ylab = "Density" + } else if (is_range && !is.null(ymin_dep) && !is.null(ymax_dep)) { + out_ylab = sprintf("[%s, %s]", ymin_dep, ymax_dep) + } else if (!is.null(y_dep)) { + out_ylab = y_dep + } else if (is.null(y) && !is.null(out_xlab)) { + out_ylab = out_xlab + } else { + out_ylab = NULL + } + + out <- list(xlab = out_xlab, ylab = out_ylab) + return(out) +} diff --git a/R/tinyplot.R b/R/tinyplot.R index 76b32a57..e0a41977 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -725,19 +725,14 @@ tinyplot.default = function( } # Capture deparsed expressions early, before x, y and by are evaluated - x_dep = if (!is.null(x)) { - deparse1(substitute(x)) - } else if (type %in% c("rect", "segments")) { - x = NULL - NULL - } - y_dep = if (is.null(y)) { - deparse1(substitute(x)) - } else { - deparse1(substitute(y)) - } + x_dep = if (is.null(x)) NULL else deparse1(substitute(x)) xmin_dep = if (is.null(xmin)) NULL else deparse1(substitute(xmin)) xmax_dep = if (is.null(xmax)) NULL else deparse1(substitute(xmax)) + + y_dep = if (is.null(y)) NULL else deparse1(substitute(y)) + ymin_dep = if (is.null(ymin)) NULL else deparse1(substitute(ymin)) + ymax_dep = if (is.null(ymax)) NULL else deparse1(substitute(ymax)) + by_dep = deparse1(substitute(by)) null_by = is.null(by) cex_dep = if (!is.null(cex)) deparse1(substitute(cex)) else NULL @@ -766,11 +761,11 @@ tinyplot.default = function( facet_attr = attributes(facet) ## TODO: better solution for restoring facet attributes? null_facet = is.null(facet) - xlab = sanitize_xlab( - xlab = xlab, type = type, y = y, - x_dep = x_dep, xmin_dep, xmax_dep) - - # ylab = sanitize_ylab(ylab = ylab, y_dep = y_dep, type = type, y = y) + tmp = sanitize_xylab( + x = x, xlab = xlab, x_dep = x_dep, xmin_dep = xmin_dep, xmax_dep = xmax_dep, + y = y, ylab = ylab, y_dep = y_dep, ymin_dep = ymin_dep, ymax_dep = ymax_dep, + type = type) + list2env(tmp, environment()) if (is.null(x)) { ## Special catch for rect and segment plots without a specified y-var @@ -781,25 +776,16 @@ tinyplot.default = function( if (is.null(y)) { ## Special catch for area and interval plots without a specified y-var if (type %in% c("rect", "segments", "pointrange", "errorbar", "ribbon")) { - ymin_dep = deparse(substitute(ymin)) - ymax_dep = deparse(substitute(ymax)) - y_dep = paste0("[", ymin_dep, ", ", ymax_dep, "]") y = rep(NA, length(x)) - } else if (type == "density") { - if (is.null(ylab)) ylab = "Density" - } else if (type == "function") { - if (is.null(ylab)) ylab = "Frequency" } else if (type == "boxplot") { y = x x = rep.int("", length(y)) xaxt = "a" - } else if (!(type %in% c("histogram", "barplot"))) { + } else if (!(type %in% c("histogram", "barplot", "density"))) { y = x x = seq_along(x) } } - - if (is.null(ylab) && type != "histogram") ylab = y_dep # flag(s) indicating whether x/ylim was set by the user (needed later for # special case where facets are free but still want to set x/ylim manually) From ac4cab587ca324a698e1a6305771b6bd97a5a523 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 7 Sep 2025 21:12:45 -0400 Subject: [PATCH 05/11] xlabels --- R/tinyplot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/tinyplot.R b/R/tinyplot.R index e0a41977..61180ea6 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -781,7 +781,7 @@ tinyplot.default = function( y = x x = rep.int("", length(y)) xaxt = "a" - } else if (!(type %in% c("histogram", "barplot", "density"))) { + } else if (!(type %in% c("histogram", "barplot", "density", "function"))) { y = x x = seq_along(x) } From ce203d809b08fc908ba37f48f0eeaf7f26f9bceb Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 7 Sep 2025 21:23:18 -0400 Subject: [PATCH 06/11] ribbon ylab is deparsed `y` ahead of [lwr,upr] --- R/sanitize_xylab.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/R/sanitize_xylab.R b/R/sanitize_xylab.R index c59dd898..f93905ed 100644 --- a/R/sanitize_xylab.R +++ b/R/sanitize_xylab.R @@ -27,13 +27,20 @@ sanitize_xylab <- function( ##### ylab is_density = type %in% c("density") is_frequency = type %in% c("function", "histogram", "barplot") - is_range = type %in% c("rect", "segments", "pointrange", "errorbar", "ribbon") + is_range = type %in% c("rect", "segments", "pointrange", "ribbon") + is_ribbon = type %in% c("ribbon") if (!is.null(ylab)) { out_ylab = ylab } else if (is_frequency && is.null(y) && !is.null(x)) { out_ylab = "Frequency" } else if (is_density && is.null(y) && !is.null(x)) { out_ylab = "Density" + } else if (is_ribbon) { + if (!is.null(y_dep)) { + out_ylab = y_dep + } else if (!is.null(ymin_dep) && !is.null(ymax_dep)) { + out_ylab = sprintf("[%s, %s]", ymin_dep, ymax_dep) + } } else if (is_range && !is.null(ymin_dep) && !is.null(ymax_dep)) { out_ylab = sprintf("[%s, %s]", ymin_dep, ymax_dep) } else if (!is.null(y_dep)) { From b7ab5f30c1957142095814cb24a8c7da679ea31b Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 7 Sep 2025 21:25:11 -0400 Subject: [PATCH 07/11] update snapshots --- inst/tinytest/_tinysnapshot/bubble_dual_continuous.svg | 9 --------- inst/tinytest/_tinysnapshot/bubble_dual_discrete.svg | 9 --------- inst/tinytest/_tinysnapshot/bubble_dual_fancy.svg | 9 --------- inst/tinytest/_tinysnapshot/readme_base_1.svg | 2 +- inst/tinytest/_tinysnapshot/tpar_grid.svg | 2 +- inst/tinytest/_tinysnapshot/type_c.svg | 2 +- inst/tinytest/_tinysnapshot/type_cap_s.svg | 2 +- inst/tinytest/_tinysnapshot/type_h.svg | 2 +- inst/tinytest/_tinysnapshot/type_s.svg | 2 +- inst/tinytest/_tinysnapshot/ylab_good.svg | 2 +- 10 files changed, 7 insertions(+), 34 deletions(-) diff --git a/inst/tinytest/_tinysnapshot/bubble_dual_continuous.svg b/inst/tinytest/_tinysnapshot/bubble_dual_continuous.svg index ea394222..97c60af4 100644 --- a/inst/tinytest/_tinysnapshot/bubble_dual_continuous.svg +++ b/inst/tinytest/_tinysnapshot/bubble_dual_continuous.svg @@ -26,15 +26,6 @@ - - - - - - - - - 2.0 2.5 diff --git a/inst/tinytest/_tinysnapshot/bubble_dual_discrete.svg b/inst/tinytest/_tinysnapshot/bubble_dual_discrete.svg index fd762626..5bb9f39c 100644 --- a/inst/tinytest/_tinysnapshot/bubble_dual_discrete.svg +++ b/inst/tinytest/_tinysnapshot/bubble_dual_discrete.svg @@ -26,15 +26,6 @@ - - - - - - - - - diff --git a/inst/tinytest/_tinysnapshot/bubble_dual_fancy.svg b/inst/tinytest/_tinysnapshot/bubble_dual_fancy.svg index 2bf07612..047c7651 100644 --- a/inst/tinytest/_tinysnapshot/bubble_dual_fancy.svg +++ b/inst/tinytest/_tinysnapshot/bubble_dual_fancy.svg @@ -26,15 +26,6 @@ - - - - - - - - - 2.0 2.5 diff --git a/inst/tinytest/_tinysnapshot/readme_base_1.svg b/inst/tinytest/_tinysnapshot/readme_base_1.svg index 315550f2..21a742cf 100644 --- a/inst/tinytest/_tinysnapshot/readme_base_1.svg +++ b/inst/tinytest/_tinysnapshot/readme_base_1.svg @@ -96,7 +96,7 @@ tinyplot -Index +0:10 0:10 diff --git a/inst/tinytest/_tinysnapshot/tpar_grid.svg b/inst/tinytest/_tinysnapshot/tpar_grid.svg index fad98092..e75f499c 100644 --- a/inst/tinytest/_tinysnapshot/tpar_grid.svg +++ b/inst/tinytest/_tinysnapshot/tpar_grid.svg @@ -26,7 +26,7 @@ -Index +0:10 0:10 diff --git a/inst/tinytest/_tinysnapshot/type_c.svg b/inst/tinytest/_tinysnapshot/type_c.svg index 96a7559c..f367b80b 100644 --- a/inst/tinytest/_tinysnapshot/type_c.svg +++ b/inst/tinytest/_tinysnapshot/type_c.svg @@ -26,7 +26,7 @@ -Index +0:10 0:10 diff --git a/inst/tinytest/_tinysnapshot/type_cap_s.svg b/inst/tinytest/_tinysnapshot/type_cap_s.svg index af96ad24..5f5b5d5c 100644 --- a/inst/tinytest/_tinysnapshot/type_cap_s.svg +++ b/inst/tinytest/_tinysnapshot/type_cap_s.svg @@ -26,7 +26,7 @@ -Index +0:10 0:10 diff --git a/inst/tinytest/_tinysnapshot/type_h.svg b/inst/tinytest/_tinysnapshot/type_h.svg index 1280c024..2234f575 100644 --- a/inst/tinytest/_tinysnapshot/type_h.svg +++ b/inst/tinytest/_tinysnapshot/type_h.svg @@ -26,7 +26,7 @@ -Index +0:10 0:10 diff --git a/inst/tinytest/_tinysnapshot/type_s.svg b/inst/tinytest/_tinysnapshot/type_s.svg index 46008843..a0d494bb 100644 --- a/inst/tinytest/_tinysnapshot/type_s.svg +++ b/inst/tinytest/_tinysnapshot/type_s.svg @@ -26,7 +26,7 @@ -Index +0:10 0:10 diff --git a/inst/tinytest/_tinysnapshot/ylab_good.svg b/inst/tinytest/_tinysnapshot/ylab_good.svg index a663b3a2..a091b81b 100644 --- a/inst/tinytest/_tinysnapshot/ylab_good.svg +++ b/inst/tinytest/_tinysnapshot/ylab_good.svg @@ -26,7 +26,7 @@ -Index +rnorm(1) rnorm(1) From b4e8f5a8c7c2c0faf05c500867c26cf311592fe5 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 7 Sep 2025 22:18:33 -0400 Subject: [PATCH 08/11] restore snapshots from main + Index xlab --- R/sanitize_xylab.R | 36 +- R/sanitize_xylab.html | 462 ++++++++++++++++++ .../_tinysnapshot/bubble_dual_continuous.svg | 9 + .../_tinysnapshot/bubble_dual_discrete.svg | 9 + .../_tinysnapshot/bubble_dual_fancy.svg | 9 + inst/tinytest/_tinysnapshot/readme_base_1.svg | 2 +- inst/tinytest/_tinysnapshot/tpar_grid.svg | 2 +- inst/tinytest/_tinysnapshot/type_c.svg | 2 +- inst/tinytest/_tinysnapshot/type_cap_s.svg | 2 +- inst/tinytest/_tinysnapshot/type_h.svg | 2 +- inst/tinytest/_tinysnapshot/type_s.svg | 2 +- inst/tinytest/_tinysnapshot/ylab_good.svg | 2 +- 12 files changed, 513 insertions(+), 26 deletions(-) create mode 100644 R/sanitize_xylab.html diff --git a/R/sanitize_xylab.R b/R/sanitize_xylab.R index f93905ed..3f3edeb2 100644 --- a/R/sanitize_xylab.R +++ b/R/sanitize_xylab.R @@ -5,30 +5,28 @@ sanitize_xylab <- function( out_xlab = NULL out_ylab = NULL + is_boxplot = type %in% c("boxplot") + is_density = type %in% c("density") + is_frequency = type %in% c("histogram", "barplot", "function") + is_function = type %in% c("function") + is_range = type %in% c("rect", "segments", "pointrange") + is_ribbon = type %in% c("ribbon") + is_index = !is_frequency && !is_ribbon && !is_density + ##### xlab if (!is.null(xlab)) { out_xlab = xlab + } else if (!is.null(xmin_dep) && !is.null(xmax_dep)) { + out_xlab = sprintf("[%s, %s]", xmin_dep, xmax_dep) + } else if (is_boxplot && is.null(y)) { + out_xlab = "" + } else if (is_index && is.null(y) && !is.null(x)) { + out_xlab = "Index" } else { out_xlab = x_dep } - if (is.null(out_xlab)) { - if (!is.null(xmin_dep) && !is.null(xmax_dep)) { - out_xlab = sprintf("[%s, %s]", xmin_dep, xmax_dep) - } else if (is.null(y)) { - if (identical(type, "boxplot")) { - out_xlab = "" - } else if (!type %in% c("histogram", "barplot")) { - out_xlab = "Index" - } - } - } - ##### ylab - is_density = type %in% c("density") - is_frequency = type %in% c("function", "histogram", "barplot") - is_range = type %in% c("rect", "segments", "pointrange", "ribbon") - is_ribbon = type %in% c("ribbon") if (!is.null(ylab)) { out_ylab = ylab } else if (is_frequency && is.null(y) && !is.null(x)) { @@ -41,12 +39,12 @@ sanitize_xylab <- function( } else if (!is.null(ymin_dep) && !is.null(ymax_dep)) { out_ylab = sprintf("[%s, %s]", ymin_dep, ymax_dep) } - } else if (is_range && !is.null(ymin_dep) && !is.null(ymax_dep)) { + } else if ((is_range || is_ribbon) && !is.null(ymin_dep) && !is.null(ymax_dep)) { out_ylab = sprintf("[%s, %s]", ymin_dep, ymax_dep) } else if (!is.null(y_dep)) { out_ylab = y_dep - } else if (is.null(y) && !is.null(out_xlab)) { - out_ylab = out_xlab + } else if (is.null(y) && !is.null(x_dep)) { + out_ylab = x_dep } else { out_ylab = NULL } diff --git a/R/sanitize_xylab.html b/R/sanitize_xylab.html new file mode 100644 index 00000000..1ba53b8a --- /dev/null +++ b/R/sanitize_xylab.html @@ -0,0 +1,462 @@ + + + + + + + + + + + + + + + +sanitize_xylab.R + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
sanitize_xylab <- function(
+    x, xlab = NULL, x_dep = NULL, xmin_dep = NULL, xmax_dep = NULL,
+    y, ylab = NULL, y_dep = NULL, ymin_dep = NULL, ymax_dep = NULL,
+    type = NULL) {
+  out_xlab = NULL
+  out_ylab = NULL
+
+  ##### xlab
+  is_boxplot = type %in% c("boxplot")
+  is_frequency = type %in% c("histogram", "barplot", "density")
+  is_ribbon = type %in% c("ribbon")
+  is_function = type %in% c("function")
+  is_index = !is_frequency && !is_ribbon && !is_function
+  if (!is.null(xlab)) {
+    out_xlab = xlab
+  } else if (!is.null(xmin_dep) && !is.null(xmax_dep)) {
+    out_xlab = sprintf("[%s, %s]", xmin_dep, xmax_dep)
+  } else if (is_boxplot && is.null(y)) {
+    out_xlab = ""
+  } else if (is_index && is.null(y) && !is.null(x)) {
+    out_xlab = "Index"
+  } else {
+    out_xlab = x_dep
+  }
+
+  ##### ylab
+  is_density = type %in% c("density")
+  is_frequency = type %in% c("function", "histogram", "barplot")
+  is_range = type %in% c("rect", "segments", "pointrange", "ribbon")
+  is_ribbon = type %in% c("ribbon")
+  if (!is.null(ylab)) {
+    out_ylab = ylab
+  } else if (is_frequency && is.null(y) && !is.null(x)) {
+    out_ylab = "Frequency"
+  } else if (is_density && is.null(y) && !is.null(x)) {
+    out_ylab = "Density"
+  } else if (is_ribbon) {
+    if (!is.null(y_dep)) {
+      out_ylab = y_dep
+    } else if (!is.null(ymin_dep) && !is.null(ymax_dep)) {
+      out_ylab = sprintf("[%s, %s]", ymin_dep, ymax_dep)
+    }
+  } else if (is_range && !is.null(ymin_dep) && !is.null(ymax_dep)) {
+    out_ylab = sprintf("[%s, %s]", ymin_dep, ymax_dep)
+  } else if (!is.null(y_dep)) {
+    out_ylab = y_dep
+  } else if (is.null(y) && !is.null(x_dep)) {
+    out_ylab = x_dep
+  } else {
+    out_ylab = NULL
+  }
+
+  out <- list(xlab = out_xlab, ylab = out_ylab)
+  return(out)
+}
+ + + + +
+ + + + + + + + + + + + + + + diff --git a/inst/tinytest/_tinysnapshot/bubble_dual_continuous.svg b/inst/tinytest/_tinysnapshot/bubble_dual_continuous.svg index 97c60af4..ea394222 100644 --- a/inst/tinytest/_tinysnapshot/bubble_dual_continuous.svg +++ b/inst/tinytest/_tinysnapshot/bubble_dual_continuous.svg @@ -26,6 +26,15 @@ + + + + + + + + + 2.0 2.5 diff --git a/inst/tinytest/_tinysnapshot/bubble_dual_discrete.svg b/inst/tinytest/_tinysnapshot/bubble_dual_discrete.svg index 5bb9f39c..fd762626 100644 --- a/inst/tinytest/_tinysnapshot/bubble_dual_discrete.svg +++ b/inst/tinytest/_tinysnapshot/bubble_dual_discrete.svg @@ -26,6 +26,15 @@ + + + + + + + + + diff --git a/inst/tinytest/_tinysnapshot/bubble_dual_fancy.svg b/inst/tinytest/_tinysnapshot/bubble_dual_fancy.svg index 047c7651..2bf07612 100644 --- a/inst/tinytest/_tinysnapshot/bubble_dual_fancy.svg +++ b/inst/tinytest/_tinysnapshot/bubble_dual_fancy.svg @@ -26,6 +26,15 @@ + + + + + + + + + 2.0 2.5 diff --git a/inst/tinytest/_tinysnapshot/readme_base_1.svg b/inst/tinytest/_tinysnapshot/readme_base_1.svg index 21a742cf..315550f2 100644 --- a/inst/tinytest/_tinysnapshot/readme_base_1.svg +++ b/inst/tinytest/_tinysnapshot/readme_base_1.svg @@ -96,7 +96,7 @@ tinyplot -0:10 +Index 0:10 diff --git a/inst/tinytest/_tinysnapshot/tpar_grid.svg b/inst/tinytest/_tinysnapshot/tpar_grid.svg index e75f499c..fad98092 100644 --- a/inst/tinytest/_tinysnapshot/tpar_grid.svg +++ b/inst/tinytest/_tinysnapshot/tpar_grid.svg @@ -26,7 +26,7 @@ -0:10 +Index 0:10 diff --git a/inst/tinytest/_tinysnapshot/type_c.svg b/inst/tinytest/_tinysnapshot/type_c.svg index f367b80b..96a7559c 100644 --- a/inst/tinytest/_tinysnapshot/type_c.svg +++ b/inst/tinytest/_tinysnapshot/type_c.svg @@ -26,7 +26,7 @@ -0:10 +Index 0:10 diff --git a/inst/tinytest/_tinysnapshot/type_cap_s.svg b/inst/tinytest/_tinysnapshot/type_cap_s.svg index 5f5b5d5c..af96ad24 100644 --- a/inst/tinytest/_tinysnapshot/type_cap_s.svg +++ b/inst/tinytest/_tinysnapshot/type_cap_s.svg @@ -26,7 +26,7 @@ -0:10 +Index 0:10 diff --git a/inst/tinytest/_tinysnapshot/type_h.svg b/inst/tinytest/_tinysnapshot/type_h.svg index 2234f575..1280c024 100644 --- a/inst/tinytest/_tinysnapshot/type_h.svg +++ b/inst/tinytest/_tinysnapshot/type_h.svg @@ -26,7 +26,7 @@ -0:10 +Index 0:10 diff --git a/inst/tinytest/_tinysnapshot/type_s.svg b/inst/tinytest/_tinysnapshot/type_s.svg index a0d494bb..46008843 100644 --- a/inst/tinytest/_tinysnapshot/type_s.svg +++ b/inst/tinytest/_tinysnapshot/type_s.svg @@ -26,7 +26,7 @@ -0:10 +Index 0:10 diff --git a/inst/tinytest/_tinysnapshot/ylab_good.svg b/inst/tinytest/_tinysnapshot/ylab_good.svg index a091b81b..a663b3a2 100644 --- a/inst/tinytest/_tinysnapshot/ylab_good.svg +++ b/inst/tinytest/_tinysnapshot/ylab_good.svg @@ -26,7 +26,7 @@ -rnorm(1) +Index rnorm(1) From 93bd1e189a2b8450b5fa1ff49db1c8e8dafef1a8 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 7 Sep 2025 22:20:37 -0400 Subject: [PATCH 09/11] cruft --- R/sanitize_xylab.html | 462 ------------------------------------------ 1 file changed, 462 deletions(-) delete mode 100644 R/sanitize_xylab.html diff --git a/R/sanitize_xylab.html b/R/sanitize_xylab.html deleted file mode 100644 index 1ba53b8a..00000000 --- a/R/sanitize_xylab.html +++ /dev/null @@ -1,462 +0,0 @@ - - - - - - - - - - - - - - - -sanitize_xylab.R - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - -
sanitize_xylab <- function(
-    x, xlab = NULL, x_dep = NULL, xmin_dep = NULL, xmax_dep = NULL,
-    y, ylab = NULL, y_dep = NULL, ymin_dep = NULL, ymax_dep = NULL,
-    type = NULL) {
-  out_xlab = NULL
-  out_ylab = NULL
-
-  ##### xlab
-  is_boxplot = type %in% c("boxplot")
-  is_frequency = type %in% c("histogram", "barplot", "density")
-  is_ribbon = type %in% c("ribbon")
-  is_function = type %in% c("function")
-  is_index = !is_frequency && !is_ribbon && !is_function
-  if (!is.null(xlab)) {
-    out_xlab = xlab
-  } else if (!is.null(xmin_dep) && !is.null(xmax_dep)) {
-    out_xlab = sprintf("[%s, %s]", xmin_dep, xmax_dep)
-  } else if (is_boxplot && is.null(y)) {
-    out_xlab = ""
-  } else if (is_index && is.null(y) && !is.null(x)) {
-    out_xlab = "Index"
-  } else {
-    out_xlab = x_dep
-  }
-
-  ##### ylab
-  is_density = type %in% c("density")
-  is_frequency = type %in% c("function", "histogram", "barplot")
-  is_range = type %in% c("rect", "segments", "pointrange", "ribbon")
-  is_ribbon = type %in% c("ribbon")
-  if (!is.null(ylab)) {
-    out_ylab = ylab
-  } else if (is_frequency && is.null(y) && !is.null(x)) {
-    out_ylab = "Frequency"
-  } else if (is_density && is.null(y) && !is.null(x)) {
-    out_ylab = "Density"
-  } else if (is_ribbon) {
-    if (!is.null(y_dep)) {
-      out_ylab = y_dep
-    } else if (!is.null(ymin_dep) && !is.null(ymax_dep)) {
-      out_ylab = sprintf("[%s, %s]", ymin_dep, ymax_dep)
-    }
-  } else if (is_range && !is.null(ymin_dep) && !is.null(ymax_dep)) {
-    out_ylab = sprintf("[%s, %s]", ymin_dep, ymax_dep)
-  } else if (!is.null(y_dep)) {
-    out_ylab = y_dep
-  } else if (is.null(y) && !is.null(x_dep)) {
-    out_ylab = x_dep
-  } else {
-    out_ylab = NULL
-  }
-
-  out <- list(xlab = out_xlab, ylab = out_ylab)
-  return(out)
-}
- - - - -
- - - - - - - - - - - - - - - From 283eaf52df38a0b4bff2615e80241f2a7f10664d Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 7 Sep 2025 23:10:46 -0400 Subject: [PATCH 10/11] style: = vs <- --- R/by_aesthetics.R | 2 +- R/hooks.R | 12 ++++++------ R/sanitize_xylab.R | 4 ++-- R/type_barplot.R | 2 +- R/type_qq.R | 8 ++++---- R/type_text.R | 12 ++++++------ 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/R/by_aesthetics.R b/R/by_aesthetics.R index 7bbca0c6..400b9e87 100755 --- a/R/by_aesthetics.R +++ b/R/by_aesthetics.R @@ -274,7 +274,7 @@ gen_pal_fun = function(pal, gradient = FALSE, alpha = NULL, n = NULL) { by_pch = function(ngrps, type, pch = NULL) { no_pch = FALSE if (identical(type, "text")) { - pch <- rep(15, ngrps) + pch = rep(15, ngrps) } else if (!type %in% c("p", "b", "o", "pointrange", "errorbar", "boxplot", "qq")) { no_pch = TRUE pch = NULL diff --git a/R/hooks.R b/R/hooks.R index 1c4af278..e04fc085 100644 --- a/R/hooks.R +++ b/R/hooks.R @@ -12,10 +12,10 @@ #' a list of functions. #' @param action `"replace"`, `"append"` or `"prepend"` #' @keywords internal -set_hooks <- function(hooks, action = "append") { - old <- list() +set_hooks = function(hooks, action = "append") { + old = list() for (hook_name in names(hooks)) { - old[[hook_name]] <- getHook(hook_name) + old[[hook_name]] = getHook(hook_name) setHook(hook_name, hooks[[hook_name]], action = action) } invisible(old) @@ -23,12 +23,12 @@ set_hooks <- function(hooks, action = "append") { #' @rdname set_hooks #' @keywords internal -remove_hooks <- function(hooks) { +remove_hooks = function(hooks) { for (hook_name in names(hooks)) { - hook <- getHook(hook_name) + hook = getHook(hook_name) if (length(hook) > 0) { for (fun in unlist(hooks[hook_name])) { - hook[sapply(hook, identical, fun)] <- NULL + hook[sapply(hook, identical, fun)] = NULL } } setHook(hook_name, hook, "replace") diff --git a/R/sanitize_xylab.R b/R/sanitize_xylab.R index 3f3edeb2..f1d3600f 100644 --- a/R/sanitize_xylab.R +++ b/R/sanitize_xylab.R @@ -1,4 +1,4 @@ -sanitize_xylab <- function( +sanitize_xylab = function( x, xlab = NULL, x_dep = NULL, xmin_dep = NULL, xmax_dep = NULL, y, ylab = NULL, y_dep = NULL, ymin_dep = NULL, ymax_dep = NULL, type = NULL) { @@ -49,6 +49,6 @@ sanitize_xylab <- function( out_ylab = NULL } - out <- list(xlab = out_xlab, ylab = out_ylab) + out = list(xlab = out_xlab, ylab = out_ylab) return(out) } diff --git a/R/type_barplot.R b/R/type_barplot.R index d6d01d96..cea11c0d 100644 --- a/R/type_barplot.R +++ b/R/type_barplot.R @@ -98,7 +98,7 @@ data_barplot = function(width = 5/6, beside = FALSE, center = FALSE, FUN = NULL, if (anyNA(xlevels) || !all(xlevels %in% levels(datapoints$x))) warning("not all 'xlevels' correspond to levels of 'x'") datapoints$x = factor(datapoints$x, levels = xlevels) } - if (!is.null(xaxlabels)) levels(datapoints$x) <- xaxlabels + if (!is.null(xaxlabels)) levels(datapoints$x) = xaxlabels datapoints = aggregate(datapoints[, "y", drop = FALSE], datapoints[, c("x", "by", "facet")], FUN = FUN, drop = FALSE) datapoints$y[is.na(datapoints$y)] = 0 #FIXME: always?# if (!is.factor(datapoints$by)) datapoints$by = factor(datapoints$by) diff --git a/R/type_qq.R b/R/type_qq.R index cbbbb5ae..7e13f656 100644 --- a/R/type_qq.R +++ b/R/type_qq.R @@ -38,10 +38,10 @@ type_qq = function(distribution = qnorm) { ) if (!is.null(ilty)) { - iy <- quantile(iy, c(0.25, 0.75)) - ix <- quantile(ix, c(0.25, 0.75)) - slope <- diff(iy) / diff(ix) - intercept <- iy[1] - slope * ix[1] + iy = quantile(iy, c(0.25, 0.75)) + ix = quantile(ix, c(0.25, 0.75)) + slope = diff(iy) / diff(ix) + intercept = iy[1] - slope * ix[1] abline(a = intercept, b = slope, lty = ilty, col = icol, lwd = ilwd) } } diff --git a/R/type_text.R b/R/type_text.R index 39a2d72c..3a129393 100644 --- a/R/type_text.R +++ b/R/type_text.R @@ -21,7 +21,7 @@ #' adj = 0 #' ) #' ) -#' +#' #' # to avoid clipping text at the plot region, we can use xpd = NA #' tinyplot(mpg ~ hp | factor(cyl), #' data = mtcars, @@ -47,23 +47,23 @@ type_text = function(labels, adj = NULL, pos = NULL, offset = 0.5, vfont = NULL, data_text = function(labels, clim = c(0.5, 2.5)) { fun = function(datapoints, legend_args, cex = NULL, ...) { if (length(labels) != 1 && length(labels) != nrow(datapoints)) { - msg <- sprintf("`labels` must be of length 1 or %s.", nrow(datapoints)) + msg = sprintf("`labels` must be of length 1 or %s.", nrow(datapoints)) stop(msg, call. = FALSE) } datapoints$labels = labels - + # browser() bubble = FALSE bubble_cex = 1 if (!is.null(cex) && length(cex) == nrow(datapoints)) { - bubble = TRUE + bubble = TRUE ## Identify the pretty break points for our bubble labels bubble_labs = pretty(cex, n = 5) len_labs = length(bubble_labs) # cex = rescale_num(c(bubble_labs, cex), to = clim) - cex = rescale_num(sqrt(c(bubble_labs, cex))/pi, to = clim) + cex = rescale_num(sqrt(c(bubble_labs, cex)) / pi, to = clim) bubble_cex = cex[1:len_labs] - cex = cex[(len_labs+1):length(cex)] + cex = cex[(len_labs + 1):length(cex)] names(bubble_cex) = format(bubble_labs) if (max(clim) > 2.5) { legend_args[["x.intersp"]] = max(clim) / 2.5 From f0628fdeba96f696130a13d30569ad26e1a8e18d Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Mon, 8 Sep 2025 06:17:10 -0400 Subject: [PATCH 11/11] no `tmp` object --- R/tinyplot.R | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/R/tinyplot.R b/R/tinyplot.R index c281a4e5..bba3ff0d 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -695,8 +695,10 @@ tinyplot.default = function( # will be overwritten by some type_data() functions and ignored by others ribbon.alpha = sanitize_ribbon.alpha(NULL) - tmp = sanitize_axes(axes, xaxt, yaxt, frame.plot) - list2env(tmp, environment()) + # axes + list2env( + sanitize_axes(axes, xaxt, yaxt, frame.plot), + environment()) # Write plot to output file or window with fixed dimensions setup_device(file = file, width = width, height = height) @@ -759,11 +761,13 @@ tinyplot.default = function( facet_attr = attributes(facet) ## TODO: better solution for restoring facet attributes? null_facet = is.null(facet) - tmp = sanitize_xylab( - x = x, xlab = xlab, x_dep = x_dep, xmin_dep = xmin_dep, xmax_dep = xmax_dep, - y = y, ylab = ylab, y_dep = y_dep, ymin_dep = ymin_dep, ymax_dep = ymax_dep, - type = type) - list2env(tmp, environment()) + # xlab & ylab + list2env( + sanitize_xylab( + x = x, xlab = xlab, x_dep = x_dep, xmin_dep = xmin_dep, xmax_dep = xmax_dep, + y = y, ylab = ylab, y_dep = y_dep, ymin_dep = ymin_dep, ymax_dep = ymax_dep, + type = type), + environment()) if (is.null(x)) { ## Special catch for rect and segment plots without a specified y-var