fix: inherit plot background color in logo compositing
R-CMD-check / R CMD check (push) Successful in 3m41s

The logo grob uses theme_void() (transparent background), so when
arrangeGrob() composites it with a dark-themed plot, the logo strip
falls back to the device default (white). Extract the plot's
plot.background fill and apply it to the logo grob before compositing.

Fixes the issue where theme_civilytics_dark(font_size = 16) piped to
civilytics_logo(variant = "dark") would show a light background in
the logo/caption area.
This commit is contained in:
2026-05-19 16:47:36 -06:00
parent 98e0949d27
commit ac8e3eb60e
+18
View File
@@ -64,6 +64,16 @@ add_logo <- function(plot, logo, margin_param = NULL, font_scale = 1.1,
position <- match.arg(position) position <- match.arg(position)
at_top <- grepl("top", position) at_top <- grepl("top", position)
# Inherit the plot's background color so the logo strip matches.
# make_logo_grob() uses theme_void() (transparent), so without this the
# logo area falls back to the device default (white).
bg_fill <- plot$theme$plot.background$fill
if (!is.null(bg_fill) && !is.na(bg_fill)) {
logo <- logo + ggplot2::theme(
plot.background = ggplot2::element_rect(fill = bg_fill, color = NA)
)
}
# Inflate text sizes to compensate for arrangeGrob viewport shrinkage. # Inflate text sizes to compensate for arrangeGrob viewport shrinkage.
# All theme text elements use rel() sizing, so scaling the root 'text' # All theme text elements use rel() sizing, so scaling the root 'text'
# element cascades to titles, axis labels, legends, captions, and strips. # element cascades to titles, axis labels, legends, captions, and strips.
@@ -164,6 +174,14 @@ has_caption <- function(gg) {
#' } #' }
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.1) { margin_param = NULL, font_scale = 1.1) {
# Inherit the background color from the first plot so the logo strip matches
bg_fill <- plot_list[[1]]$theme$plot.background$fill
if (!is.null(bg_fill) && !is.na(bg_fill)) {
logo <- logo + ggplot2::theme(
plot.background = ggplot2::element_rect(fill = bg_fill, color = NA)
)
}
# Inflate text sizes to compensate for viewport shrinkage # 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) {