feat: add map theme variants and fix dark theme text readability
R-CMD-check / R CMD check (push) Successful in 3m48s

Add theme_civilytics_map(), theme_civilytics_dark_map(), and
theme_civilytics_slide_map() for choropleths — suppress axes, ticks,
gridlines, and axis labels while preserving titles, captions, and legends.

Fix subtitle, caption, and axis text colors in theme_civilytics_dark()
which were inheriting dark ink_2/ink_3 values meant for light backgrounds,
making them hard to read on navy. Now uses navy_200/navy_300 instead.
This commit is contained in:
2026-05-19 16:07:42 -06:00
parent 328d9d2fd6
commit 98e0949d27
7 changed files with 552 additions and 1 deletions
+204 -1
View File
@@ -355,7 +355,27 @@ theme_civilytics_dark <- function(
strip_color = strip_color,
grid = grid,
paper_bg = paper_bg
)
) +
# The base theme hardcodes ink_2/ink_3 for subtitle/caption, which are
# dark colors meant for light backgrounds. Override with lighter values
# so text remains readable on the navy background.
ggplot2::theme(
plot.subtitle = ggplot2::element_text(
color = unname(civilytics_colors["navy_200"])
),
plot.caption = ggplot2::element_text(
color = unname(civilytics_colors["navy_300"])
),
axis.text = ggplot2::element_text(
color = unname(civilytics_colors["navy_200"])
),
axis.title.x = ggplot2::element_text(
color = unname(civilytics_colors["navy_300"])
),
axis.title.y = ggplot2::element_text(
color = unname(civilytics_colors["navy_300"])
)
)
}
@@ -417,3 +437,186 @@ theme_civilytics_slide <- function(
plot.margin = ggplot2::margin(24, 24, 24, 24)
)
}
# -- Map themes ----------------------------------------------------------------
#' Shared map-theme overrides
#'
#' Strips away axes, ticks, gridlines, and axis titles/labels — the elements
#' that are meaningless on a choropleth or spatial plot.
#'
#' @return A partial ggplot2 [ggplot2::theme()] object.
#' @keywords internal
.map_theme_extras <- function() {
ggplot2::theme(
axis.line = ggplot2::element_blank(),
axis.line.x = ggplot2::element_blank(),
axis.line.y = ggplot2::element_blank(),
axis.text = ggplot2::element_blank(),
axis.text.x = ggplot2::element_blank(),
axis.text.y = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
axis.ticks.length = ggplot2::unit(0, "pt"),
axis.title.x = ggplot2::element_blank(),
axis.title.y = ggplot2::element_blank(),
panel.grid.major.x = ggplot2::element_blank(),
panel.grid.major.y = ggplot2::element_blank(),
panel.grid.minor = ggplot2::element_blank()
)
}
#' Map-friendly Civilytics ggplot2 theme
#'
#' Variant of [theme_civilytics()] for choropleths and spatial plots.
#' Suppresses axes, axis labels, ticks, and gridlines while keeping the
#' Civilytics brand typography, colors, and plot-level elements (title,
#' subtitle, caption, legend).
#'
#' @inheritParams theme_civilytics
#'
#' @return A complete ggplot2 [ggplot2::theme()] object.
#' @export
#'
#' @examples
#' \dontrun{
#' library(ggplot2)
#' # With sf data:
#' ggplot(map_data) +
#' geom_sf(aes(fill = value)) +
#' scale_fill_civilytics_c("seq_navy") +
#' theme_civilytics_map()
#' }
theme_civilytics_map <- function(
font_size = 14,
font_family = CV_FONT_SANS,
title_family = CV_FONT_DISPLAY,
line_size = 0.5,
rel_small = 12 / 14,
rel_tiny = 11 / 14,
rel_large = 20 / 14,
ink = unname(civilytics_colors["ink"]),
paper = unname(civilytics_colors["paper"]),
accent = unname(civilytics_colors["ember_600"]),
strip_color = unname(civilytics_colors["paper_2"]),
paper_bg = TRUE) {
theme_civilytics(
font_size = font_size,
font_family = font_family,
title_family = title_family,
line_size = line_size,
rel_small = rel_small,
rel_tiny = rel_tiny,
rel_large = rel_large,
ink = ink,
paper = paper,
accent = accent,
strip_color = strip_color,
grid = "none",
paper_bg = paper_bg
) + .map_theme_extras()
}
#' Dark map-friendly Civilytics ggplot2 theme
#'
#' Variant of [theme_civilytics_dark()] for choropleths and spatial plots.
#' Suppresses axes, axis labels, ticks, and gridlines on a dark navy
#' background. Pair with `make_logo_grob(variant = "dark")`.
#'
#' @inheritParams theme_civilytics_dark
#'
#' @return A complete ggplot2 [ggplot2::theme()] object.
#' @export
#'
#' @examples
#' \dontrun{
#' library(ggplot2)
#' ggplot(map_data) +
#' geom_sf(aes(fill = value)) +
#' scale_fill_civilytics_c("seq_ember") +
#' theme_civilytics_dark_map()
#' }
theme_civilytics_dark_map <- function(
font_size = 14,
font_family = CV_FONT_SANS,
title_family = CV_FONT_DISPLAY,
line_size = 0.5,
rel_small = 12 / 14,
rel_tiny = 11 / 14,
rel_large = 20 / 14,
ink = unname(civilytics_colors["paper"]),
paper = unname(civilytics_colors["navy_700"]),
accent = unname(civilytics_colors["ember_400"]),
strip_color = unname(civilytics_colors["navy_600"]),
paper_bg = TRUE) {
theme_civilytics_dark(
font_size = font_size,
font_family = font_family,
title_family = title_family,
line_size = line_size,
rel_small = rel_small,
rel_tiny = rel_tiny,
rel_large = rel_large,
ink = ink,
paper = paper,
accent = accent,
strip_color = strip_color,
grid = "none",
paper_bg = paper_bg
) + .map_theme_extras()
}
#' Slide-friendly map Civilytics ggplot2 theme
#'
#' Variant of [theme_civilytics_slide()] for choropleths and spatial plots
#' on slides. Combines the larger base font and transparent background of
#' the slide theme with suppressed axes, ticks, and gridlines.
#'
#' @inheritParams theme_civilytics_slide
#'
#' @return A complete ggplot2 [ggplot2::theme()] object.
#' @export
#'
#' @examples
#' \dontrun{
#' library(ggplot2)
#' ggplot(map_data) +
#' geom_sf(aes(fill = value)) +
#' scale_fill_civilytics_c("seq_navy") +
#' theme_civilytics_slide_map()
#' }
theme_civilytics_slide_map <- function(
font_size = 18,
font_family = CV_FONT_SANS,
title_family = CV_FONT_DISPLAY,
line_size = 0.5,
rel_small = 12 / 14,
rel_tiny = 11 / 14,
rel_large = 20 / 14,
ink = unname(civilytics_colors["ink"]),
paper = unname(civilytics_colors["paper"]),
accent = unname(civilytics_colors["ember_600"]),
strip_color = unname(civilytics_colors["paper_2"]),
paper_bg = FALSE) {
theme_civilytics_slide(
font_size = font_size,
font_family = font_family,
title_family = title_family,
line_size = line_size,
rel_small = rel_small,
rel_tiny = rel_tiny,
rel_large = rel_large,
ink = ink,
paper = paper,
accent = accent,
strip_color = strip_color,
grid = "none",
paper_bg = paper_bg
) + .map_theme_extras()
}