feat: resolve #7 theme_civilytics(): default to a transparent background, opt-in for Civilytics cream

This commit is contained in:
2026-06-21 03:13:56 +00:00
parent 41162597cf
commit e256b73053
3 changed files with 45 additions and 17 deletions
+19 -8
View File
@@ -36,10 +36,9 @@
#' to [civilytics_colors]`["paper_2"]` (`#F2EDE4`). #' to [civilytics_colors]`["paper_2"]` (`#F2EDE4`).
#' @param grid Character. Which major gridlines to draw: `"y"` (default, #' @param grid Character. Which major gridlines to draw: `"y"` (default,
#' horizontal only), `"x"` (vertical only), `"both"`, or `"none"`. #' horizontal only), `"x"` (vertical only), `"both"`, or `"none"`.
#' @param paper_bg Logical. If `TRUE` (default), fill the plot and panel #' @param paper_bg Logical. If `FALSE` (default), the plot and panel
#' backgrounds with the warm `paper` color. Set to `FALSE` for a #' backgrounds are transparent (`fill = NA`). Set to `TRUE` to fill them
#' transparent background (useful for slides or overlay on colored #' with the warm `paper` color for a branded cream canvas.
#' surfaces).
#' #'
#' @section Font size hierarchy: #' @section Font size hierarchy:
#' All text sizes are derived from `font_size` using relative scale factors. #' All text sizes are derived from `font_size` using relative scale factors.
@@ -64,11 +63,23 @@
#' @return A complete ggplot2 [ggplot2::theme()] object. #' @return A complete ggplot2 [ggplot2::theme()] object.
#' @export #' @export
#' #'
#' @section Transparent device background:
#' Setting `paper_bg = FALSE` makes the theme's `plot.background` and
#' `panel.background` transparent (`fill = NA`), but the graphics device
#' itself may still paint a solid background. To produce fully transparent
#' PNGs, also pass a transparent device background:
#'
#' ```r
#' ragg::agg_png("plot.png", background = "transparent")
#' print(p)
#' dev.off()
#' ```
#'
#' @examples #' @examples
#' \dontrun{ #' \dontrun{
#' library(ggplot2) #' library(ggplot2)
#' #'
#' # Default editorial theme #' # Default: transparent background
#' ggplot(mpg, aes(displ, hwy)) + #' ggplot(mpg, aes(displ, hwy)) +
#' geom_point() + #' geom_point() +
#' theme_civilytics() #' theme_civilytics()
@@ -79,10 +90,10 @@
#' scale_color_civilytics() + #' scale_color_civilytics() +
#' theme_civilytics(grid = "both") #' theme_civilytics(grid = "both")
#' #'
#' # Transparent background for embedding #' # Branded cream canvas (opt-in)
#' ggplot(mpg, aes(displ, hwy)) + #' ggplot(mpg, aes(displ, hwy)) +
#' geom_point() + #' geom_point() +
#' theme_civilytics(paper_bg = FALSE) #' theme_civilytics(paper_bg = TRUE)
#' #'
#' # Larger text for poster or display #' # Larger text for poster or display
#' ggplot(mpg, aes(displ, hwy)) + #' ggplot(mpg, aes(displ, hwy)) +
@@ -102,7 +113,7 @@ theme_civilytics <- function(
accent = unname(civilytics_colors["ember_600"]), accent = unname(civilytics_colors["ember_600"]),
strip_color = unname(civilytics_colors["paper_2"]), strip_color = unname(civilytics_colors["paper_2"]),
grid = c("y", "x", "both", "none"), grid = c("y", "x", "both", "none"),
paper_bg = TRUE) { paper_bg = FALSE) {
grid <- match.arg(grid) grid <- match.arg(grid)
half_line <- font_size / 2 half_line <- font_size / 2
+20 -8
View File
@@ -17,7 +17,7 @@ theme_civilytics(
accent = unname(civilytics_colors["ember_600"]), accent = unname(civilytics_colors["ember_600"]),
strip_color = unname(civilytics_colors["paper_2"]), strip_color = unname(civilytics_colors["paper_2"]),
grid = c("y", "x", "both", "none"), grid = c("y", "x", "both", "none"),
paper_bg = TRUE paper_bg = FALSE
) )
} }
\arguments{ \arguments{
@@ -56,10 +56,9 @@ to [civilytics_colors]`["paper_2"]` (`#F2EDE4`).}
\item{grid}{Character. Which major gridlines to draw: `"y"` (default, \item{grid}{Character. Which major gridlines to draw: `"y"` (default,
horizontal only), `"x"` (vertical only), `"both"`, or `"none"`.} horizontal only), `"x"` (vertical only), `"both"`, or `"none"`.}
\item{paper_bg}{Logical. If `TRUE` (default), fill the plot and panel \item{paper_bg}{Logical. If `FALSE` (default), the plot and panel
backgrounds with the warm `paper` color. Set to `FALSE` for a backgrounds are transparent (`fill = NA`). Set to `TRUE` to fill them
transparent background (useful for slides or overlay on colored with the warm `paper` color for a branded cream canvas.}
surfaces).}
} }
\value{ \value{
A complete ggplot2 [ggplot2::theme()] object. A complete ggplot2 [ggplot2::theme()] object.
@@ -101,11 +100,24 @@ logo below the plot, pass `font_scale` to compensate for viewport
shrinkage. shrinkage.
} }
\section{Transparent device background}{
Setting `paper_bg = FALSE` makes the theme's `plot.background` and
`panel.background` transparent (`fill = NA`), but the graphics device
itself may still paint a solid background. To produce fully transparent
PNGs, also pass a transparent device background:
\if{html}{\out{<div class="sourceCode r">}}
\preformatted{ragg::agg_png("plot.png", background = "transparent")
print(p)
dev.off()}
\if{html}{\out{</div>}}
}
\examples{ \examples{
\dontrun{ \dontrun{
library(ggplot2) library(ggplot2)
# Default editorial theme # Default: transparent background
ggplot(mpg, aes(displ, hwy)) + ggplot(mpg, aes(displ, hwy)) +
geom_point() + geom_point() +
theme_civilytics() theme_civilytics()
@@ -116,10 +128,10 @@ ggplot(mpg, aes(displ, hwy, colour = class)) +
scale_color_civilytics() + scale_color_civilytics() +
theme_civilytics(grid = "both") theme_civilytics(grid = "both")
# Transparent background for embedding # Branded cream canvas (opt-in)
ggplot(mpg, aes(displ, hwy)) + ggplot(mpg, aes(displ, hwy)) +
geom_point() + geom_point() +
theme_civilytics(paper_bg = FALSE) theme_civilytics(paper_bg = TRUE)
# Larger text for poster or display # Larger text for poster or display
ggplot(mpg, aes(displ, hwy)) + ggplot(mpg, aes(displ, hwy)) +
+6 -1
View File
@@ -133,8 +133,13 @@ test_that("theme_civilytics uses brand ink color for text", {
expect_equal(th$text$colour, unname(civilytics_colors["ink"])) expect_equal(th$text$colour, unname(civilytics_colors["ink"]))
}) })
test_that("theme_civilytics uses brand paper color for plot background", { test_that("theme_civilytics defaults to transparent plot background", {
th <- theme_civilytics() th <- theme_civilytics()
expect_true(is.na(th$plot.background$fill))
})
test_that("theme_civilytics paper_bg=TRUE opt-in gives cream background", {
th <- theme_civilytics(paper_bg = TRUE)
expect_equal(th$plot.background$fill, unname(civilytics_colors["paper"])) expect_equal(th$plot.background$fill, unname(civilytics_colors["paper"]))
}) })