From d9874da2ce2646aa6a11a58db0d90667e4ead110 Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Mon, 17 Feb 2025 17:39:21 -0800 Subject: [PATCH 1/6] character vars should inherit same default types as factors --- R/sanitize.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/sanitize.R b/R/sanitize.R index 86af40e4..63ecf4ae 100644 --- a/R/sanitize.R +++ b/R/sanitize.R @@ -22,10 +22,10 @@ sanitize_type = function(type, x, y, dots) { assert_choice(type, types, null.ok = TRUE) if (is.null(type)) { - if (!is.null(x) && is.factor(x) && !is.factor(y)) { + if (!is.null(x) && (is.factor(x) || is.character(x)) && !(is.factor(y) || is.character(y))) { # enforce boxplot type for y ~ factor(x) type = type_boxplot - } else if (is.factor(y)) { + } else if (is.factor(y) || is.character(y)) { # enforce spineplot type for factor(y) ~ x type = type_spineplot } else { From e850d7aac5da1b933fc27f1a35a05ee1b48cfd6e Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Mon, 17 Feb 2025 18:03:43 -0800 Subject: [PATCH 2/6] `type_points()` should work with character/factor variables --- R/type_points.R | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/R/type_points.R b/R/type_points.R index 0041f2c5..7c488473 100644 --- a/R/type_points.R +++ b/R/type_points.R @@ -23,13 +23,41 @@ type_points = function() { out = list( draw = draw_points(), - data = NULL, + data = data_points(), name = "p" ) class(out) = "tinyplot_type" return(out) } +data_points = function() { + fun = function(datapoints, ...) { + # catch for factors (we should still be able to "force" plot these with points) + if (is.factor(datapoints$x)) { + xlvls = levels(datapoints$x) + xlabs = seq_along(xlvls) + names(xlabs) = xlvls + datapoints$x = as.integer(datapoints$x) + } else { + xlabs = NULL + } + if (is.factor(datapoints$y)) { + ylvls = levels(datapoints$y) + ylabs = seq_along(ylvls) + names(ylabs) = ylvls + datapoints$y = as.integer(datapoints$y) + } else { + ylabs = NULL + } + + out = list( + datapoints = datapoints, + xlabs = xlabs, + ylabs = ylabs + ) + return(out) + } +} draw_points = function() { fun = function(ix, iy, icol, ibg, ipch, ilwd, cex, ...) { From a0d59171ada101874ce290ddcd9a55b7627b7f9f Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Mon, 17 Feb 2025 20:54:02 -0800 Subject: [PATCH 3/6] NEWS --- NEWS.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 15955991..ad26ea0e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,8 +16,11 @@ Bugs fixes: - The `cex` argument should be respected when using `type="bg"`. Thanks to @rjknell for report #307 and @vincentarelbundock for the fix. - -- The `lwd` argument should be passed down to `pt.lwd` for type "p". Sets proper line weight for the border of pch symbols in legend. Report in #319 and fix in #320 by @kscott-1. +- The `lwd` argument is now correctly passed down to `pt.lwd` for type `"p"`, + which sets proper line weight for the border of pch symbols in legend. Report + in #319 and fix in #320 by @kscott-1. +- Passing `x` and/or `y` as character variables now triggers the same default + plot type behaviour as factors, e.g. boxplots. (#323 @grantmcdermott) ## 0.3.0 From 92ab72204da4196bc3d88924c063adc9179fae91 Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Mon, 17 Feb 2025 20:55:21 -0800 Subject: [PATCH 4/6] redundant code --- R/type_jitter.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/type_jitter.R b/R/type_jitter.R index c9e6e4d1..57339df3 100644 --- a/R/type_jitter.R +++ b/R/type_jitter.R @@ -27,8 +27,6 @@ data_jitter = function(factor, amount) { fun = function(datapoints, ...) { x = datapoints$x y = datapoints$y - 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) From 662e0631699fd28b9baad69c054be7ee83325136 Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Mon, 17 Feb 2025 20:59:17 -0800 Subject: [PATCH 5/6] catch for type = "p" when x or y are factors - see #322 --- R/sanitize.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/sanitize.R b/R/sanitize.R index 63ecf4ae..76208356 100644 --- a/R/sanitize.R +++ b/R/sanitize.R @@ -49,6 +49,7 @@ sanitize_type = function(type, x, y, dots) { "lines" = type_lines, "lm" = type_lm, "loess" = type_loess, + "p" = type_points, "pointrange" = type_pointrange, "points" = type_points, "polygon" = type_polygon, From 4beefcf87be99832c8cc0682294d1be70b9da1c8 Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Mon, 17 Feb 2025 21:04:28 -0800 Subject: [PATCH 6/6] NEWS --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index ad26ea0e..c0c01f6b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -21,6 +21,8 @@ Bugs fixes: in #319 and fix in #320 by @kscott-1. - Passing `x` and/or `y` as character variables now triggers the same default plot type behaviour as factors, e.g. boxplots. (#323 @grantmcdermott) +- Scatter plots (`type_points()`/`"p"`) now work even if `x` or `y` is a factor + or character variable. (#323 @grantmcdermott) ## 0.3.0