fix(logo): reserve a logo band in add_logo_ga() instead of a negative margin (#28)
R-CMD-check / R CMD check (push) Successful in 4m38s
R-CMD-check / R CMD check (push) Successful in 4m38s
add_logo_ga() now gives the logo its own band (logo_height inches) below the plots, as add_logo() does, and leaves plot margins alone unless margin_param is given. Closes #28 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a2a0fce265
commit
f3ec052144
4 files changed
+48
-13
No files matched your search
@@ -16,6 +16,9 @@
|
|||||||
- The logo now sits in its own reserved band instead of pulling the plot down with
|
- 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
|
negative margins, so captions are no longer clipped at the bottom edge and the
|
||||||
layout no longer depends on whether showtext is active (#20).
|
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
|
- 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
|
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).
|
the canvas and it was already about 1.5 times too wide at 11 by 7 inches (#20).
|
||||||
|
|||||||
@@ -160,9 +160,12 @@ has_caption <- function(gg) {
|
|||||||
#' @param logo a grob containing the logo created with `make_logo_grob`
|
#' @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 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 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
|
#' @param font_scale Numeric. Multiplicative scaling factor applied to text
|
||||||
#' sizes before composing. Default `1`. See [add_logo()] for details.
|
#' 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
|
#' @return a grid object
|
||||||
#' @note The resulting object needs to be drawn to the screen using grid.draw()
|
#' @note The resulting object needs to be drawn to the screen using grid.draw()
|
||||||
@@ -181,11 +184,11 @@ has_caption <- function(gg) {
|
|||||||
#' dev.off()
|
#' dev.off()
|
||||||
#' }
|
#' }
|
||||||
add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL,
|
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
|
# Capture the background color from the first plot
|
||||||
bg_fill <- plot_list[[1]]$theme$plot.background$fill
|
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) {
|
if (!is.null(font_scale) && font_scale != 1) {
|
||||||
plot_list <- lapply(plot_list, function(p) {
|
plot_list <- lapply(plot_list, function(p) {
|
||||||
base_size <- p$theme$text$size %||% 14
|
base_size <- p$theme$text$size %||% 14
|
||||||
@@ -195,21 +198,17 @@ 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)) {
|
if (!is.null(margin_param)) {
|
||||||
margin <- theme(plot.margin = unit(c(7, 7, margin_param, 7), "pt"))
|
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) {
|
if (nrow > 1) {
|
||||||
len <- length(plot_list)
|
len <- length(plot_list)
|
||||||
plot_list[[len]] <- plot_list[[len]] + margin
|
plot_list[[len]] <- plot_list[[len]] + margin
|
||||||
} else {
|
} else {
|
||||||
plot_list <- lapply(plot_list, "+", margin)
|
plot_list <- lapply(plot_list, "+", margin)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (nrow != 1) {
|
if (nrow != 1) {
|
||||||
if (!is.null(widths)) warning("`widths` is ignored when `nrow > 1`")
|
if (!is.null(widths)) warning("`widths` is ignored when `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)
|
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)) {
|
if (!is.null(bg_fill) && !is.na(bg_fill)) {
|
||||||
bg_rect <- grid::rectGrob(gp = grid::gpar(fill = bg_fill, col = NA))
|
bg_rect <- grid::rectGrob(gp = grid::gpar(fill = bg_fill, col = NA))
|
||||||
|
|||||||
+7
-2
@@ -10,7 +10,8 @@ add_logo_ga(
|
|||||||
nrow = 1,
|
nrow = 1,
|
||||||
widths = NULL,
|
widths = NULL,
|
||||||
margin_param = NULL,
|
margin_param = NULL,
|
||||||
font_scale = 1
|
font_scale = 1,
|
||||||
|
logo_height = 0.5
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
\arguments{
|
\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{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
|
\item{font_scale}{Numeric. Multiplicative scaling factor applied to text
|
||||||
sizes before composing. Default `1`. See [add_logo()] for details.}
|
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{
|
\value{
|
||||||
a grid object
|
a grid object
|
||||||
|
|||||||
@@ -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)
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in new issue
Block a user