From 5fd562ad12df04ad3b39cad898dc3a5ffb0bc90c Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Thu, 10 Apr 2025 11:09:29 -0700 Subject: [PATCH 1/6] fix xaxis dynmar adjustment --- R/facet.R | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/R/facet.R b/R/facet.R index 29634a01..6974dbab 100644 --- a/R/facet.R +++ b/R/facet.R @@ -203,9 +203,9 @@ draw_facet_window = function(grid, ...) { 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")) - if (!is.null(xaxl)) xaxlabs = tinylabel(xaxlabs, xaxl) - whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - 1 + if (is.null(xlabs)) axisTicks(usr = extendrange(xlim, f = 0.04), log = par("xlog")) else + if (!is.null(names(xlabs))) names(xlabs) else xlabs + whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") # whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - grconvertY(0, from = "nfc", to = "lines") - 1 if (whtsbp > 0) { omar = omar + c(whtsbp, 0, 0, 0) * cex_fct_adj @@ -219,7 +219,7 @@ draw_facet_window = function(grid, ...) { } # 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) + fmar[1] = fmar[1] - (whtsbp * cex_fct_adj) } } @@ -271,9 +271,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") # whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - grconvertY(0, from = "nfc", to = "lines") - 1 if (whtsbp > 0) { omar[1] = omar[1] + whtsbp From 0bfc6bf2abb420d75c9a042bbee882b5f3c2f7dc Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Thu, 10 Apr 2025 11:13:55 -0700 Subject: [PATCH 2/6] news --- NEWS.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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: From 6ce2cf136d2038eaf7252b584a3222577a342bf4 Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Thu, 10 Apr 2025 11:55:45 -0700 Subject: [PATCH 3/6] fixups --- R/facet.R | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/R/facet.R b/R/facet.R index 6974dbab..a1adf465 100644 --- a/R/facet.R +++ b/R/facet.R @@ -199,28 +199,33 @@ 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) - if (is.null(xlabs)) axisTicks(usr = extendrange(xlim, f = 0.04), log = par("xlog")) else + 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 - whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") + whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") #- 1 + if (nfacets >= 3) whtsbp = whtsbp - 1 # FIXME # whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - grconvertY(0, 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[1] = fmar[1] - (whtsbp * cex_fct_adj) - } } # Now we set the margins. The trick here is that we simultaneously adjust From b76588944633971463608f97b2c673b0d354d047 Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Thu, 10 Apr 2025 13:30:02 -0700 Subject: [PATCH 4/6] better spacing with grConvertX --- R/facet.R | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/R/facet.R b/R/facet.R index a1adf465..d3fce4a6 100644 --- a/R/facet.R +++ b/R/facet.R @@ -209,9 +209,7 @@ draw_facet_window = function(grid, ...) { # xaxlabs = axTicks(1) 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 - whtsbp = grconvertY(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") #- 1 - if (nfacets >= 3) whtsbp = whtsbp - 1 # FIXME - # 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 @@ -279,8 +277,7 @@ draw_facet_window = function(grid, ...) { 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") - # 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 } From 6877c2eb121f2f67829001622ae7305a70249c1b Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Thu, 10 Apr 2025 13:37:43 -0700 Subject: [PATCH 5/6] catch for xaxl --- R/facet.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/facet.R b/R/facet.R index d3fce4a6..f4aa0aab 100644 --- a/R/facet.R +++ b/R/facet.R @@ -209,6 +209,7 @@ draw_facet_window = function(grid, ...) { # xaxlabs = axTicks(1) 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 = grconvertX(max(strwidth(xaxlabs, "figure")), from = "nfc", to = "lines") - 1 if (whtsbp > 0) { omar = omar + c(whtsbp, 0, 0, 0) * cex_fct_adj From 4a7e780997c418f5913a49ffb5c7c21cd21dbec2 Mon Sep 17 00:00:00 2001 From: Grant McDermott Date: Thu, 10 Apr 2025 15:19:38 -0700 Subject: [PATCH 6/6] tests --- .../tinytheme_dynamic_clean_facet.svg | 594 +++++++++--------- .../tinytheme_dynamic_dark_facet.svg | 354 +++++------ .../tinytheme_dynamic_x_boxplot.svg | 123 ++++ inst/tinytest/test-tinytheme.R | 17 +- 4 files changed, 611 insertions(+), 477 deletions(-) create mode 100644 inst/tinytest/_tinysnapshot/tinytheme_dynamic_x_boxplot.svg 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")