diff --git a/NEWS.md b/NEWS.md index ae375f65..16a5430d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -25,6 +25,8 @@ where the formatting is also better._ - Jittered plots now support Date/POSIXt axes. Thanks to @wachtermh for the bug report and @vincentarelbundock for the code contribution. (#327) +- `tinyplot_add(type = "jitter")` no longer errors when layered on top of + boxplot, violin, or similar categorical plot types. (#560 @grantmcdermott) ### Internals diff --git a/R/align_layer.R b/R/align_layer.R index 7a837696..1ed43fb3 100644 --- a/R/align_layer.R +++ b/R/align_layer.R @@ -34,6 +34,8 @@ align_layer = function(settings) { } else if (!is.null(names(xlabs_layer))) { # Case 2: match implicit integer -> label mapping (e.g., lines added to errorbars) if (setequal(names(xlabs_layer), names(xlabs_orig))) { + # If mappings already agree and no dodge, no realignment needed + if (identical(xlabs_layer, xlabs_orig) && is.null(settings$dodge)) return(invisible()) orig_order = xlabs_orig[names(xlabs_layer)[settings$datapoints[["x"]]]] x_layer = settings$datapoints[["x"]] if (is.null(settings$dodge)) { @@ -55,4 +57,3 @@ align_layer = function(settings) { } } } - diff --git a/inst/tinytest/_tinysnapshot/tinyplot_add_jitter_on_violin.svg b/inst/tinytest/_tinysnapshot/tinyplot_add_jitter_on_violin.svg new file mode 100644 index 00000000..7cd8fb86 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/tinyplot_add_jitter_on_violin.svg @@ -0,0 +1,212 @@ + + + + + + + + + + + + + +Species +Sepal.Length + + + + +setosa +versicolor +virginica + + + + + + +4 +5 +6 +7 +8 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/test-tinyplot_add.R b/inst/tinytest/test-tinyplot_add.R index af2a4305..9b72ddac 100644 --- a/inst/tinytest/test-tinyplot_add.R +++ b/inst/tinytest/test-tinyplot_add.R @@ -93,3 +93,12 @@ f = function() { tinyplot_add(type = "lm") } expect_snapshot_plot(f, label = "tinyplot_add_no_recursive_margins") + + +# jitter layer on top of boxplot (#559) +set.seed(42) +f = function() { + tinyplot(Sepal.Length ~ Species, data = iris, type = "violin") + tinyplot_add(type = "jitter", cex = 0.5, alpha = 0.3) +} +expect_snapshot_plot(f, label = "tinyplot_add_jitter_on_violin")