diff --git a/NEWS.md b/NEWS.md index a6d67cba..9c2315ae 100644 --- a/NEWS.md +++ b/NEWS.md @@ -34,6 +34,10 @@ where the formatting is also better._ - Fixed `type_ridge()` fill errors for themes that set a qualitative palette, e.g. `"clean2"`. (#564 @grantmcdermott) +- Fixed plot clipping when using ephemeral `theme = "default"` with a legend + and `tinyplot_add()`. Thanks to @katrinabrock for the report in #557. + (#565 @grantmcdermott) + ### Internals - We now encourage type-specific legend customizations within the individual diff --git a/R/tinyplot.R b/R/tinyplot.R index 2ba8f847..3a980165 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -692,13 +692,19 @@ tinyplot.default = function( } else { warning('Argument `theme` must be a character of length 1 (e.g. "clean"), or a list. Ignoring.') } - dtheme = theme_default - otheme = opar[names(dtheme)] - - on.exit(do.call(tinytheme, otheme), add = TRUE) + if (is.character(theme) && theme == "default") { + # Reset mar to pre-theme value so legend margin adjustment isn't + # clobbered. Only needed for "default" theme which uses hook = FALSE + # and thus sets par(mar) immediately. (#557) + par(mar = opar$mar) + on.exit(init_tpar(rm_hook = TRUE), add = TRUE) + } else { + dtheme = theme_default + otheme = opar[names(dtheme)] + on.exit(do.call(tinytheme, otheme), add = TRUE) + } } - # ## settings container ----- # diff --git a/inst/tinytest/_tinysnapshot/ephemeral_default_theme_add.svg b/inst/tinytest/_tinysnapshot/ephemeral_default_theme_add.svg new file mode 100644 index 00000000..cbc30034 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/ephemeral_default_theme_add.svg @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + +c("a", "a", "a") +c(1, 1, 1) + + + + + + + +1:3 +c(1, 1, 1) + + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + + +0.6 +0.8 +1.0 +1.2 +1.4 + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/test-tinytheme.R b/inst/tinytest/test-tinytheme.R index 01bb9d0b..d58f171d 100644 --- a/inst/tinytest/test-tinytheme.R +++ b/inst/tinytest/test-tinytheme.R @@ -166,4 +166,11 @@ f = function() { plt_add(type = "lm") par(opar) } -expect_snapshot_plot(f, label = "tinytheme_ephemeral") \ No newline at end of file +expect_snapshot_plot(f, label = "tinytheme_ephemeral") + +# Ephemeral "default" theme with by + plt_add should not clip (#557) +f = function() { + plt(1:3, c(1, 1, 1), by = c("a", "a", "a"), theme = "default", type = "n") + plt_add(type = "b") +} +expect_snapshot_plot(f, label = "ephemeral_default_theme_add")