diff --git a/R/tinyplot.R b/R/tinyplot.R index c076702c..597b8f05 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -659,15 +659,6 @@ tinyplot.default = function( } } - # catch for adding to existing facet plot - if (!is.null(facet) && add) { - recordGraphics( - par(get_saved_par(when = "after")), - list = list(), - env = getNamespace('tinyplot') - ) - } - # Save current graphical parameters opar = par(no.readonly = TRUE) if (restore.par || !is.null(facet)) { @@ -678,6 +669,15 @@ tinyplot.default = function( } set_saved_par(when = "before", opar) + # Catch for adding to existing facet plot + if (!is.null(facet) && add) { + recordGraphics( + par(get_saved_par(when = "after")), + list = list(), + env = getNamespace('tinyplot') + ) + } + # Ephemeral theme if (!is.null(theme)) { # browser() diff --git a/inst/tinytest/_tinysnapshot/tinyplot_add_no_recursive_margins.svg b/inst/tinytest/_tinysnapshot/tinyplot_add_no_recursive_margins.svg new file mode 100644 index 00000000..40bd5ce7 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/tinyplot_add_no_recursive_margins.svg @@ -0,0 +1,339 @@ + + + + + + + + + + + + + + + + + + + + + + +Sepal.Length +Sepal.Width + + + + + + + + + + + + + + + + + + +4.5 +5.5 +6.5 +7.5 + + + + + + +2.0 +2.5 +3.0 +3.5 +4.0 + +setosa + + + + + + + + + + + + + + + + + + + +4.5 +5.5 +6.5 +7.5 + + + + + + +2.0 +2.5 +3.0 +3.5 +4.0 + +versicolor + + + + + + + + + + + + + + + + + + + +4.5 +5.5 +6.5 +7.5 + + + + + + +2.0 +2.5 +3.0 +3.5 +4.0 + +virginica + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/test-tinyplot_add.R b/inst/tinytest/test-tinyplot_add.R index ed9fecef..1ee292c9 100644 --- a/inst/tinytest/test-tinyplot_add.R +++ b/inst/tinytest/test-tinyplot_add.R @@ -73,3 +73,14 @@ f = function() { tinyplot_lollipop(1:5, sin(1:5)) } expect_snapshot_plot(f, label = "tinyplot_lollipop") + + +# check that we are avoiding recursive margins for facets, by properly restoring +# the original state +f = function() { + tinyplot(Sepal.Length ~ Petal.Length, facet = ~Species, data = iris) + tinyplot_add(type = "lm") + tinyplot(Sepal.Width ~ Sepal.Length, facet = ~Species, data = iris) + tinyplot_add(type = "lm") +} +expect_snapshot_plot(f, label = "tinyplot_add_no_recursive_margins") \ No newline at end of file