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
+53
-18
No files matched your search
@@ -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))
|
||||
|
||||
Reference in new issue
Block a user