feat: bump title sizes, add font_scale to logo functions, ship SVGs
R-CMD-check / R CMD check (push) Successful in 3m36s
R-CMD-check / R CMD check (push) Successful in 3m36s
- Change rel_large default from 16/14 to 20/14 (~1.43x) to match the Civilytics editorial design system. Subtitle now renders at 1.0x base instead of 0.86x. - Switch axis.text to rel() sizing so all text elements cascade from the root font_size, enabling uniform scaling. - Add font_scale parameter (default 1.1) to civilytics_logo(), add_logo(), and add_logo_ga() — inflates text by ~10% before arrangeGrob composition to compensate for viewport shrinkage. - Ship SVG logo variants (mark, wordmark, pulse) in inst/img/ for Quarto/HTML templates. - Add "Maintaining brand assets" section to README documenting inst/img/ file inventory and update workflow. - Document full font size hierarchy in theme_civilytics() roxygen.
This commit is contained in:
@@ -46,17 +46,27 @@ get_png <- function(filename) {
|
||||
#' @param plot a ggplot2 grob
|
||||
#' @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.
|
||||
#'
|
||||
#' @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) {
|
||||
add_logo <- function(plot, logo, margin_param = NULL, font_scale = 1.1) {
|
||||
# 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 + ggplot2::theme(
|
||||
text = ggplot2::element_text(size = base_size * font_scale)
|
||||
)
|
||||
}
|
||||
|
||||
if(has_caption(plot)) {
|
||||
# convert the caption size to a negative number and on the "pt" scale
|
||||
# p1$theme$plot.caption$size * 1.1
|
||||
# Count the number of lines, which is this + 1
|
||||
# For each line, we can add a certain negative space to align the logo
|
||||
cap_lines <- measure_caption(plot)
|
||||
if (!is.null(margin_param)) {
|
||||
plot <- plot + theme(plot.margin = unit(c(7, 7, margin_param, 7), "pt"))
|
||||
@@ -107,13 +117,15 @@ has_caption <- function(gg) {
|
||||
any(names(gg$labels) == "caption")
|
||||
}
|
||||
|
||||
#' Add a logo to a ggplot2 object
|
||||
#' Add a logo to multiple ggplot2 objects
|
||||
#'
|
||||
#' @param plot_list a list containing ggplot2 objects
|
||||
#' @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 font_scale Numeric. Multiplicative scaling factor applied to text
|
||||
#' sizes before composing. Default `1.1`. See [add_logo()] for details.
|
||||
#'
|
||||
#' @return a grid object
|
||||
#' @note The resulting object needs to be drawn to the screen using grid.draw()
|
||||
@@ -131,7 +143,18 @@ has_caption <- function(gg) {
|
||||
#' grid.draw(plot_and_logo)
|
||||
#' dev.off()
|
||||
#' }
|
||||
add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL, margin_param = NULL) {
|
||||
add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL,
|
||||
margin_param = NULL, font_scale = 1.1) {
|
||||
# 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
|
||||
p + ggplot2::theme(
|
||||
text = ggplot2::element_text(size = base_size * font_scale)
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
# Change position of logo depending on if plot has a caption
|
||||
if (!is.null(margin_param)) {
|
||||
margin <- theme(plot.margin = unit(c(7, 7, margin_param, 7), "pt"))
|
||||
@@ -224,6 +247,9 @@ make_logo_grob <- function(type = c("wordmark", "mark"),
|
||||
#' [make_logo_grob()].
|
||||
#' @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.
|
||||
#'
|
||||
#' @return A grob (from [gridExtra::arrangeGrob()]) ready to draw with
|
||||
#' [grid::grid.draw()].
|
||||
@@ -246,12 +272,20 @@ make_logo_grob <- function(type = c("wordmark", "mark"),
|
||||
#' theme_civilytics_dark()) |>
|
||||
#' civilytics_logo(variant = "dark", type = "mark") |>
|
||||
#' grid.draw()
|
||||
#'
|
||||
#' # Disable font inflation
|
||||
#' (ggplot(mpg, aes(displ, hwy)) +
|
||||
#' geom_point() +
|
||||
#' theme_civilytics()) |>
|
||||
#' civilytics_logo(font_scale = 1) |>
|
||||
#' grid.draw()
|
||||
#' }
|
||||
civilytics_logo <- function(plot,
|
||||
type = c("wordmark", "mark"),
|
||||
variant = c("light", "dark"),
|
||||
margin_param = NULL) {
|
||||
margin_param = NULL,
|
||||
font_scale = 1.1) {
|
||||
logo <- make_logo_grob(type = type, variant = variant)
|
||||
add_logo(plot, logo, margin_param = margin_param)
|
||||
add_logo(plot, logo, margin_param = margin_param, font_scale = font_scale)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user