diff --git a/NEWS.md b/NEWS.md index d9645753..b80a19d5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -47,8 +47,10 @@ where the formatting is also better._ `pch = "."`. (#338 @grantmcdermott) - Line plots (`type_lines()`/`"l"`) now pass on the `bg` argument to the drawing function. Thanks to @wviechtb for report in #355 (@zeileis). -- Fixed dynamic LHS margin spacing for flipped `"boxplot"` and `"jitter"` types. - Thanks to @eddelbuettel for the report in #357 (@grantmcdermott). +- Fixed dynamic y-axis margin spacing for flipped `"boxplot"` and `"jitter"` + types. Thanks to @eddelbuettel for the report in #357 (@grantmcdermott). +- Fixed dynamic x-axis margin spacing for perpendicular (vertical) label text, + i.e. cases where `las = 2` or `las = 3`. #369 (@grantmcdermott). ### Internals: diff --git a/R/facet.R b/R/facet.R index 29634a01..f4aa0aab 100644 --- a/R/facet.R +++ b/R/facet.R @@ -199,28 +199,32 @@ draw_facet_window = function(grid, ...) { omar = omar + c(0, whtsbp, 0, 0) * cex_fct_adj fmar[2] = fmar[2] + whtsbp * cex_fct_adj } + # Extra reduction if no plot frame to reduce whitespace + if (isFALSE(frame.plot) && !isTRUE(facet.args[["free"]])) { + fmar[2] = fmar[2] - (whtsbp * cex_fct_adj) + } } if (par("las") %in% 2:3) { # extra whitespace bump on the x axis # xaxlabs = axTicks(1) - xaxlabs = axisTicks(usr = extendrange(xlim, f = 0.04), log = par("xlog")) + xaxlabs = if (is.null(xlabs)) axisTicks(usr = extendrange(xlim, f = 0.04), log = par("xlog")) else + if (!is.null(names(xlabs))) names(xlabs) else xlabs if (!is.null(xaxl)) xaxlabs = tinylabel(xaxlabs, xaxl) - whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - 1 - # whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - grconvertY(0, from = "nfc", to = "lines") - 1 + whtsbp = grconvertX(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - 1 if (whtsbp > 0) { omar = omar + c(whtsbp, 0, 0, 0) * cex_fct_adj fmar[1] = fmar[1] + whtsbp * cex_fct_adj } + # Extra reduction if no plot frame to reduce whitespace + if (isFALSE(frame.plot) && !isTRUE(facet.args[["free"]])) { + fmar[1] = fmar[1] - (whtsbp * cex_fct_adj) + } } # FIXME: Is this causing issues for lhs legends with facet_grid? # catch for missing rhs legend if (isTRUE(attr(facet, "facet_grid")) && !has_legend) { omar[4] = omar[4] + 1 } - # Extra reduction if no plot frame to reduce whitespace - if (isFALSE(frame.plot) && !isTRUE(facet.args[["free"]])) { - fmar[2] = fmar[2] - (whtsbp * cex_fct_adj) - } } # Now we set the margins. The trick here is that we simultaneously adjust @@ -271,10 +275,10 @@ draw_facet_window = function(grid, ...) { if (par("las") %in% 2:3) { # extra whitespace bump on the x axis # xaxl = axTicks(1) - xaxlabs = axisTicks(usr = extendrange(ylim, f = 0.04), log = par("xlog")) + xaxlabs = if (is.null(xlabs)) axisTicks(usr = extendrange(xlim, f = 0.04), log = par("xlog")) else + if (!is.null(names(xlabs))) names(xlabs) else xlabs if (!is.null(xaxl)) xaxlabs = tinylabel(xaxlabs, xaxl) - whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - 1 - # whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - grconvertY(0, from = "nfc", to = "lines") - 1 + whtsbp = grconvertX(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - 1 if (whtsbp > 0) { omar[1] = omar[1] + whtsbp } diff --git a/inst/tinytest/_tinysnapshot/tinytheme_dynamic_clean_facet.svg b/inst/tinytest/_tinysnapshot/tinytheme_dynamic_clean_facet.svg index 7633092c..107b3a6d 100644 --- a/inst/tinytest/_tinysnapshot/tinytheme_dynamic_clean_facet.svg +++ b/inst/tinytest/_tinysnapshot/tinytheme_dynamic_clean_facet.svg @@ -40,356 +40,356 @@ Dynamic plot adjustment and whitespace reduction -hp +I(hp * 100) I(mpg * 1000) - - + + - + - - - - - - - -50 -100 -150 -200 -250 -300 - - - - - - -10000 -15000 -20000 -25000 -30000 + + + + + + + +5000 +10000 +15000 +20000 +25000 +30000 + + + + + + +10000 +15000 +20000 +25000 +30000 0 - + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - + - - - - - - - -50 -100 -150 -200 -250 -300 - - - - - - -10000 -15000 -20000 -25000 -30000 + + + + + + + +5000 +10000 +15000 +20000 +25000 +30000 + + + + + + +10000 +15000 +20000 +25000 +30000 1 - -4 - + +4 + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - + - - - - - - - -50 -100 -150 -200 -250 -300 - - - - - - -10000 -15000 -20000 -25000 -30000 - + + + + + + + +5000 +10000 +15000 +20000 +25000 +30000 + + + + + + +10000 +15000 +20000 +25000 +30000 + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - + - - - - - - - -50 -100 -150 -200 -250 -300 - - - - - - -10000 -15000 -20000 -25000 -30000 - -6 - + + + + + + + +5000 +10000 +15000 +20000 +25000 +30000 + + + + + + +10000 +15000 +20000 +25000 +30000 + +6 + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - + - - - - - - - -50 -100 -150 -200 -250 -300 - - - - - - -10000 -15000 -20000 -25000 -30000 - + + + + + + + +5000 +10000 +15000 +20000 +25000 +30000 + + + + + + +10000 +15000 +20000 +25000 +30000 + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - + - - - - - - - -50 -100 -150 -200 -250 -300 - - - - - - -10000 -15000 -20000 -25000 -30000 - -8 - + + + + + + + +5000 +10000 +15000 +20000 +25000 +30000 + + + + + + +10000 +15000 +20000 +25000 +30000 + +8 + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - + + + + - - - - - - - - - + + + + + + + + + - - - - - + + + + + - - - - + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - + + + diff --git a/inst/tinytest/_tinysnapshot/tinytheme_dynamic_dark_facet.svg b/inst/tinytest/_tinysnapshot/tinytheme_dynamic_dark_facet.svg index d2e9aefb..8343765f 100644 --- a/inst/tinytest/_tinysnapshot/tinytheme_dynamic_dark_facet.svg +++ b/inst/tinytest/_tinysnapshot/tinytheme_dynamic_dark_facet.svg @@ -40,239 +40,239 @@ Dynamic plot adjustment and whitespace reduction -hp +I(hp * 100) I(mpg * 1000) - - + + - + -10000 -15000 -20000 -25000 -30000 +10000 +15000 +20000 +25000 +30000 0 - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - + 1 - -4 + +4 - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - + -10000 -15000 -20000 -25000 -30000 +10000 +15000 +20000 +25000 +30000 - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - + - -6 + +6 - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - + -50 -100 -150 -200 -250 -300 -10000 -15000 -20000 -25000 -30000 +5000 +10000 +15000 +20000 +25000 +30000 +10000 +15000 +20000 +25000 +30000 - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - + + - + -50 -100 -150 -200 -250 -300 - -8 +5000 +10000 +15000 +20000 +25000 +30000 + +8 - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - + + + + - - - - - - - - - + + + + + + + + + - - - - - + + + + + - - - - + + + + - - - - - - - - - - - - - + + + + + + + + + + + + + - - - + + + - - + + - + diff --git a/inst/tinytest/_tinysnapshot/tinytheme_dynamic_x_boxplot.svg b/inst/tinytest/_tinysnapshot/tinytheme_dynamic_x_boxplot.svg new file mode 100644 index 00000000..6c78f5dc --- /dev/null +++ b/inst/tinytest/_tinysnapshot/tinytheme_dynamic_x_boxplot.svg @@ -0,0 +1,123 @@ + + + + + + + + + + + + +Works for perpendicular x-axis labels too +Dynamic plot adjustment and whitespace reduction +feed +weight + + + + + + + +casein +horsebean +linseed +meatmeal +soybean +sunflower + + + + + + + + +100 +150 +200 +250 +300 +350 +400 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/test-tinytheme.R b/inst/tinytest/test-tinytheme.R index f2ba29bf..282f151f 100644 --- a/inst/tinytest/test-tinytheme.R +++ b/inst/tinytest/test-tinytheme.R @@ -62,18 +62,29 @@ tinytheme("dark") f() expect_snapshot_plot(f, label = "tinytheme_dynamic_dark") +# x-axis adjustment +f = function() { + tinytheme('clean', las = 2) + tinyplot(weight ~ feed, data = chickwts, type = "boxplot", + main = "Dynamic plot adjustment and whitespace reduction", + sub = "Works for perpendicular x-axis labels too") + tinytheme() +} +expect_snapshot_plot(f, label = "tinytheme_dynamic_x_boxplot") + +# facets f = function() { tinyplot( - I(mpg*1e3) ~ hp | disp, data = mtcars, facet = cyl ~ am, + I(mpg*1e3) ~ I(hp*1e2) | disp, data = mtcars, facet = cyl ~ am, main = "Dynamic plot adjustment and whitespace reduction", sub = "Works with facets too" ) } -tinytheme("clean") +tinytheme("clean", las = 2) f() expect_snapshot_plot(f, label = "tinytheme_dynamic_clean_facet") -tinytheme("dark") +tinytheme("dark", las = 2) f() expect_snapshot_plot(f, label = "tinytheme_dynamic_dark_facet")