diff --git a/NEWS.md b/NEWS.md index e77c0e9..fe11a7b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,6 +16,9 @@ - The logo now sits in its own reserved band instead of pulling the plot down with negative margins, so captions are no longer clipped at the bottom edge and the layout no longer depends on whether showtext is active (#20). +- `add_logo_ga()` gets the same reserved logo band (`logo_height`) and no longer + pulls the plots down with a negative margin. `margin_param` still overrides the + bottom margin when given (#28). - The logo keeps its own aspect ratio on any canvas. `make_logo_grob()` used to stretch it to a fixed fraction of the plot width, so its shape changed with the canvas and it was already about 1.5 times too wide at 11 by 7 inches (#20). diff --git a/R/logo.R b/R/logo.R index 9f28b4b..997e8a5 100644 --- a/R/logo.R +++ b/R/logo.R @@ -160,9 +160,12 @@ has_caption <- function(gg) { #' @param logo a grob containing the logo created with `make_logo_grob` #' @param nrow an integer, default = 1, for the number of rows to align the plots in #' @param widths an optional vector the same length as plot_list with the widths for each plot -#' @param margin_param a number giving the adjustment up or down to help manually align logo and captions +#' @param margin_param Optional number. Replaces the bottom plot margin (in points). +#' `NULL` (default) leaves plot margins alone. #' @param font_scale Numeric. Multiplicative scaling factor applied to text #' sizes before composing. Default `1`. See [add_logo()] for details. +#' @param logo_height Numeric. Height in inches of the band reserved for the +#' logo below the plots. Default `0.5`. #' #' @return a grid object #' @note The resulting object needs to be drawn to the screen using grid.draw() @@ -181,11 +184,11 @@ has_caption <- function(gg) { #' dev.off() #' } add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL, - margin_param = NULL, font_scale = 1) { + margin_param = NULL, font_scale = 1, + logo_height = 0.5) { # Capture the background color from the first plot bg_fill <- plot_list[[1]]$theme$plot.background$fill - # Inflate text sizes to compensate for viewport shrinkage if (!is.null(font_scale) && font_scale != 1) { plot_list <- lapply(plot_list, function(p) { base_size <- p$theme$text$size %||% 14 @@ -195,20 +198,16 @@ add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL, }) } - # Change position of logo depending on if plot has a caption + # The logo has its own band below the plots, so no margin adjustment is + # needed. `margin_param` still replaces the bottom margin when given. if (!is.null(margin_param)) { margin <- theme(plot.margin = unit(c(7, 7, margin_param, 7), "pt")) - } else if (any(unlist(lapply(plot_list, has_caption)))) { - cap_lines <- max(sapply(plot_list, measure_caption)) - margin <- theme(plot.margin = unit(c(7, 7, -7 * sqrt(cap_lines), 7), "pt")) - } else { - margin <- theme(plot.margin = unit(c(7, 7, 7, 7), "pt")) - } - if (nrow > 1) { - len <- length(plot_list) - plot_list[[len]] <- plot_list[[len]] + margin - } else { - plot_list <- lapply(plot_list, "+", margin) + if (nrow > 1) { + len <- length(plot_list) + plot_list[[len]] <- plot_list[[len]] + margin + } else { + plot_list <- lapply(plot_list, "+", margin) + } } if (nrow != 1) { @@ -218,7 +217,10 @@ add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL, hold <- arrangeGrob(grobs = plot_list, nrow = 1, ncol = length(plot_list), widths = widths) } - composed <- arrangeGrob(hold, logo, heights = c(0.93, .07)) + composed <- arrangeGrob( + hold, logo, + heights = grid::unit.c(grid::unit(1, "null"), grid::unit(logo_height, "in")) + ) if (!is.null(bg_fill) && !is.na(bg_fill)) { bg_rect <- grid::rectGrob(gp = grid::gpar(fill = bg_fill, col = NA)) diff --git a/man/add_logo_ga.Rd b/man/add_logo_ga.Rd index 9163a01..a4cd78b 100644 --- a/man/add_logo_ga.Rd +++ b/man/add_logo_ga.Rd @@ -10,7 +10,8 @@ add_logo_ga( nrow = 1, widths = NULL, margin_param = NULL, - font_scale = 1 + font_scale = 1, + logo_height = 0.5 ) } \arguments{ @@ -22,10 +23,14 @@ add_logo_ga( \item{widths}{an optional vector the same length as plot_list with the widths for each plot} -\item{margin_param}{a number giving the adjustment up or down to help manually align logo and captions} +\item{margin_param}{Optional number. Replaces the bottom plot margin (in points). +`NULL` (default) leaves plot margins alone.} \item{font_scale}{Numeric. Multiplicative scaling factor applied to text sizes before composing. Default `1`. See [add_logo()] for details.} + +\item{logo_height}{Numeric. Height in inches of the band reserved for the +logo below the plots. Default `0.5`.} } \value{ a grid object diff --git a/tests/testthat/test_logo.R b/tests/testthat/test_logo.R index 6483de6..65d51a2 100644 --- a/tests/testthat/test_logo.R +++ b/tests/testthat/test_logo.R @@ -111,3 +111,28 @@ test_that("the logo keeps its aspect ratio at any canvas width (#20)", { expect_lt(diff(range(aspects)) / mean(aspects), 0.03, label = type) } }) + +# --- add_logo_ga() layout (#28) ---------------------------------------------- + +test_that("add_logo_ga reserves a logo band instead of using negative margins", { + logo <- make_logo_grob() + composed <- add_logo_ga(list(caption_plot(), caption_plot()), logo, logo_height = 0.6) + heights <- composed_table(composed)$heights + expect_equal(length(heights), 2) + expect_true(any(grepl("null", as.character(heights)))) + expect_true(any(grepl("0.6", as.character(heights)))) +}) + +test_that("add_logo_ga does not clip captions at several canvas sizes", { + skip_on_cran() + logo <- make_logo_grob() + for (nrow in c(1, 2)) { + for (w in c(8, 11, 16, 24)) { + composed <- suppressWarnings( + add_logo_ga(list(caption_plot(), caption_plot()), logo, nrow = nrow) + ) + ink <- bottom_ink(composed, w, w * 0.64) + expect_equal(ink, 0, info = paste("nrow", nrow, "width", w)) + } + } +})