diff --git a/NEWS.md b/NEWS.md index 42153052..a6d67cba 100644 --- a/NEWS.md +++ b/NEWS.md @@ -31,6 +31,9 @@ where the formatting is also better._ - Jitter layers added via `tinyplot_add()` now align correctly with grouped (offset) boxplot, violin, and ridge base layers. (#493 @grantmcdermott) +- Fixed `type_ridge()` fill errors for themes that set a qualitative palette, + e.g. `"clean2"`. (#564 @grantmcdermott) + ### Internals - We now encourage type-specific legend customizations within the individual diff --git a/R/type_ridge.R b/R/type_ridge.R index 605a0c40..946863d5 100644 --- a/R/type_ridge.R +++ b/R/type_ridge.R @@ -396,6 +396,16 @@ data_ridge = function(bw = "nrd0", adjust = 1, kernel = "gaussian", n = 512, if (is.null(col) && (!anyby || x_by)) col = "black" + # For ridge themes without groups, a numeric bg (e.g. bg = 0.2) should + # produce transparent gray, not a transparent palette colour. (#547) + ridge_theme = identical(.tpar[["tinytheme"]], "ridge") || identical(.tpar[["tinytheme"]], "ridge2") + if (ridge_theme && !anyby) { + ubg = settings[["bg"]] + if (!is.null(ubg) && length(ubg) == 1 && is.numeric(ubg) && ubg >= 0 && ubg <= 1) { + settings[["bg"]] = adjustcolor("gray", alpha.f = ubg) + } + } + # Save original yaxt for type_info before overwriting yaxt_orig = yaxt yaxt = "n" @@ -408,6 +418,7 @@ data_ridge = function(bw = "nrd0", adjust = 1, kernel = "gaussian", n = 512, manbreaks = manbreaks, yaxt = yaxt_orig, raster = raster, + ridge_theme = ridge_theme, x_by = x_by, y_by = y_by, fill_by = fill_by, @@ -438,17 +449,25 @@ data_ridge = function(bw = "nrd0", adjust = 1, kernel = "gaussian", n = 512, ## Underlying draw_ridge function draw_ridge = function() { fun = function(ix, iy, iz, ibg, icol, iymin, iymax, type_info, ...) { - ridge_theme = identical(.tpar[["tinytheme"]], "ridge") || identical(.tpar[["tinytheme"]], "ridge2") + ridge_theme = isTRUE(type_info[["ridge_theme"]]) d = data.frame(x = ix, y = iy, ymin = iymin, ymax = iymax) dsplit = split(d, d$y) + if (!is.null(type_info[["col"]])) icol = type_info[["col"]] if (is.null(ibg)) { - default_bg = if (!ridge_theme && !is.null(.tpar[["palette.qualitative"]])) seq_palette(by_col(), n = 2)[2] else "gray" + pal_q = .tpar[["palette.qualitative"]] + # For non-ridge themes with a palette, derive fill from the first palette + # colour. We need palette.colors() here because the palette is still just + # a string name at this point (not yet resolved to colours). (#547) + default_bg = if (!ridge_theme && !is.null(pal_q)) { + seq_palette(palette.colors(1, palette = pal_q), n = 2)[2] + } else { + "gray" + } ibg = if (isTRUE(type_info[["fill_by"]])) seq_palette(icol, n = 2)[2] else default_bg } if (!is.null(type_info[["alpha"]]) && is.null(type_info[["palette"]])) { ibg = adjustcolor(ibg, alpha.f = type_info[["alpha"]]) } - if (!is.null(type_info[["col"]])) icol = type_info[["col"]] lab = if (is.factor(d$y)) levels(d$y) else unique(d$y) if (isTRUE(type_info[["y_by"]])) { # avoid duplicating the y-axis labs for the special y==by case diff --git a/inst/tinytest/_tinysnapshot/ridge_theme_bg_numeric.svg b/inst/tinytest/_tinysnapshot/ridge_theme_bg_numeric.svg new file mode 100644 index 00000000..7acdb280 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/ridge_theme_bg_numeric.svg @@ -0,0 +1,69 @@ + + + + + + + + + + + + + +Sepal.Width +Species +1.5 +2.0 +2.5 +3.0 +3.5 +4.0 +4.5 + + + + + + + + + + + + + + + + + + +setosa +versicolor +virginica + + + + + + + + + + + diff --git a/inst/tinytest/_tinysnapshot/ridge_theme_palette.svg b/inst/tinytest/_tinysnapshot/ridge_theme_palette.svg new file mode 100644 index 00000000..7138677a --- /dev/null +++ b/inst/tinytest/_tinysnapshot/ridge_theme_palette.svg @@ -0,0 +1,73 @@ + + + + + + + + + + + + + +Sepal.Width +Species +1.5 +2.0 +2.5 +3.0 +3.5 +4.0 +4.5 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +setosa +versicolor +virginica + + + diff --git a/inst/tinytest/test-type_ridge.R b/inst/tinytest/test-type_ridge.R index 18e4b902..eef900fc 100644 --- a/inst/tinytest/test-type_ridge.R +++ b/inst/tinytest/test-type_ridge.R @@ -147,4 +147,16 @@ f = function() { col = "white" ) } -expect_snapshot_plot(f, label = "ridge_gradient_facet_raster_alpha") \ No newline at end of file +expect_snapshot_plot(f, label = "ridge_gradient_facet_raster_alpha") + +# Issue #547: ridge with themed palette should not error +f = function() { + tinyplot(Species ~ Sepal.Width, data = iris, type = "ridge", theme = "clean2") +} +expect_snapshot_plot(f, label = "ridge_theme_palette") + +# Issue #547: numeric bg with ridge theme should produce transparent gray +f = function() { + tinyplot(Species ~ Sepal.Width, data = iris, type = "ridge", theme = "ridge2", bg = 0.2) +} +expect_snapshot_plot(f, label = "ridge_theme_bg_numeric")