Compare commits
11
Commits
76aebe2b49
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b0a7b618a7
|
||
|
|
119aa7aa0f
|
||
|
|
f3ec052144
|
||
|
|
a2a0fce265
|
||
|
|
7bc547a995
|
||
|
|
c16e6cec19
|
||
|
|
dab960aaf1
|
||
|
|
dfcad3d0b8
|
||
|
|
0561df4221
|
||
|
|
d6e3ba2fb4
|
||
|
|
4f697e2d52
|
No files matched your search
@@ -2,9 +2,9 @@ name: R-CMD-check
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [master]
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
check:
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ review_guidelines = '''
|
||||
- Tests use testthat edition 3. Each test is self-sufficient -- no reliance on state
|
||||
left by an earlier test or on a fixture built elsewhere in the file.
|
||||
- Prefer duplication in tests over a helper that hides what is being asserted.
|
||||
- Brand colors come from `civilytics_colors`; never write a hex literal in theme, scale, logo, or table code. `R/flextable.R` still carries off-brand Bootstrap defaults (#2c3e50, #f0f0eb, #888888, #cccccc, #555555) -- do not add more.
|
||||
- Brand colors come from `civilytics_colors`; never write a hex literal in theme, scale, logo, or table code.
|
||||
- No tidyverse dependency. Imports is base R plus ggplot2, grid/gridExtra, png/jpeg, stringr, stringdist, showtext/sysfonts, jsonlite. Reject dplyr, purrr, magrittr, tibble, and data.table; use base R idioms.
|
||||
- NAMESPACE is roxygen-generated. Declare imports with `@importFrom` in the roxygen block and re-run roxygen; never hand-edit NAMESPACE.
|
||||
- Themes default to a transparent background (`paper_bg = FALSE`) so plots composite onto any Quarto, Reveal, or Typst background. Making an opaque background the default is a regression, not a preference.
|
||||
|
||||
@@ -47,8 +47,7 @@ tibble, or data.table. Prefer an existing dependency or base R over adding one
|
||||
this package is installed into other people's environments.
|
||||
|
||||
**Brand colors** live in `civilytics_colors` and nowhere else. Never write a hex
|
||||
literal in theme, scale, logo, or table code. (`R/flextable.R` still carries
|
||||
off-brand Bootstrap defaults; that is known debt, not a pattern to copy.)
|
||||
literal in theme, scale, logo, or table code.
|
||||
|
||||
**Themes** default to a transparent background (`paper_bg = FALSE`) so plots
|
||||
composite onto any Quarto, Reveal, or Typst background. Thread `ink`, `paper`, and
|
||||
@@ -84,7 +83,7 @@ Rscript -e 'devtools::test()'
|
||||
```
|
||||
|
||||
CI runs `R CMD check` on `rocker/r-ver:4.6` via `.gitea/workflows/check.yaml` for
|
||||
every push and PR to `master`.
|
||||
every push and PR to `main`.
|
||||
|
||||
## Release
|
||||
|
||||
|
||||
+4
-2
@@ -1,7 +1,7 @@
|
||||
Package: civilytics
|
||||
Type: Package
|
||||
Title: Brand Themes, Color Palettes, and Utility Functions for Civilytics
|
||||
Version: 0.3.1
|
||||
Version: 0.3.2
|
||||
Authors@R:
|
||||
person("Jared", "E. Knowles", email = "jared@civilytics.com",
|
||||
role = c("aut", "cre"))
|
||||
@@ -34,7 +34,9 @@ Suggests:
|
||||
quarto,
|
||||
flextable,
|
||||
officer,
|
||||
ragg
|
||||
patchwork,
|
||||
ragg,
|
||||
withr
|
||||
Config/testthat/edition: 3
|
||||
Config/roxygen2/version: 8.0.0
|
||||
RoxygenNote: 7.3.3
|
||||
@@ -1,11 +1,41 @@
|
||||
# civilytics 0.3.2
|
||||
|
||||
## New features
|
||||
- `civilytics_logo()` and `add_logo()` gain `logo_height` (inches, default `0.5`),
|
||||
the height of a band reserved for the logo.
|
||||
- `civilytics_load_fonts()` gains `showtext` (default `getOption("civilytics.showtext", TRUE)`), so ragg
|
||||
users can register the brand fonts without turning on showtext, which draws text
|
||||
at a third of its size on a 300 dpi ragg device. The default is unchanged (#29).
|
||||
- `patchwork` is now a suggested package, used when a patchwork composition is
|
||||
passed to `civilytics_logo()`.
|
||||
- Added comprehensive test coverage for the `prop_conf` module, including
|
||||
correctness checks against `binom.test()` for Clopper-Pearson intervals and
|
||||
formula-based verification for Wald and Agresti-Coull intervals.
|
||||
|
||||
## Bug fixes
|
||||
- `civilytics_logo()` / `add_logo()` now render every panel and the
|
||||
`plot_annotation()` of a patchwork composition. They previously drew only the
|
||||
last panel (#20).
|
||||
- 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).
|
||||
- `font_scale` now defaults to `1` in `civilytics_logo()`, `add_logo()` and
|
||||
`add_logo_ga()`. The old default of `1.1` enlarged the base text by 10% whenever
|
||||
a logo was added. Pass `font_scale = 1.1` to keep the old sizes (#20).
|
||||
- `style_flextable_civilytics()` defaults now come from `civilytics_colors`
|
||||
instead of Bootstrap hex values: header `navy_700`, zebra `paper_2`, outer
|
||||
border `rule_strong`, inner border `rule`, footer text `ink_3`. Tables built
|
||||
with the defaults change appearance (#21).
|
||||
- `civilytics-reveal`: PDF output keeps slide margins, ordered-list numbers no
|
||||
longer clip at the left edge, and Quarto column widths (`.column width="60%"`)
|
||||
are honored. Columns without a width stay equal, and the default gap is now
|
||||
`1.5em` (#25, #26, #27).
|
||||
- Removed vendored headshot images (`Knowles_Headshot_2019_good.jpg`,
|
||||
`Knowles_Headshot_2019_prisma.jpg`) from `inst/img/` to prevent personal
|
||||
photos from being distributed with the package. The `plot_jpeg()` example
|
||||
|
||||
+12
-10
@@ -15,7 +15,8 @@
|
||||
#' `flextable` is unavailable.
|
||||
#'
|
||||
#' @param ft A [flextable::flextable()] object.
|
||||
#' @param header_bg Character. Header background fill. Default `"#2c3e50"`.
|
||||
#' @param header_bg Character. Header background fill. Default
|
||||
#' `civilytics_colors[["navy_700"]]`.
|
||||
#' @param header_color Character. Header text color. Default `"white"`.
|
||||
#' @param title_fontsize Numeric. Font size for the first header line (the
|
||||
#' title row, `i = 1`). Default `14`.
|
||||
@@ -25,17 +26,18 @@
|
||||
#' @param zebra Logical. Apply alternating-row striping to even body rows.
|
||||
#' Default `TRUE`. Safely skipped for tables with fewer than two body rows.
|
||||
#' @param zebra_bg Character. Fill color for striped (even) body rows.
|
||||
#' Default `"#f0f0eb"`.
|
||||
#' Default `civilytics_colors[["paper_2"]]`.
|
||||
#' @param outer_border_color Character. Outer border color. Default
|
||||
#' `"#888888"`.
|
||||
#' `civilytics_colors[["rule_strong"]]`.
|
||||
#' @param outer_border_width Numeric. Outer border width. Default `1`.
|
||||
#' @param inner_border_color Character. Inner horizontal border color (body).
|
||||
#' Default `"#cccccc"`.
|
||||
#' Default `civilytics_colors[["rule"]]`.
|
||||
#' @param inner_border_width Numeric. Inner horizontal border width. Default
|
||||
#' `0.5`.
|
||||
#' @param footer_fontsize Numeric. Footer font size (applied only if a footer
|
||||
#' part exists). Default `9`.
|
||||
#' @param footer_color Character. Footer text color. Default `"#555555"`.
|
||||
#' @param footer_color Character. Footer text color. Default
|
||||
#' `civilytics_colors[["ink_3"]]`.
|
||||
#'
|
||||
#' @return The styled [flextable::flextable()] object.
|
||||
#' @export
|
||||
@@ -50,19 +52,19 @@
|
||||
#' style_flextable_civilytics()
|
||||
#' }
|
||||
style_flextable_civilytics <- function(ft,
|
||||
header_bg = "#2c3e50",
|
||||
header_bg = civilytics_colors[["navy_700"]],
|
||||
header_color = "white",
|
||||
title_fontsize = 14,
|
||||
body_fontsize = 11,
|
||||
font_name = "Arial",
|
||||
zebra = TRUE,
|
||||
zebra_bg = "#f0f0eb",
|
||||
outer_border_color = "#888888",
|
||||
zebra_bg = civilytics_colors[["paper_2"]],
|
||||
outer_border_color = civilytics_colors[["rule_strong"]],
|
||||
outer_border_width = 1,
|
||||
inner_border_color = "#cccccc",
|
||||
inner_border_color = civilytics_colors[["rule"]],
|
||||
inner_border_width = 0.5,
|
||||
footer_fontsize = 9,
|
||||
footer_color = "#555555") {
|
||||
footer_color = civilytics_colors[["ink_3"]]) {
|
||||
if (!requireNamespace("flextable", quietly = TRUE)) {
|
||||
stop("Install 'flextable' to use this function.", call. = FALSE)
|
||||
}
|
||||
|
||||
@@ -14,16 +14,26 @@ CV_FONT_MONO <- "JetBrains Mono" # code, data tables, numeric callouts
|
||||
#' Load Civilytics brand fonts
|
||||
#'
|
||||
#' Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from
|
||||
#' Google Fonts via [sysfonts::font_add_google()], then calls
|
||||
#' [showtext::showtext_auto()] so that all graphics devices render text with
|
||||
#' those fonts. This is called automatically when the package loads; use this
|
||||
#' function to retry if the initial load failed (e.g., the machine was offline
|
||||
#' at load time).
|
||||
#' Google Fonts via [sysfonts::font_add_google()], then, unless
|
||||
#' `showtext = FALSE`, calls [showtext::showtext_auto()] so that all graphics
|
||||
#' devices render text with those fonts. This is called automatically when the
|
||||
#' package loads; use this function to retry if the initial load failed (e.g.,
|
||||
#' the machine was offline at load time).
|
||||
#'
|
||||
#' Subsequent calls within the same session are no-ops unless `force = TRUE`.
|
||||
#'
|
||||
#' **Using ragg.** [showtext::showtext_auto()] draws text at 96 dpi whatever the
|
||||
#' device resolution, so on a `ragg::agg_png(res = 300)` device text comes out
|
||||
#' at about a third of its requested size. For ragg, set
|
||||
#' `options(civilytics.showtext = FALSE)` before loading the package, or call
|
||||
#' `showtext::showtext_auto(FALSE)` afterwards. The brand fonts must then be
|
||||
#' installed on the system, because ragg reads system fonts rather than the
|
||||
#' ones registered through sysfonts.
|
||||
#'
|
||||
#' @param force Logical. If `TRUE`, reload fonts even if they were already
|
||||
#' loaded in this session. Default `FALSE`.
|
||||
#' @param showtext Logical. Turn on [showtext::showtext_auto()] after
|
||||
#' registering the fonts. Default is `getOption("civilytics.showtext", TRUE)`.
|
||||
#'
|
||||
#' @return Invisibly returns `TRUE` if fonts were loaded, `FALSE` if skipped
|
||||
#' (already loaded and `force = FALSE`).
|
||||
@@ -32,15 +42,22 @@ CV_FONT_MONO <- "JetBrains Mono" # code, data tables, numeric callouts
|
||||
#' @examples
|
||||
#' \dontrun{
|
||||
#' civilytics_load_fonts()
|
||||
#'
|
||||
#' # For ragg devices: register the fonts but leave showtext off
|
||||
#' civilytics_load_fonts(force = TRUE, showtext = FALSE)
|
||||
#' }
|
||||
civilytics_load_fonts <- function(force = FALSE) {
|
||||
civilytics_load_fonts <- function(force = FALSE,
|
||||
showtext = getOption("civilytics.showtext", TRUE)) {
|
||||
if (!is.logical(showtext) || length(showtext) != 1L || is.na(showtext)) {
|
||||
stop("`showtext` must be a single TRUE or FALSE.", call. = FALSE)
|
||||
}
|
||||
if (.cv_state$fonts_loaded && !force) return(invisible(FALSE))
|
||||
|
||||
sysfonts::font_add_google("Inter", family = "Inter")
|
||||
sysfonts::font_add_google("Libre Franklin", family = "Libre Franklin")
|
||||
sysfonts::font_add_google("Source Serif 4", family = "Source Serif 4")
|
||||
sysfonts::font_add_google("JetBrains Mono", family = "JetBrains Mono")
|
||||
showtext::showtext_auto()
|
||||
if (showtext) showtext::showtext_auto()
|
||||
|
||||
.cv_state$fonts_loaded <- TRUE
|
||||
invisible(TRUE)
|
||||
|
||||
@@ -44,65 +44,67 @@ get_png <- function(filename) {
|
||||
|
||||
#' Add a logo to a ggplot2 object
|
||||
#'
|
||||
#' @param plot a ggplot2 grob
|
||||
#' The logo gets its own band of reserved height (`logo_height`, in inches)
|
||||
#' below or above the plot, so it cannot overlap the plot's caption or push
|
||||
#' anything off the canvas. Patchwork compositions are converted with
|
||||
#' [patchwork::patchworkGrob()] so every panel and the
|
||||
#' [patchwork::plot_annotation()] survive.
|
||||
#'
|
||||
#' @param plot a ggplot2 object, or a patchwork composition
|
||||
#' @param logo a logo grob created by make_logo_grob()
|
||||
#' @param margin_param a numeric specifying what margin to add or subtract to align the logo
|
||||
#' @param font_scale Numeric. Multiplicative scaling factor applied to text
|
||||
#' sizes before composing the plot with the logo. Default `1.1` inflates
|
||||
#' text by ~10 \% to compensate for the viewport shrinkage caused by
|
||||
#' [gridExtra::arrangeGrob()]. Set to `1` to disable.
|
||||
#' @param margin_param Optional numeric. Replaces the plot margin (in points)
|
||||
#' on the side next to the logo. `NULL` (default) leaves the plot's margins
|
||||
#' alone.
|
||||
#' @param font_scale Numeric. Multiplicative scaling factor applied to the
|
||||
#' base text size before composing. Default `1` leaves typography unchanged.
|
||||
#' @param position Character. Corner placement for the logo: `"bottom-right"`
|
||||
#' (default), `"bottom-left"`, `"top-right"`, or `"top-left"`. Controls
|
||||
#' whether the logo is placed above or below the plot.
|
||||
#' @param logo_height Numeric. Height in inches of the band reserved for the
|
||||
#' logo. Default `0.5`.
|
||||
#'
|
||||
#' @return a grob with a logo attached to it ready to plot
|
||||
#' @importFrom ggplot2 theme
|
||||
#' @importFrom gridExtra arrangeGrob
|
||||
#' @export
|
||||
add_logo <- function(plot, logo, margin_param = NULL, font_scale = 1.1,
|
||||
add_logo <- function(plot, logo, margin_param = NULL, font_scale = 1,
|
||||
position = c("bottom-right", "bottom-left",
|
||||
"top-right", "top-left")) {
|
||||
"top-right", "top-left"),
|
||||
logo_height = 0.5) {
|
||||
position <- match.arg(position)
|
||||
at_top <- grepl("top", position)
|
||||
is_patchwork <- inherits(plot, "patchwork")
|
||||
|
||||
# Capture the plot's background color so we can fill the entire composed
|
||||
# grob with it. The logo grob must stay transparent (theme_void) so that
|
||||
# caption/axis text overlapping via negative margins remains visible.
|
||||
# grob with it. The logo grob stays transparent (theme_void).
|
||||
bg_fill <- plot$theme$plot.background$fill
|
||||
|
||||
# Inflate text sizes to compensate for arrangeGrob viewport shrinkage.
|
||||
# All theme text elements use rel() sizing, so scaling the root 'text'
|
||||
# element cascades to titles, axis labels, legends, captions, and strips.
|
||||
if (!is.null(font_scale) && font_scale != 1) {
|
||||
base_size <- plot$theme$text$size %||% 14
|
||||
plot <- plot + theme(
|
||||
text = element_text(size = base_size * font_scale)
|
||||
)
|
||||
base_size <- plot$theme$text$size
|
||||
if (is.null(base_size)) base_size <- 14
|
||||
scaled <- theme(text = ggplot2::element_text(size = base_size * font_scale))
|
||||
plot <- if (is_patchwork) plot & scaled else plot + scaled
|
||||
}
|
||||
|
||||
if (at_top) {
|
||||
# For top placement, tighten the top margin of the plot
|
||||
if (!is.null(margin_param)) {
|
||||
plot <- plot + theme(plot.margin = unit(c(margin_param, 7, 7, 7), "pt"))
|
||||
} else {
|
||||
plot <- plot + theme(plot.margin = unit(c(-14, 7, 7, 7), "pt"))
|
||||
side <- if (at_top) c(margin_param, 7, 7, 7) else c(7, 7, margin_param, 7)
|
||||
margin <- theme(plot.margin = grid::unit(side, "pt"))
|
||||
plot <- if (is_patchwork) plot & margin else plot + margin
|
||||
}
|
||||
composed <- arrangeGrob(logo, plot, heights = c(0.1, 0.93),
|
||||
padding = unit(0.1, "line"))
|
||||
|
||||
# ggplot_build() on a patchwork builds only its last panel, so convert it
|
||||
# explicitly before handing it to arrangeGrob().
|
||||
plot_grob <- if (is_patchwork) patchwork::patchworkGrob(plot) else plot
|
||||
|
||||
plot_height <- grid::unit(1, "null")
|
||||
band_height <- grid::unit(logo_height, "in")
|
||||
composed <- if (at_top) {
|
||||
arrangeGrob(logo, plot_grob, heights = grid::unit.c(band_height, plot_height),
|
||||
padding = grid::unit(0.1, "line"))
|
||||
} else {
|
||||
# Bottom placement (original behaviour)
|
||||
if(has_caption(plot)) {
|
||||
cap_lines <- measure_caption(plot)
|
||||
if (!is.null(margin_param)) {
|
||||
plot <- plot + theme(plot.margin = unit(c(7, 7, margin_param, 7), "pt"))
|
||||
} else {
|
||||
plot <- plot + theme(plot.margin = unit(c(7, 7, cap_lines * -52, 7), "pt"))
|
||||
}
|
||||
} else {
|
||||
plot <- plot + theme(plot.margin = unit(c(7, 7, -14, 7), "pt"))
|
||||
}
|
||||
composed <- arrangeGrob(plot, logo, heights = c(0.93, 0.1),
|
||||
padding = unit(0.1, "line"))
|
||||
arrangeGrob(plot_grob, logo,
|
||||
heights = grid::unit.c(plot_height, band_height),
|
||||
padding = grid::unit(0.1, "line"))
|
||||
}
|
||||
|
||||
# Wrap with a full-bleed background rect so any transparent areas (the
|
||||
@@ -158,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.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
|
||||
#' @note The resulting object needs to be drawn to the screen using grid.draw()
|
||||
@@ -179,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.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
|
||||
@@ -193,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)) {
|
||||
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) {
|
||||
if (!is.null(widths)) warning("`widths` is ignored when `nrow > 1`")
|
||||
@@ -216,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))
|
||||
@@ -268,20 +272,20 @@ make_logo_grob <- function(type = c("wordmark", "mark"),
|
||||
|
||||
align_left <- grepl("left", position)
|
||||
|
||||
if (align_left) {
|
||||
xmin <- 0
|
||||
xmax <- if (type == "mark") 0.07 else 0.35
|
||||
} else {
|
||||
xmin <- if (type == "mark") 0.93 else 0.65
|
||||
xmax <- 1
|
||||
}
|
||||
# Give the raster a height only, so grid derives its width from the image's
|
||||
# own aspect ratio, then pin it to the corner. The logo is the same shape on
|
||||
# any canvas width.
|
||||
logo <- grid::rasterGrob(
|
||||
png::readPNG(system.file("img", img_file, package = "civilytics")),
|
||||
x = if (align_left) grid::unit(7, "pt") else grid::unit(1, "npc") - grid::unit(7, "pt"),
|
||||
just = if (align_left) "left" else "right",
|
||||
height = grid::unit(1, "npc"),
|
||||
interpolate = TRUE
|
||||
)
|
||||
|
||||
ggplot() +
|
||||
theme_void() +
|
||||
annotation_custom(
|
||||
get_png(system.file("img", img_file, package = "civilytics")),
|
||||
xmin = xmin, xmax = xmax
|
||||
)
|
||||
annotation_custom(logo, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
|
||||
}
|
||||
|
||||
#' Add a Civilytics logo to a ggplot (pipe-friendly)
|
||||
@@ -308,9 +312,10 @@ make_logo_grob <- function(type = c("wordmark", "mark"),
|
||||
#' (default), `"bottom-left"`, `"top-right"`, or `"top-left"`.
|
||||
#' @param margin_param Numeric or `NULL`. Manual margin adjustment passed to
|
||||
#' [add_logo()].
|
||||
#' @param font_scale Numeric. Inflate text sizes by this factor to compensate
|
||||
#' for viewport shrinkage when composing with [gridExtra::arrangeGrob()].
|
||||
#' Default `1.1` (~10 \% inflation). Set to `1` to disable.
|
||||
#' @param font_scale Numeric. Multiplies the plot's base text size before
|
||||
#' composing. Default `1` leaves typography unchanged.
|
||||
#' @param logo_height Numeric. Height in inches of the band reserved for the
|
||||
#' logo. Default `0.5`.
|
||||
#'
|
||||
#' @return A grob (from [gridExtra::arrangeGrob()]) ready to draw with
|
||||
#' [grid::grid.draw()].
|
||||
@@ -355,11 +360,12 @@ civilytics_logo <- function(plot,
|
||||
position = c("bottom-right", "bottom-left",
|
||||
"top-right", "top-left"),
|
||||
margin_param = NULL,
|
||||
font_scale = 1.1) {
|
||||
font_scale = 1,
|
||||
logo_height = 0.5) {
|
||||
position <- match.arg(position)
|
||||
logo <- make_logo_grob(type = type, variant = variant, position = position)
|
||||
add_logo(plot, logo, margin_param = margin_param, font_scale = font_scale,
|
||||
position = position)
|
||||
position = position, logo_height = logo_height)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -209,7 +209,11 @@ $code-color: #923D00 !default;
|
||||
height: 2px;
|
||||
background: var(--accent-600);
|
||||
}
|
||||
.reveal ol { list-style: decimal; }
|
||||
.reveal ol {
|
||||
list-style: decimal;
|
||||
/* Room for two-digit markers in the bold mono face (#27) */
|
||||
padding-left: 1.6em;
|
||||
}
|
||||
.reveal ol > li::marker {
|
||||
color: var(--accent-600);
|
||||
font-family: var(--font-mono);
|
||||
@@ -747,7 +751,7 @@ img.civilytics-wordmark,
|
||||
----------------------------------------------------------- */
|
||||
.reveal .columns {
|
||||
display: flex;
|
||||
gap: 3em;
|
||||
gap: 1.5em;
|
||||
align-items: flex-start;
|
||||
margin: 0.25em 0;
|
||||
}
|
||||
@@ -755,6 +759,11 @@ img.civilytics-wordmark,
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
/* Quarto writes width="60%" as an inline style; honor it (#26). Columns
|
||||
without a width stay equal. */
|
||||
.reveal .column[style*="width"] {
|
||||
flex: 0 1 auto;
|
||||
}
|
||||
.reveal .column > :first-child { margin-top: 0; }
|
||||
|
||||
/* Hand-rolled two-col for pandoc (non-Quarto) users */
|
||||
@@ -1040,6 +1049,15 @@ body.civilytics-hide-chrome .slide-number {
|
||||
.reveal section { break-inside: avoid; }
|
||||
}
|
||||
|
||||
/* reveal.js print-pdf zeroes section padding with !important, so the screen
|
||||
padding is lost. Restore a smaller print value; 48px 72px pushed dense
|
||||
slides onto extra pages (#25). */
|
||||
html.print-pdf .reveal .slides section:not(.full-bleed):not(.stack),
|
||||
html.reveal-print .reveal .slides section:not(.full-bleed):not(.stack) {
|
||||
padding: 32px 56px !important;
|
||||
box-sizing: border-box !important;
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------
|
||||
SMALL-SCREEN GUARDRAILS
|
||||
Reveal scales slides, but the narrowest keep-legible margins.
|
||||
|
||||
+17
-9
@@ -8,29 +8,37 @@ add_logo(
|
||||
plot,
|
||||
logo,
|
||||
margin_param = NULL,
|
||||
font_scale = 1.1,
|
||||
position = c("bottom-right", "bottom-left", "top-right", "top-left")
|
||||
font_scale = 1,
|
||||
position = c("bottom-right", "bottom-left", "top-right", "top-left"),
|
||||
logo_height = 0.5
|
||||
)
|
||||
}
|
||||
\arguments{
|
||||
\item{plot}{a ggplot2 grob}
|
||||
\item{plot}{a ggplot2 object, or a patchwork composition}
|
||||
|
||||
\item{logo}{a logo grob created by make_logo_grob()}
|
||||
|
||||
\item{margin_param}{a numeric specifying what margin to add or subtract to align the logo}
|
||||
\item{margin_param}{Optional numeric. Replaces the plot margin (in points)
|
||||
on the side next to the logo. `NULL` (default) leaves the plot's margins
|
||||
alone.}
|
||||
|
||||
\item{font_scale}{Numeric. Multiplicative scaling factor applied to text
|
||||
sizes before composing the plot with the logo. Default `1.1` inflates
|
||||
text by ~10 \% to compensate for the viewport shrinkage caused by
|
||||
[gridExtra::arrangeGrob()]. Set to `1` to disable.}
|
||||
\item{font_scale}{Numeric. Multiplicative scaling factor applied to the
|
||||
base text size before composing. Default `1` leaves typography unchanged.}
|
||||
|
||||
\item{position}{Character. Corner placement for the logo: `"bottom-right"`
|
||||
(default), `"bottom-left"`, `"top-right"`, or `"top-left"`. Controls
|
||||
whether the logo is placed above or below the plot.}
|
||||
|
||||
\item{logo_height}{Numeric. Height in inches of the band reserved for the
|
||||
logo. Default `0.5`.}
|
||||
}
|
||||
\value{
|
||||
a grob with a logo attached to it ready to plot
|
||||
}
|
||||
\description{
|
||||
Add a logo to a ggplot2 object
|
||||
The logo gets its own band of reserved height (`logo_height`, in inches)
|
||||
below or above the plot, so it cannot overlap the plot's caption or push
|
||||
anything off the canvas. Patchwork compositions are converted with
|
||||
[patchwork::patchworkGrob()] so every panel and the
|
||||
[patchwork::plot_annotation()] survive.
|
||||
}
|
||||
+8
-3
@@ -10,7 +10,8 @@ add_logo_ga(
|
||||
nrow = 1,
|
||||
widths = NULL,
|
||||
margin_param = NULL,
|
||||
font_scale = 1.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.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{
|
||||
a grid object
|
||||
|
||||
@@ -4,11 +4,17 @@
|
||||
\alias{civilytics_load_fonts}
|
||||
\title{Load Civilytics brand fonts}
|
||||
\usage{
|
||||
civilytics_load_fonts(force = FALSE)
|
||||
civilytics_load_fonts(
|
||||
force = FALSE,
|
||||
showtext = getOption("civilytics.showtext", TRUE)
|
||||
)
|
||||
}
|
||||
\arguments{
|
||||
\item{force}{Logical. If \code{TRUE}, reload fonts even if they were already
|
||||
loaded in this session. Default \code{FALSE}.}
|
||||
|
||||
\item{showtext}{Logical. Turn on \code{showtext::showtext_auto()} after
|
||||
registering the fonts. Default is \code{getOption("civilytics.showtext", TRUE)}.}
|
||||
}
|
||||
\value{
|
||||
Invisibly returns \code{TRUE} if fonts were loaded, \code{FALSE} if skipped
|
||||
@@ -16,16 +22,28 @@ Invisibly returns \code{TRUE} if fonts were loaded, \code{FALSE} if skipped
|
||||
}
|
||||
\description{
|
||||
Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from
|
||||
Google Fonts via \code{sysfonts::font_add_google()}, then calls
|
||||
\code{showtext::showtext_auto()} so that all graphics devices render text with
|
||||
those fonts. This is called automatically when the package loads; use this
|
||||
function to retry if the initial load failed (e.g., the machine was offline
|
||||
at load time).
|
||||
|
||||
Google Fonts via \code{sysfonts::font_add_google()}, then, unless
|
||||
\code{showtext = FALSE}, calls \code{showtext::showtext_auto()} so that all graphics
|
||||
devices render text with those fonts. This is called automatically when the
|
||||
package loads; use this function to retry if the initial load failed (e.g.,
|
||||
the machine was offline at load time).
|
||||
}
|
||||
\details{
|
||||
Subsequent calls within the same session are no-ops unless \code{force = TRUE}.
|
||||
|
||||
\strong{Using ragg.} \code{showtext::showtext_auto()} draws text at 96 dpi whatever the
|
||||
device resolution, so on a \code{ragg::agg_png(res = 300)} device text comes out
|
||||
at about a third of its requested size. For ragg, set
|
||||
\code{options(civilytics.showtext = FALSE)} before loading the package, or call
|
||||
\code{showtext::showtext_auto(FALSE)} afterwards. The brand fonts must then be
|
||||
installed on the system, because ragg reads system fonts rather than the
|
||||
ones registered through sysfonts.
|
||||
}
|
||||
\examples{
|
||||
\dontrun{
|
||||
civilytics_load_fonts()
|
||||
|
||||
# For ragg devices: register the fonts but leave showtext off
|
||||
civilytics_load_fonts(force = TRUE, showtext = FALSE)
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,8 @@ civilytics_logo(
|
||||
variant = c("light", "dark"),
|
||||
position = c("bottom-right", "bottom-left", "top-right", "top-left"),
|
||||
margin_param = NULL,
|
||||
font_scale = 1.1
|
||||
font_scale = 1,
|
||||
logo_height = 0.5
|
||||
)
|
||||
}
|
||||
\arguments{
|
||||
@@ -28,9 +29,11 @@ civilytics_logo(
|
||||
\item{margin_param}{Numeric or `NULL`. Manual margin adjustment passed to
|
||||
[add_logo()].}
|
||||
|
||||
\item{font_scale}{Numeric. Inflate text sizes by this factor to compensate
|
||||
for viewport shrinkage when composing with [gridExtra::arrangeGrob()].
|
||||
Default `1.1` (~10 \% inflation). Set to `1` to disable.}
|
||||
\item{font_scale}{Numeric. Multiplies the plot's base text size before
|
||||
composing. Default `1` leaves typography unchanged.}
|
||||
|
||||
\item{logo_height}{Numeric. Height in inches of the band reserved for the
|
||||
logo. Default `0.5`.}
|
||||
}
|
||||
\value{
|
||||
A grob (from [gridExtra::arrangeGrob()]) ready to draw with
|
||||
|
||||
@@ -6,25 +6,26 @@
|
||||
\usage{
|
||||
style_flextable_civilytics(
|
||||
ft,
|
||||
header_bg = "#2c3e50",
|
||||
header_bg = civilytics_colors[["navy_700"]],
|
||||
header_color = "white",
|
||||
title_fontsize = 14,
|
||||
body_fontsize = 11,
|
||||
font_name = "Arial",
|
||||
zebra = TRUE,
|
||||
zebra_bg = "#f0f0eb",
|
||||
outer_border_color = "#888888",
|
||||
zebra_bg = civilytics_colors[["paper_2"]],
|
||||
outer_border_color = civilytics_colors[["rule_strong"]],
|
||||
outer_border_width = 1,
|
||||
inner_border_color = "#cccccc",
|
||||
inner_border_color = civilytics_colors[["rule"]],
|
||||
inner_border_width = 0.5,
|
||||
footer_fontsize = 9,
|
||||
footer_color = "#555555"
|
||||
footer_color = civilytics_colors[["ink_3"]]
|
||||
)
|
||||
}
|
||||
\arguments{
|
||||
\item{ft}{A [flextable::flextable()] object.}
|
||||
|
||||
\item{header_bg}{Character. Header background fill. Default `"#2c3e50"`.}
|
||||
\item{header_bg}{Character. Header background fill. Default
|
||||
`civilytics_colors[["navy_700"]]`.}
|
||||
|
||||
\item{header_color}{Character. Header text color. Default `"white"`.}
|
||||
|
||||
@@ -40,15 +41,15 @@ title row, `i = 1`). Default `14`.}
|
||||
Default `TRUE`. Safely skipped for tables with fewer than two body rows.}
|
||||
|
||||
\item{zebra_bg}{Character. Fill color for striped (even) body rows.
|
||||
Default `"#f0f0eb"`.}
|
||||
Default `civilytics_colors[["paper_2"]]`.}
|
||||
|
||||
\item{outer_border_color}{Character. Outer border color. Default
|
||||
`"#888888"`.}
|
||||
`civilytics_colors[["rule_strong"]]`.}
|
||||
|
||||
\item{outer_border_width}{Numeric. Outer border width. Default `1`.}
|
||||
|
||||
\item{inner_border_color}{Character. Inner horizontal border color (body).
|
||||
Default `"#cccccc"`.}
|
||||
Default `civilytics_colors[["rule"]]`.}
|
||||
|
||||
\item{inner_border_width}{Numeric. Inner horizontal border width. Default
|
||||
`0.5`.}
|
||||
@@ -56,7 +57,8 @@ Default `"#cccccc"`.}
|
||||
\item{footer_fontsize}{Numeric. Footer font size (applied only if a footer
|
||||
part exists). Default `9`.}
|
||||
|
||||
\item{footer_color}{Character. Footer text color. Default `"#555555"`.}
|
||||
\item{footer_color}{Character. Footer text color. Default
|
||||
`civilytics_colors[["ink_3"]]`.}
|
||||
}
|
||||
\value{
|
||||
The styled [flextable::flextable()] object.
|
||||
|
||||
@@ -13,6 +13,18 @@ file carries the reasoning and the pointers.
|
||||
|
||||
---
|
||||
|
||||
## 2026-10-15 · packaging · default branch renamed to main; add_logo_ga band and showtext switch
|
||||
|
||||
**Why:** `Closes` keywords only fire on the default branch, so the rename came first; #28 and #29 finished the logo work, and #29 took the opt-out switch because changing the showtext default would alter published reports we cannot see from here.
|
||||
**Obligates:** none; turning showtext off by default is a future decision if consumers allow it
|
||||
**Refs:** #28, #29, f3ec052, 119aa7a, 7bc547a
|
||||
|
||||
## 2026-10-15 · logo · logo band, patchwork panels, brand table colors, reveal print fixes
|
||||
|
||||
**Why:** figures with captions were shipping clipped and patchwork figures lost panels (#20); the table default and reveal print/column bugs were off-brand or broken in published decks. Default changes (font_scale 1, brand table colors) alter existing output, so this is 0.3.2.
|
||||
**Obligates:** PDF check of #25/#27 in another project (never rendered here)
|
||||
**Refs:** #20, #21, #22, #24, #25, #26, #27, 4f697e2, d6e3ba2, 0561df4, dfcad3d
|
||||
|
||||
## 2026-08-24 · packaging · roborev exclusion patterns fixed; init review triaged
|
||||
|
||||
**Why:** roborev reviewed the init commit because `excluded_commit_patterns` are
|
||||
|
||||
+9
-34
@@ -7,59 +7,34 @@
|
||||
|
||||
## Where this stands
|
||||
|
||||
`civilytics` is the house R package behind every Civilytics chart, table, and report —
|
||||
ggplot2 themes, brand palettes, logo composition, and Quarto templates, plus the
|
||||
data-wrangling helpers the analysis work leans on. It is at version 0.3.1 and already
|
||||
depended on by live projects, so changes here surface in reports that are published.
|
||||
|
||||
Project tracking went in on 23 August. Five workstreams divide the package by how it
|
||||
actually goes stale rather than by directory: themes and palettes, logo and branded
|
||||
output, Quarto templates, analysis helpers, and packaging. Documentation debt is caught
|
||||
automatically — if code moves and its roxygen pages do not, an issue gets filed.
|
||||
|
||||
Automated review is running, with one sharp edge now documented. The exclusion patterns
|
||||
that decide which commits get reviewed are matched against the whole commit message, not
|
||||
just the subject, so a commit whose body merely mentions an excluded prefix is skipped
|
||||
with no record anywhere. That is filed as #24. It is not urgent, but it is the kind of
|
||||
silence that is expensive to diagnose cold.
|
||||
|
||||
Four things are open, none blocked. A logo defect (#20) drops patchwork panels and clips
|
||||
captions, which is the one that bites users. The flextable styling helper (#21) ships
|
||||
Bootstrap's palette as its default instead of the brand's, so tables built with the
|
||||
defaults are quietly off-brand. The remaining two are housekeeping: duplicate `kodor/*`
|
||||
labels at repo and org scope (#22), and the review-skip trap (#24).
|
||||
|
||||
CI passes on `04a5ee9`. The next substantive work is #20.
|
||||
Every issue that was open is closed, and the default branch is `main`. The 0.3.2 changes are all pushed: the logo and `add_logo_ga()` reserve their own band so captions are not clipped, patchwork figures keep every panel, flextable defaults use brand colors, and `civilytics_load_fonts()` has a switch for ragg users who do not want showtext. The showtext default is unchanged, so turning it off by default is a separate decision that depends on which reports rely on it. The reveal print fixes still need a look in a rendered PDF, which another project will do.
|
||||
|
||||
## Ready to work on next
|
||||
|
||||
- **#20** civilytics_logo(): drops patchwork panels, clips captions, and silently rescales fonts · `ws/logo` — nothing is blocking it; something is currently wrong
|
||||
- **#21** style_flextable_civilytics() defaults to Bootstrap colors, not Civilytics brand · `ws/logo` — nothing is blocking it; owed work from an earlier change
|
||||
- **#22** kodor/* labels are defined at both repo and org scope · `ws/packaging` — nothing is blocking it
|
||||
- **#24** roborev exclusion patterns match the whole commit message, causing silent skips · `ws/packaging` — nothing is blocking it
|
||||
_Nothing is open and unblocked._
|
||||
|
||||
## Workstreams
|
||||
|
||||
| Stream | Commits since | Open | Debt | Owes docs |
|
||||
|---|---|---|---|---|
|
||||
| Themes, palettes, and fonts | 0 | 0 | 0 | no |
|
||||
| Logo and branded output composition | 0 | 2 | 1 | no |
|
||||
| Themes, palettes, and fonts | 1 | 0 | 0 | no |
|
||||
| Logo and branded output composition | 1 | 0 | 0 | no |
|
||||
| Quarto themes and publishing templates | 0 | 0 | 0 | no |
|
||||
| Analysis and workflow helpers | 0 | 0 | 0 | no |
|
||||
| Package infrastructure and release | 0 | 2 | 0 | no |
|
||||
| Package infrastructure and release | 2 | 0 | 0 | no |
|
||||
|
||||
## CI
|
||||
|
||||

|
||||

|
||||
|
||||
<details>
|
||||
<summary>Dependency graph and detail</summary>
|
||||
|
||||
_Nothing blocks anything else, so there is no graph to draw._
|
||||
|
||||
- Marker: `04a5ee95` (2026-08-24)
|
||||
- Commits since: 0
|
||||
- Open issues: 4
|
||||
- Marker: `c16e6cec` (2026-10-15)
|
||||
- Commits since: 4
|
||||
- Open issues: 0
|
||||
|
||||
</details>
|
||||
|
||||
|
||||
+1
-1
@@ -95,7 +95,7 @@ docs = [
|
||||
|
||||
[roborev]
|
||||
project_guidelines = [
|
||||
"Brand colors come from `civilytics_colors`; never write a hex literal in theme, scale, logo, or table code. `R/flextable.R` still carries off-brand Bootstrap defaults (#2c3e50, #f0f0eb, #888888, #cccccc, #555555) -- do not add more.",
|
||||
"Brand colors come from `civilytics_colors`; never write a hex literal in theme, scale, logo, or table code.",
|
||||
"No tidyverse dependency. Imports is base R plus ggplot2, grid/gridExtra, png/jpeg, stringr, stringdist, showtext/sysfonts, jsonlite. Reject dplyr, purrr, magrittr, tibble, and data.table; use base R idioms.",
|
||||
"NAMESPACE is roxygen-generated. Declare imports with `@importFrom` in the roxygen block and re-run roxygen; never hand-edit NAMESPACE.",
|
||||
"Themes default to a transparent background (`paper_bg = FALSE`) so plots composite onto any Quarto, Reveal, or Typst background. Making an opaque background the default is a regression, not a preference.",
|
||||
|
||||
@@ -87,3 +87,21 @@ test_that("extra_height increases the exported PNG height", {
|
||||
|
||||
expect_gt(dim(png::readPNG(path_big))[1], dim(png::readPNG(path_small))[1])
|
||||
})
|
||||
|
||||
test_that("style_flextable_civilytics defaults come from civilytics_colors (#21)", {
|
||||
defaults <- formals(style_flextable_civilytics)
|
||||
expect_identical(eval(defaults$header_bg), civilytics_colors[["navy_700"]])
|
||||
expect_identical(eval(defaults$zebra_bg), civilytics_colors[["paper_2"]])
|
||||
expect_identical(eval(defaults$outer_border_color), civilytics_colors[["rule_strong"]])
|
||||
expect_identical(eval(defaults$inner_border_color), civilytics_colors[["rule"]])
|
||||
expect_identical(eval(defaults$footer_color), civilytics_colors[["ink_3"]])
|
||||
})
|
||||
|
||||
test_that("style_flextable_civilytics paints the header with brand navy", {
|
||||
skip_if_not_installed("flextable")
|
||||
skip_if_not_installed("officer")
|
||||
|
||||
styled <- style_flextable_civilytics(flextable::flextable(head(mtcars, 3)))
|
||||
header_fill <- unique(as.vector(styled$header$styles$cells$background.color$data))
|
||||
expect_identical(toupper(header_fill), toupper(civilytics_colors[["navy_700"]]))
|
||||
})
|
||||
@@ -0,0 +1,50 @@
|
||||
# civilytics_load_fonts() must not touch the network or global devices in tests:
|
||||
# both sysfonts::font_add_google() and showtext::showtext_auto() are mocked.
|
||||
with_font_mocks <- function(code, env = parent.frame()) {
|
||||
calls <- new.env()
|
||||
calls$showtext <- 0L
|
||||
calls$google <- character()
|
||||
old <- .cv_state$fonts_loaded
|
||||
.cv_state$fonts_loaded <- FALSE
|
||||
withr::defer(.cv_state$fonts_loaded <- old, envir = env)
|
||||
testthat::local_mocked_bindings(
|
||||
font_add_google = function(name, family = name, ...) {
|
||||
calls$google <- c(calls$google, family)
|
||||
},
|
||||
.package = "sysfonts", .env = env
|
||||
)
|
||||
testthat::local_mocked_bindings(
|
||||
showtext_auto = function(enable = TRUE) calls$showtext <- calls$showtext + 1L,
|
||||
.package = "showtext", .env = env
|
||||
)
|
||||
calls
|
||||
}
|
||||
|
||||
test_that("civilytics_load_fonts turns showtext on by default (#29)", {
|
||||
skip_if_not_installed("withr")
|
||||
calls <- with_font_mocks()
|
||||
expect_true(civilytics_load_fonts())
|
||||
expect_length(calls$google, 4)
|
||||
expect_equal(calls$showtext, 1L)
|
||||
})
|
||||
|
||||
test_that("showtext = FALSE registers fonts but leaves showtext off (#29)", {
|
||||
skip_if_not_installed("withr")
|
||||
calls <- with_font_mocks()
|
||||
expect_true(civilytics_load_fonts(showtext = FALSE))
|
||||
expect_length(calls$google, 4)
|
||||
expect_equal(calls$showtext, 0L)
|
||||
})
|
||||
|
||||
test_that("options(civilytics.showtext = FALSE) sets the default (#29)", {
|
||||
skip_if_not_installed("withr")
|
||||
calls <- with_font_mocks()
|
||||
withr::local_options(civilytics.showtext = FALSE)
|
||||
civilytics_load_fonts()
|
||||
expect_equal(calls$showtext, 0L)
|
||||
})
|
||||
|
||||
test_that("showtext must be a single TRUE or FALSE", {
|
||||
expect_error(civilytics_load_fonts(showtext = NA), "showtext")
|
||||
expect_error(civilytics_load_fonts(showtext = c(TRUE, FALSE)), "showtext")
|
||||
})
|
||||
@@ -28,3 +28,111 @@ test_that("stamp_logo_png validates its inputs", {
|
||||
# invalid type is rejected by match.arg() before the file is touched
|
||||
expect_error(stamp_logo_png(tempfile(fileext = ".png"), type = "banner"))
|
||||
})
|
||||
|
||||
# --- civilytics_logo() layout (#20) ------------------------------------------
|
||||
|
||||
caption_plot <- function() {
|
||||
ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) +
|
||||
ggplot2::geom_point() +
|
||||
ggplot2::labs(title = "Title",
|
||||
caption = "Source: a reasonably long attribution line, 2021-22\nSecond line") +
|
||||
theme_civilytics()
|
||||
}
|
||||
|
||||
# The composed grob is a gtable, wrapped in a grobTree when the plot has a
|
||||
# background fill.
|
||||
composed_table <- function(composed) {
|
||||
if (inherits(composed, "gtable")) composed else composed$children[[2]]
|
||||
}
|
||||
|
||||
bottom_ink <- function(grob, width, height, rows = 2) {
|
||||
path <- tempfile(fileext = ".png")
|
||||
grDevices::png(path, width = width, height = height, units = "in", res = 100)
|
||||
grid::grid.newpage()
|
||||
grid::grid.draw(grob)
|
||||
grDevices::dev.off()
|
||||
img <- png::readPNG(path)
|
||||
n <- dim(img)[1]
|
||||
edge <- img[(n - rows + 1):n, , 1:3, drop = FALSE]
|
||||
sum(edge < 0.5)
|
||||
}
|
||||
|
||||
test_that("font_scale defaults to 1 everywhere", {
|
||||
expect_identical(formals(add_logo)$font_scale, 1)
|
||||
expect_identical(formals(add_logo_ga)$font_scale, 1)
|
||||
expect_identical(formals(civilytics_logo)$font_scale, 1)
|
||||
})
|
||||
|
||||
test_that("the logo band is reserved space that sums to the canvas", {
|
||||
p <- caption_plot()
|
||||
for (pos in c("bottom-right", "top-left")) {
|
||||
composed <- suppressWarnings(civilytics_logo(p, position = pos, 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("a caption is not clipped at the bottom edge at several canvas sizes", {
|
||||
skip_on_cran()
|
||||
p <- caption_plot()
|
||||
for (w in c(8, 11, 16, 24)) {
|
||||
h <- w * 0.64
|
||||
ink <- bottom_ink(suppressWarnings(civilytics_logo(p, position = "top-right")), w, h)
|
||||
expect_equal(ink, 0, info = paste("width", w))
|
||||
}
|
||||
})
|
||||
|
||||
test_that("patchwork compositions keep every panel", {
|
||||
skip_if_not_installed("patchwork")
|
||||
p1 <- ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) + ggplot2::geom_point()
|
||||
p2 <- ggplot2::ggplot(mtcars, ggplot2::aes(hp, wt)) + ggplot2::geom_point()
|
||||
pw <- p1 + p2 + patchwork::plot_annotation(title = "Both panels")
|
||||
composed <- suppressWarnings(civilytics_logo(pw))
|
||||
panel_grob <- composed_table(composed)$grobs[[1]]
|
||||
n_panels <- sum(grepl("^panel", panel_grob$layout$name))
|
||||
expect_gte(n_panels, 2)
|
||||
})
|
||||
|
||||
test_that("the logo keeps its aspect ratio at any canvas width (#20)", {
|
||||
skip_if_not_installed("ragg")
|
||||
ink_aspect <- function(width, type) {
|
||||
path <- tempfile(fileext = ".png")
|
||||
ragg::agg_png(path, width = width, height = 0.5, units = "in", res = 200,
|
||||
background = "transparent")
|
||||
grid::grid.draw(ggplot2::ggplotGrob(make_logo_grob(type = type)))
|
||||
grDevices::dev.off()
|
||||
ink <- which(png::readPNG(path)[, , 4] > 0.05, arr.ind = TRUE)
|
||||
diff(range(ink[, 2])) / diff(range(ink[, 1]))
|
||||
}
|
||||
for (type in c("wordmark", "mark")) {
|
||||
aspects <- vapply(c(6, 11, 24), ink_aspect, numeric(1), type = 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