From faddcc2ca26bffeb3498e07af437b7e036d025af Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Tue, 30 Jul 2024 17:41:47 -0700 Subject: [PATCH] type = NULL (#179) * switch to type = NULL * run through type sanitizer * redundant linux check * oops - need to keep alias for formula method before passing type sanitizer * docs --- R/sanitize.R | 16 +++++++++----- R/tinyplot.R | 48 ++++++++++++++++++++++++++++++----------- inst/tinytest/helpers.R | 4 ++-- man/tinyplot.Rd | 8 ++++--- 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/R/sanitize.R b/R/sanitize.R index b163cf9a..4dc38e91 100644 --- a/R/sanitize.R +++ b/R/sanitize.R @@ -6,12 +6,18 @@ sanitize_ribbon.alpha = function(ribbon.alpha) { - sanitize_type = function(type, x, y) { - # enforce boxplot type for y ~ factor(x) - if (!is.null(x) && is.factor(x) && !is.factor(y) && !identical(type, "boxplot")) { - type = "boxplot" - warning('The `type` argument was changed to "boxplot" automatically because `x` is a factor but not `y`.', call. = FALSE) + if (is.null(type)) { + # enforce boxplot type for y ~ factor(x) + if (!is.null(x) && is.factor(x) && !is.factor(y)) { + type = "boxplot" + } else { + type = "p" + } + } else if (type %in% c("hist", "histogram")) { + type = "histogram" + } else if (type %in% c("j", "jitter")) { + type = "jitter" } return(type) } diff --git a/R/tinyplot.R b/R/tinyplot.R index b1093753..f49fab22 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -67,7 +67,9 @@ #' should not be specified in the same call. #' @param data a data.frame (or list) from which the variables in formula #' should be taken. A matrix is converted to a data frame. -#' @param type character string giving the type of plot desired. Options are: +#' @param type character string giving the type of plot desired. If no argument +#' is provided, then the plot type will default to something sensible for the +#' type of `x` and `y` inputs (i.e., usually `"p"`). Options are: #' - The same set of 1-character values supported by #' \code{\link[graphics]{plot}}: `"p"` for points, `"l"` for lines, `"b"` for #' both points and lines, `"c"` for empty points joined by lines, `"o"` for @@ -470,7 +472,7 @@ tinyplot.default = function( facet = NULL, facet.args = NULL, data = NULL, - type = "p", + type = NULL, xlim = NULL, ylim = NULL, log = "", @@ -504,15 +506,17 @@ tinyplot.default = function( width = NULL, height = NULL, empty = FALSE, - ...) { + ... + ) { + dots = list(...) + if (isTRUE(add)) legend = FALSE + + # sanitize arguments ribbon.alpha = sanitize_ribbon.alpha(ribbon.alpha) type = sanitize_type(type, x, y) - if (isTRUE(add)) legend = FALSE - - xlabs = NULL # Write plot to output file or window with fixed dimensions @@ -579,7 +583,7 @@ tinyplot.default = function( y_dep = paste0("[", ymin_dep, ", ", ymax_dep, "]") y = rep(NA, length(x)) if (is.null(ylim)) ylim = range(c(ymin, ymax)) - } else if (!type %in% c("density", "hist", "histogram")) { + } else if (!type %in% c("density", "histogram")) { y = x x = seq_along(x) xlab = "Index" @@ -600,7 +604,7 @@ tinyplot.default = function( return(do.call(tinyplot.density, args = fargs)) } - if (type %in% c("hist", "histogram")) { + if (type == "histogram") { fargs = histogram_args( x = x, by = by, facet = facet, facet_by = facet_by, dots = dots, ylab = ylab, col = col, bg = bg, fill = fill, ribbon.alpha = ribbon.alpha) @@ -616,7 +620,25 @@ tinyplot.default = function( was_area_type = FALSE # flag to keep track for some legend adjustments below } - if (type %in% c("j", "jitter")) { + if (type == "jitter") { + if (is.character(x)) x = as.factor(x) + if (is.character(y)) y = as.factor(y) + if (is.factor(x)) { + xlvls = levels(x) + xlabs = seq_along(xlvls) + names(xlabs) = xlvls + x = as.integer(x) + } else { + xlabs = NULL + } + if (is.factor(y)) { + ylvls = levels(y) + ylabs = seq_along(ylvls) + names(ylabs) = ylvls + y = as.integer(y) + } else { + ylabs = NULL + } x = jitter(x) y = jitter(y) type = "p" @@ -1059,7 +1081,7 @@ tinyplot.default = function( if (isTRUE(axes)) { if (isTRUE(frame.plot)) { # if plot frame is true then print axes per normal... - if (type %in% c("pointrange", "errorbar", "ribbon", "boxplot") && !is.null(xlabs)) { + if (type %in% c("pointrange", "errorbar", "ribbon", "boxplot", "p") && !is.null(xlabs)) { Axis(x, side = xside, at = xlabs, labels = names(xlabs)) } else { Axis(x, side = xside) @@ -1068,7 +1090,7 @@ tinyplot.default = function( } else { # ... else only print the "outside" axes. if (ii %in% oxaxis) { - if (type %in% c("pointrange", "errorbar", "ribbon", "boxplot") && !is.null(xlabs)) { + if (type %in% c("pointrange", "errorbar", "ribbon", "boxplot", "p") && !is.null(xlabs)) { Axis(x, side = xside, at = xlabs, labels = names(xlabs)) } else { Axis(x, side = xside) @@ -1301,7 +1323,7 @@ tinyplot.formula = function( data = parent.frame(), facet = NULL, facet.args = NULL, - type = "p", + type = NULL, xlim = NULL, ylim = NULL, # log = "", @@ -1463,7 +1485,7 @@ tinyplot.formula = function( } ## nice axis and legend labels - if (type %in% c("hist", "histogram")) { + if (!is.null(type) && type %in% c("hist", "histogram")) { if (is.null(ylab)) ylab = "Frequency" if (is.null(xlab)) xlab = names(mf)[x_loc] } else if (no_y) { diff --git a/inst/tinytest/helpers.R b/inst/tinytest/helpers.R index e0c7db0a..bba50e3b 100755 --- a/inst/tinytest/helpers.R +++ b/inst/tinytest/helpers.R @@ -2,8 +2,8 @@ library(tinytest) library(tinysnapshot) # # Skip tests if not on Linux -ON_LINUX = Sys.info()["sysname"] == "Linux" -if (!ON_LINUX) exit_file("Linux snapshots") +# ON_LINUX = Sys.info()["sysname"] == "Linux" +# if (!ON_LINUX) exit_file("Linux snapshots") options("tinysnapshot_os" = "Linux") options("tinysnapshot_device" = "svglite") diff --git a/man/tinyplot.Rd b/man/tinyplot.Rd index ca0d5615..894d8137 100644 --- a/man/tinyplot.Rd +++ b/man/tinyplot.Rd @@ -44,7 +44,7 @@ tinyplot(x, ...) facet = NULL, facet.args = NULL, data = NULL, - type = "p", + type = NULL, xlim = NULL, ylim = NULL, log = "", @@ -86,7 +86,7 @@ tinyplot(x, ...) data = parent.frame(), facet = NULL, facet.args = NULL, - type = "p", + type = NULL, xlim = NULL, ylim = NULL, main = NULL, @@ -175,7 +175,9 @@ and background. Default values for these arguments are inherited from features globally for all \code{tinyplot} plots. }} -\item{type}{character string giving the type of plot desired. Options are: +\item{type}{character string giving the type of plot desired. If no argument +is provided, then the plot type will default to something sensible for the +type of \code{x} and \code{y} inputs (i.e., usually \code{"p"}). Options are: \itemize{ \item The same set of 1-character values supported by \code{\link[graphics]{plot}}: \code{"p"} for points, \code{"l"} for lines, \code{"b"} for