From 2d70d9aecfb0988b13cc534981c04b7b4ebd7936 Mon Sep 17 00:00:00 2001 From: Jared Knowles Date: Sun, 17 May 2026 10:01:35 -0600 Subject: [PATCH] fix: clean up theme.R and logo.R before restyling theme.R: - Remove deprecated legend.text.align and legend.title.align (ggplot2 3.5.0) - Remove stale @importFrom graphics plot (unused in theme function) logo.R: - Fix make_logo_grob() spurious two-row data frame (drop aes/data from canvas) - Fix add_logo_ga() caption measurement always using first plot; now uses max across all plots with captions - Fix add_logo_ga() ncol hardcoded to 2; now uses length(plot_list) - Fix add_logo_ga() widths silently ignored in nrow > 1 path; now warns - Replace deprecated qplot() in examples with ggplot() equivalents - Fix native = T -> TRUE in plot_jpeg() --- NAMESPACE | 1 - R/logo.R | 20 ++++++++++---------- R/theme.R | 3 --- man/has_caption.Rd | 2 +- man/measure_caption.Rd | 4 ++-- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 3486ee2..68b993b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -41,7 +41,6 @@ export(z_gap_test) export(z_univariate) import(ggplot2) importFrom(ggplot2,theme) -importFrom(graphics,plot) importFrom(graphics,rasterImage) importFrom(grid,grid.draw) importFrom(grid,rasterGrob) diff --git a/R/logo.R b/R/logo.R index d44fbd8..f28c711 100644 --- a/R/logo.R +++ b/R/logo.R @@ -13,7 +13,7 @@ #' plot_jpeg(img) plot_jpeg <- function(path, add=FALSE, upscale = TRUE) { - jpg = readJPEG(path, native = T) # read the file + jpg = readJPEG(path, native = TRUE) # read the file res = dim(jpg)[2:1] # get the resolution, [x, y] if (upscale){ res <- res * 3 @@ -79,8 +79,8 @@ add_logo <- function(plot, logo, margin_param = NULL) { #' @export #' #' @examples -#' p1 <- ggplot2::qplot(mpg, wt, data = mtcars) -#' measure_caption(p1) # Should equal 1 since no caption is required +#' p1 <- ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) + ggplot2::geom_point() +#' measure_caption(p1) # Should equal 1 since no caption is present measure_caption <- function(gg) { if (has_caption(gg)) { stringr::str_count(gg$labels$caption, pattern = "\n") + 1 @@ -99,7 +99,7 @@ measure_caption <- function(gg) { #' @export #' #' @examples -#' p1 <- ggplot2::qplot(mpg, wt, data = mtcars) +#' p1 <- ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) + ggplot2::geom_point() #' has_caption(p1) # FALSE has_caption <- function(gg) { any(names(gg$labels) == "caption") @@ -132,7 +132,7 @@ add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL, margin_param = 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 <- measure_caption(plot_list[[1]]) # measure caption in first plot + 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")) @@ -145,9 +145,10 @@ add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL, margin_param = } if (nrow != 1) { - hold <- arrangeGrob(grobs = plot_list, nrow = nrow, ncol = 1, widths = widths) + if (!is.null(widths)) warning("`widths` is ignored when `nrow > 1`") + hold <- arrangeGrob(grobs = plot_list, nrow = nrow, ncol = 1) } else { - hold <- arrangeGrob(grobs = plot_list, nrow = 1, ncol = 2, widths = widths) + hold <- arrangeGrob(grobs = plot_list, nrow = 1, ncol = length(plot_list), widths = widths) } arrangeGrob(hold, logo, heights = c(0.93, .07)) @@ -164,10 +165,9 @@ add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL, margin_param = #' logo <- make_logo_grob() #' class(logo) # gg make_logo_grob <- function() { - logo_grob <- ggplot(mapping = aes(x = 0:1, y = 1)) + + ggplot() + theme_void() + annotation_custom(get_png(system.file("img", "civilytics_logo.png", - package="civilytics")), xmin= 0.7, xmax = 1) - logo_grob + package = "civilytics")), xmin = 0.7, xmax = 1) } diff --git a/R/theme.R b/R/theme.R index c081feb..36f0ab4 100644 --- a/R/theme.R +++ b/R/theme.R @@ -6,7 +6,6 @@ #' @param rel_small default 12/14, the scale factor to create a small font from the base font_size #' @param rel_tiny default 11/14, the scale factor to create a tiny font from the base font_size #' @param rel_large default 16/14, the scale factor to create a large font from the base font_size -#' @importFrom graphics plot #' @return a ggplot2 theme object suitable for combining with ggplot objects to theme them #' @export theme_civilytics <- @@ -92,9 +91,7 @@ theme_civilytics <- legend.key.height = NULL, legend.key.width = NULL, legend.text = element_text(size = rel(rel_small)), - legend.text.align = NULL, legend.title = element_text(hjust = 0), - legend.title.align = NULL, legend.position = "right", legend.direction = NULL, legend.justification = c("left", diff --git a/man/has_caption.Rd b/man/has_caption.Rd index 7293122..86d53fa 100644 --- a/man/has_caption.Rd +++ b/man/has_caption.Rd @@ -16,6 +16,6 @@ a logical, TRUE if a caption exists and FALSE if it does not Test whether a ggplot2 object has a caption } \examples{ -p1 <- ggplot2::qplot(mpg, wt, data = mtcars) +p1 <- ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) + ggplot2::geom_point() has_caption(p1) # FALSE } diff --git a/man/measure_caption.Rd b/man/measure_caption.Rd index 63f37f6..c42c4d0 100644 --- a/man/measure_caption.Rd +++ b/man/measure_caption.Rd @@ -17,6 +17,6 @@ the caption Measure a ggplot2 object caption } \examples{ -p1 <- ggplot2::qplot(mpg, wt, data = mtcars) -measure_caption(p1) # Should equal 1 since no caption is required +p1 <- ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) + ggplot2::geom_point() +measure_caption(p1) # Should equal 1 since no caption is present }