From c104e73933f6eaf10cba5b280ed8d2bda1241c5e Mon Sep 17 00:00:00 2001 From: Valerie Wirtschafter <38087149+vwirtschafter@users.noreply.github.com> Date: Fri, 26 Jan 2024 13:23:45 -0500 Subject: [PATCH] Updates based on the new style guide Changes to the base fond and style --- README.Rmd | 6 +- fonts.R | 146 +++++++++++++++++++++++ palettes.R | 271 ++++++++++++++++++++++++++++++++++++++++++ theme.R | 342 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 762 insertions(+), 3 deletions(-) create mode 100644 fonts.R create mode 100644 palettes.R create mode 100644 theme.R diff --git a/README.Rmd b/README.Rmd index 98f0cba..03ffa35 100644 --- a/README.Rmd +++ b/README.Rmd @@ -45,8 +45,8 @@ Sys.setenv(TZ = 'UTC') ## Fonts -**Roboto** is Brooking's main font. -You will need to install it by visiting [Google fonts](https://fonts.google.com/specimen/Roboto?query=Roboto) and clicking "Download family". +**Inter** is Brooking's main font. +You will need to install it by visiting [Google fonts](https://fonts.google.com/specimen/Inter?query=Inter) and clicking "Download family". Once you've done this, unzip and open each of the .ttf files and click install. Finaly, run the code chunk below to ensure Roboto is imported and registered: @@ -56,7 +56,7 @@ library(ggbrookings) ``` ```{r, eval = FALSE} -import_roboto() +import_inter() ``` If you run into any problems while installing fonts on a Windows computer try the following solution from this [issue](https://github.com/wch/extrafont/issues/88): diff --git a/fonts.R b/fonts.R new file mode 100644 index 0000000..db91774 --- /dev/null +++ b/fonts.R @@ -0,0 +1,146 @@ +#' Import and register Inter font +#' +#' \code{import_inter()} tests to see if Inter is imported and registered. If +#' Inter, isn't imported and registered, then \code{import_inter()} imports and +#' registers Inter with +#' \href{https://github.com/wch/extrafont}{library(extrafont)}. +#' +#' Note: Inter must be installed on your computer for \code{import_inter()} to +#' work. Inter is Brooking's main font. To install, visit +#' \href{https://fonts.google.com/specimen/Inter}{Google fonts} and click +#' "Download family". Unzip and open each of the .ttf files and click install. +#' +#' +#' @md +#' @export +import_inter <- function() { + if (sum(grepl("[Ii]nter$", extrafont::fonts())) > 0) { + "Inter is already imported and registered." + } else { + + # get a list of all fonts on the machine + local_fonts <- systemfonts::system_fonts() + + # subset the list to just inter fonts + inter_fonts <- local_fonts[grepl(pattern = "[Ii]nter", x = local_fonts$family), ] + + # create a vector of directories where inter fonts are located + inter_directories <- unique(gsub("[Ii]nter.*\\.ttf", "", inter_fonts$path)) + + # import the inter fonts + extrafont::font_import(paths = inter_directories, pattern = "[Ii]nter") + + # register the fonts + extrafont::loadfonts() + } +} + + + + +#' @title Import THP fonts +#' @description Imports Photina, Helvetica Neue, and Myriad Pro +#' @details import_*() functions taken from hrbrthemes. +#' @seealso +#' \code{\link[extrafont]{font_import}} +#' @rdname import_thp_fonts +#' @export +#' @importFrom extrafont font_import + +import_thp_fonts <- function() { + + if (sum(grepl("[Mm]yriad Pro$", extrafont::fonts())) > 0) { + print("Myriad Pro is already imported and registered.") + } else{ + import_myriad_pro() + } + # Photina + if (sum(grepl("[Pp]hotina", extrafont::fonts())) > 0) { + print("Photina is already imported and registered.") + } else{ + import_photina() + } + + # Helvetica + if (sum(grepl("[Hh]elveticaNeueLT", extrafont::fonts())) > 0) { + print("Helvetica is already imported and registered.") + } else{ + import_helvetica() + } + +} + + + +import_photina <- function() { + photina_font_dir <- system.file("fonts", "Photina", package = "ggbrookings") + + suppressWarnings(suppressMessages(extrafont::font_import(paths = photina_font_dir, prompt = FALSE))) + + message( + sprintf( + "You will likely need to install these fonts on your system as well.\n\nYou can find them in [%s]", + photina_font_dir + ) + ) +} + +import_helvetica <- function(){ + helvetica_font_dir <- system.file("fonts", "Helvetica", package = "ggbrookings") + + suppressWarnings(suppressMessages(extrafont::font_import(paths = helvetica_font_dir, prompt = FALSE))) + + message( + sprintf( + "You will likely need to install these fonts on your system as well.\n\nYou can find them in [%s]", + helvetica_font_dir + ) + ) +} + +import_myriad_pro <- function(){ + myriad_pro_font_dir <- system.file("fonts", "Myriad-Pro", package = "ggbrookings") + + suppressWarnings(suppressMessages(extrafont::font_import(paths = myriad_pro_font_dir, prompt = FALSE))) + + message( + sprintf( + "You will likely need to install these fonts on your system as well.\n\nYou can find them in [%s]", + myriad_pro_font_dir + ) + ) +} + + + + +#' Register Hamilton Fonts +#' +#' @param dpi set dpi (default is 300) +#' +#' @export +register_thp_fonts <- function(dpi = 300){ + + path <- system.file(package = "ggbrookings", 'fonts') + + sysfonts::font_add(family = 'photina', + regular = glue::glue('{path}/Photina/Photina.ttf')) + sysfonts::font_add( + family = 'myriad-pro', + regular = glue::glue('{path}/Myriad-Pro/MYRIADPRO-REGULAR.OTF'), + bold = glue::glue('{path}/Myriad-Pro/MYRIADPRO-SEMIBOLD.OTF'), + italic = glue::glue('{path}/Myriad-Pro/MYRIADPRO-CONDIT.OTF'), + bolditalic = glue::glue('{path}/Myriad-Pro/MYRIADPRO-BOLDIT.OTF') + ) + + sysfonts::font_add( + family = 'helvetica', + regular = glue::glue('{path}/Helvetica/HelveticaNeueLTStd-Lt.OTF'), + bold = glue::glue('{path}/Helvetica/HelveticaNeueLTStd-Md.OTF'), + ) + + showtext::showtext_auto() + showtext::showtext_opts(dpi = dpi) +} + + diff --git a/palettes.R b/palettes.R new file mode 100644 index 0000000..d322c8a --- /dev/null +++ b/palettes.R @@ -0,0 +1,271 @@ +#' Brookings Institution palettes +#' +#' Vectors with hex-color codes that correspond to the color palettes outlined in the Brookings Style Guide. +#' x +#' +#' @source Brookings Style Guide +#' x +#' +#' +#' @family brookings palettes +#' @rdname brookings_palettes +#' @export +brookings_colors <- + c( + `brookings blue` = "#003A70", + # Brand blue + brand_blue_90 = "#022A4E", + brand_blue_80 = "#003A70", + brand_blue_70 = "#1A4E80", + brand_blue_60 = "#326295", + brand_blue_50 = "#517EAD", + brand_blue_40 = "#7098C3", + brand_blue_30 = "#8DADD0", + brand_blue_20 = "#A8BDD5", + brand_blue_10 = "#DDE5ED", + + # Vivid blue + vivid_blue_90 = "#023147", + vivid_blue_80 = "#004B6E", + vivid_blue_70 = "#0061A0", + vivid_blue_60 = "#1479BB", + vivid_blue_50 = "#1E8AD6", + vivid_blue_40 = "#3398EA", + vivid_blue_30 = "#5AADF6", + vivid_blue_20 = "#8BB8E8", + vivid_blue_10 = "#BFDFFC", + + # Teal + teal_90 = "#032B30", + teal_80 = "#09484F", + teal_70 = "#116470", + teal_60 = "#1C8090", + teal_50 = "#2A9AAD", + teal_40 = "#3EB2C6", + teal_30 = "#59C6DA", + teal_20 = "#7CD9EA", + teal_10 = "#A6E9F5", + + # Green + green_90 = "#1A3404", + green_80 = "#294D0A", + green_70 = "#33660F", + green_60 = "#45821B", + green_50 = "#5CA632", + green_40 = "#7DBF52", + green_30 = "#9CD674", + green_20 = "#BDED9D", + green_10 = "#DEF5CC", + + # Yellow + yellow_90 = "#594C09", + yellow_80 = "#877414", + yellow_70 = "#C7A70A", + yellow_60 = "#E0BB00", + yellow_50 = "#F2CD00", + yellow_40 = "#FFDD00", + yellow_30 = "#FFE926", + yellow_20 = "#FFF170", + yellow_10 = "#FFF9C2", + + # Orange + orange_90 = "#663205", + orange_80 = "#994B08", + orange_70 = "#B85B0A", + orange_60 = "#EF6A00", + orange_50 = "#FF851A", + orange_40 = "#FF9E1B", + orange_30 = "#FFB24D", + orange_20 = "#FEC87F", + orange_10 = "#FBD9A5", + + # Red + red_90 = "#660507", + red_80 = "#A00D11", + red_70 = "#CD1A1C", + red_60 = "#E22827", + red_50 = "#ED3A35", + red_40 = "#F75C57", + red_30 = "#F98B83", + red_20 = "#FCB0AA", + red_10 = "#FDD7D4", + + # Magenta + magenta_90 = "#510831", + magenta_80 = "#8D1655", + magenta_70 = "#A82168", + magenta_60 = "#BF317B", + magenta_50 = "#D2468E", + magenta_40 = "#E160A2", + magenta_30 = "#EC81B7", + magenta_20 = "#F5A8CF", + magenta_10 = "#FAD4E7", + + # Purple + purple_90 = "#3E2C72", + purple_80 = "#533C91", + purple_70 = "#6A50AD", + purple_60 = "#7C60BF", + purple_50 = "#8E72D0", + purple_40 = "#9C82D9", + purple_30 = "#B59DEA", + purple_20 = "#D0BEF5", + purple_10 = "#E9E0FC", + + # Grey + `PMS 427 C` = "#B1B3b3", + + # The Hamilton Project colors + THP_turquoise ='#007363', + THP_ltgreen ='#A5D867', + THP_dkblue ='#006983', + THP_dkgreen ='#427730', + THP_purple ='#6E2585', + THP_grey ='#FC7F92', + THP_ltblue ='#00ADD0', + THP_green ='#69BE28', + THP_orange ='#FF6E00', + THP_yellow ='#FFB612', + THP_ltpurple = '#C957BC' + ) + +#' Function to extract brookings colors as hex codes +#' +#' @param ... Character names of Brookings_colors +#' +brookings_cols <- function(...) { + cols <- c(...) + + if (is.null(cols)) { + return(brookings_colors) + } + + brookings_colors[cols] +} + +#' @rdname brookings_palettes +#' +brookings_palettes <- list( + # BRAND COLORS + # Use colors from the brand palette whenever possible. + brand1 = brookings_cols("brookings blue", "vivid_blue_20", "orange_40"), + brand2 = brookings_cols("brookings blue", "orange_40", "PMS 427 C"), + # Analogous + # Different shades of the same hue, or of similar hues can be used when the associated + # values are related + analogous1 = brookings_cols("brookings blue", "vivid_blue_20"), + analogous2 = brookings_cols("brookings blue", "teal_40"), + + # CONTRASTING + # Colors on the opposite ends of the spectrum. Use Brookings Blue with Secondary colors. + contrasting1 = brookings_cols("brookings blue", "orange_40"), + contrasting2 = brookings_cols("brookings blue", "yellow_50"), + + # SEMANTIC + # Where applicable, use colors that are associated with certain concepts. For e.g., option 1 shows male and female data, and option 2 show pros and cons or positive and negative data. + semantic1 = brookings_cols("teal_30", "red_40"), + semantic2 = brookings_cols("teal_60", "red_80", "yellow_60"), + semantic3 = brookings_cols("teal_30", "red_40", "yellow_40"), + pos_neg1 = brookings_cols("green_50", "red_70"), + pos_neg2 = brookings_cols("green_50", "yellow_50", "red_70"), + + + # POLITICAL + # Use red and blue + # of similar intensity to represent data related to political parties in the US. + # Do not use the Brand Blue color family to represent a political party. + political1 = brookings_cols("vivid_blue_60", "red_50"), + political2 = brookings_cols("vivid_blue_30", "red_30"), + political3 = brookings_cols("vivid_blue_60", "red_50", "yellow_60"), + political4 = brookings_cols("vivid_blue_30", "red_30", "yellow_30"), + + # CATEGORICAL + categorical = c("#2599adff", "#00649fff", "#fd9d1fff", "#f5cc05ff", "#de60a1ff", "#9e0d12ff"), + categorical_expanded = c("#2A9AAD", "#00649F", "#FF9E1B", "#533C91", "#E160A2", "#A00D11", "#5CA632"), + yor = c("#FF9E1B", "#FFF170"), + + # SEQUENTIAL + sequential1 = c( + "#00649fff", "#0f78baff", "#1c8ad6ff", "#2e97eaff", "#56adf6ff", + "#87c4feff", "#bcdefbff" + ), + sequential2 = c("#0d636fff", "#008080ff", "#009a80ff", "#2bb275ff", "#6dc960ff", "#b1dc44ff", "#fce829ff"), + + # Diverging + diverging = c("#0f78baff", "#739fceff", "#b1c5deff", "#efefefff", "#f6b5a9ff", "#f07867ff", "#e02928ff"), + + # MISCELLANEOUS + # A pleasing option using Brookings Blue and accent yellow + misc = brookings_cols("brookings blue", "teal_40", "yellow_50"), + + # THP Palettes + thp_categorical1 = brookings_cols("THP_dkgreen","THP_green","THP_purple"), + + thp_categorical2 = brookings_cols("THP_dkblue","THP_green", "THP_ltblue", "THP_yellow", "THP_purple", "THP_orange"), + + thp_continuous_purple = brookings_cols("THP_ltpurple", "THP_purple"), + + thp_gender=brookings_cols("THP_dkblue", "THP_purple", "THP_ltblue", "THP_ltpurple") +) + +#' Return function to interpolate a brookings color palette +#' +#' @param palette Character name of palette in brookings_palettes +#' @param reverse Boolean indicating whether the palette should be reversed +#' @param ... Additional arguments to pass to colorRampPalette() +#' +brookings_pal <- function(palette = "brand1", reverse = FALSE, ...) { + pal <- brookings_palettes[[palette]] + + if (reverse) pal <- rev(pal) + + colorRampPalette(pal, ...) +} + +#' Color scales in the Brookings style + +#' @param palette Character name of brookings_palettes +#' @param discrete Boolean indicating whether color aesthetic is discrete or not +#' @param reverse Boolean indicating whether the palette should be reversed +#' @param ... Additional arguments passed to discrete_scale() or scale_color_gradientn(), used respectively when discrete is TRUE or FALSE. +#' +#' @section Palettes: +#' \describe{ +#' \item{Analogous}{Different shades of the same hue, or of similar hues can be used when the associated values are related.} +#' \item{Contrasting}{Colors on the opposite ends of the spectrum. Use Brookings Blue with Secondary colors.} +#' \item{Semantic}{Where applicable, use colors that are associated with certain concepts. For e.g., semantic1, semantic2, and semantic3 could show subsets of gender data (female, male and other).} +#' \item{Positive & Negative}{Shows pros, cons and neutral, or positive, negative and neutral data.} +#' \item{Political}{Use red and blue of similar intensity to represent data related to political parties in the US. Yellow in political3 and political4 represents ‘Independent’ category} +#' \item{Categorical}{Use categorical palettes to distinguish discrete categories of data that do not have an inherent ordering.} +#' \item{Sequential}{Sequential palettes can be used to show an inherent order or variations in numeric values.} +#' \item{Diverging}{Diverging palettes are useful when dealing with negative and positive values or a range of values that have two extremes with a baseline central value, like zero. The Brookings diverging palette should uses two distinct hues of similar brightness and saturation with a neutral color in the middle. Using a discrete set of colors with evenly distributed gradation can improve clarity of values relative to a continuous palette.} +#' \item{Misc}{A pleasing option using Brookings Blue and accent yellow.} +#' } +#' +#' @name scales_brookings +NULL + +#' @rdname scales_brookings +#' @export +scale_color_brookings <- function(palette = "brand1", discrete = TRUE, reverse = FALSE, ...) { + pal <- brookings_pal(palette = palette, reverse = reverse) + + if (discrete) { + ggplot2::discrete_scale("colour", paste0("brookings_", palette), palette = pal, ...) + } else { + ggplot2::scale_color_gradientn(colours = pal(256), ...) + } +} + +#' @rdname scales_brookings +#' @export +scale_fill_brookings <- function(palette = "brand1", discrete = TRUE, reverse = FALSE, ...) { + + pal <- brookings_pal(palette = palette, reverse = reverse) + + if (discrete) { + ggplot2::discrete_scale("fill", paste0("brookings_", palette), palette = pal, ...) + } else { + scale_fill_gradientn(colours = pal(256), ...) + } +} diff --git a/theme.R b/theme.R new file mode 100644 index 0000000..9423dec --- /dev/null +++ b/theme.R @@ -0,0 +1,342 @@ +#' Brookings style ggplot2 theme +#' +#' A theme following the Brookings style guide. +#' @param base_size base font size, given in pts. +#' @param base_family base font family. Default is "Inter". +#' @param base_line_size line size in mm. +#' @param base_rect_size border size in mm. +#' @param web If TRUE sets the plot background color to the Brookings website background color +#' @param ticks include axis ticks. Default is FALSE. +#' @importFrom ggtext element_textbox_simple +#' @importFrom ggplot2 margin rel scale_color_gradientn scale_fill_gradientn theme theme_light unit +#' @importFrom ggplot2 `%+replace%` +#' @export +#' + +theme_brookings <- function(base_size = 14, + base_family = "Inter", + base_line_size = base_size / 24, + base_rect_size = base_size / 24, + web = FALSE, + ticks = TRUE) { + # The half-line (base-fontsize / 2) sets up the basic vertical + # rhythm of the theme. Most margins will be set to this value. + # However, when we work with relative sizes, we may want to multiply + # `half_line` with the appropriate relative size. This applies in + # particular for axis tick sizes. And also, for axis ticks and + # axis titles, `half_size` is too large a distance, and we use `half_size/2` + # instead. + + half_line <- base_size / 2 + # Throughout the theme, we use three font sizes, `base_size` (`rel(1)`) + # for normal, `rel(0.8)` for small, and `rel(1.2)` for large. + + t <- theme_light() %+replace% + theme( + # Elements in this first block aren't used directly, but are inherited + # by others + line = + ggplot2::element_line( + colour = "black", + size = base_line_size, + linetype = 1, + lineend = "butt" + ), + rect = + ggplot2::element_rect( + fill = NULL, + colour = "black", + size = base_rect_size, + linetype = 1 + ), + text = + ggplot2::element_text( + family = base_family, + face = "plain", + colour = "black", + size = base_size, + lineheight = 1, + hjust = 0.5, + vjust = 0.5, + angle = 0, + margin = margin(), + debug = FALSE + ), + axis.line = ggplot2::element_line(), + axis.line.x = ggplot2::element_line( + colour = "#000000", + size = NULL, + linetype = NULL, + lineend = NULL + ), + axis.line.y = ggplot2::element_blank(), + axis.text = ggplot2::element_text(size = rel(0.8), colour = "#000000"), + axis.text.x = ggplot2::element_text(margin = margin(t = 0.8 * half_line / 2), vjust = 1), + axis.text.x.top = ggplot2::element_text(margin = margin(b = 0.8 * half_line / 2), vjust = 0), + axis.text.y = ggplot2::element_text(margin = margin(r = 0.8 * half_line / 2), hjust = 1), + axis.text.y.right = ggplot2::element_text(margin = margin(l = 0.8 * half_line / 2), hjust = 0), + axis.title.x = ggplot2::element_text( + margin = margin(t = 1.25 * half_line), + size = rel(0.8), + vjust = 1 + ), + axis.title.x.top = ggplot2::element_text( + margin = margin(b = half_line / 2), + vjust = 0 + ), + axis.title.y = ggplot2::element_text( + angle = 90, + margin = margin(r = half_line * 1.25), + vjust = 1, + size = rel(0.8) + ), + axis.title.y.right = ggplot2::element_text( + angle = -90, + margin = margin(l = half_line / 2), + vjust = 0 + ), + legend.background = ggplot2::element_rect(colour = NA), + # legend.spacing = unit( half_line, "pt"), + # legend.spacing.x = NULL, + # legend.spacing.y = NULL, + # legend.margin = margin(0,0,0,0), + legend.key = ggplot2::element_blank(), + legend.key.size = unit(rel(0.8), "lines"), + legend.key.height = NULL, + legend.key.width = NULL, + legend.text = ggplot2::element_text(size = rel(1)), + legend.text.align = 0, + legend.title = ggplot2::element_blank(), + legend.title.align = NULL, + legend.position = "top", + legend.direction = NULL, + legend.justification = "center", + # legend.box = NULL, + # legend.box.margin = margin(0, 0, 0, 0, "cm"), + # legend.box.background = ggplot2::element_blank(), + # legend.box.spacing = unit(2 * half_line, "pt"), + + panel.background = ggplot2::element_blank(), + panel.border = ggplot2::element_blank(), + panel.grid = ggplot2::element_line( + colour = "#CCCCCC", + # size = 1, + linetype = "dotted" + ), + panel.grid.major.x = ggplot2::element_blank(), + panel.grid.minor = ggplot2::element_blank(), + # panel.spacing = unit(half_line, "pt"), + # panel.spacing.x = unit(1.5, "lines"), + # panel.spacing.y = unit(2, "lines"), + panel.ontop = FALSE, + strip.background = ggplot2::element_rect(fill = "#003A70", colour = NA), + strip.text = ggplot2::element_text( + colour = "#FAFAFA", + size = rel(0.8), + margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line) + ), + # strip.text.x = NULL, + # strip.text.y = ggplot2::element_text(angle = -90), + # strip.text.y.left = ggplot2::element_text(angle = 90), + # # strip.placement = "outside", + # strip.placement.x = NULL, + # strip.placement.y = NULL, + # strip.switch.pad.grid = unit(half_line / 2, "pt"), + # strip.switch.pad.wrap = unit(half_line / 2, "pt"), + + plot.title = ggtext::element_textbox_simple( + # font size "large" + size = rel(1.2), + color = "#191919", + face = "bold", + hjust = 0, + vjust = 1, + margin = margin(b = half_line / 2, t = base_size) + ), + plot.title.position = "plot", + plot.subtitle = ggtext::element_textbox_simple( + # font size "regular" + hjust = 0, + size = rel(1), + color = "#191919", + vjust = 1, + margin = margin(b = base_size) + ), + plot.caption = ggtext::element_textbox_simple( + # font size "small" + size = rel(0.8), + vjust = 1, + color = "#666666", + hjust = 0, + margin = margin(t = half_line) + ), + plot.caption.position = "plot", + plot.tag = ggplot2::element_text( + size = rel(0.8), + hjust = 0, + vjust = 1, + color = "#666666" + ), + plot.tag.position = c(0, 1), + plot.margin = margin(half_line, half_line, half_line * 2, half_line), + # plot.margin = unit(c(0.5, 0.5, 2.5, 0.5), "lines"), + + complete = FALSE + ) + + + if (ticks == FALSE) { + t <- t + theme( + axis.ticks = ggplot2::element_blank(), + axis.ticks.x = ggplot2::element_blank(), + axis.ticks.y = ggplot2::element_blank() + ) + } else { + t <- + t + theme( + axis.ticks = ggplot2::element_line(colour = "#000000"), + axis.ticks.length = unit(half_line / 2, "pt"), + axis.ticks.length.x = NULL, + axis.ticks.length.x.top = NULL, + axis.ticks.length.x.bottom = NULL, + axis.ticks.y = ggplot2::element_blank() + ) + } + + if (web == FALSE) { + t <- + t + theme(plot.background = ggplot2::element_rect(fill = "#FFFFFF")) + } else { + t <- + t + theme(plot.background = ggplot2::element_rect(fill = "#FAFAFA")) + } +} + + +#' Title +#' +#' @param base_size base font size, given in pts. +#' @param base_family base font family. Default is "Inter". +#' @param base_line_size line size in mm. +#' @param base_rect_size border size in mm. +#' @param ... additional arguments passed to theme_brookings() +#' +#' @importFrom ggplot2 `%+replace%` +#' @export +#' +theme_thp <- function(base_size = 12, + base_family = "Calibri", + base_line_size = base_size / 24, + base_rect_size = base_size / 24, + ...) { + + if (!requireNamespace("showtext", quietly = TRUE)) { + stop("Package \"showtext\" needed for this function to work. Please install it.", + call. = FALSE) + } + + if (!requireNamespace("sysfonts", quietly = TRUE)) { + stop("Package \"sysfonts\" needed for this function to work. Please install it.", + call. = FALSE) + } + + + # Half line + half_line <- base_size / 2 + # Load fonts + # if(sum(grepl('photina|myriad-pro|helvetica_medium|helvetica_light', sysfonts::font_families())) != 4){ + # path <- system.file(package = "ggbrookings", 'fonts') + # sysfonts::font_add(family = 'photina', + # regular = glue::glue('{path}/Photina/Photina.ttf')) + # sysfonts::font_add( + # family = 'myriad-pro', + # regular = glue::glue('{path}/Myriad-Pro/MYRIADPRO-REGULAR.ttf'), + # bold = glue::glue('{path}/Myriad-Pro/MYRIADPRO-SEMIBOLD.ttf'), + # italic = glue::glue('{path}/Myriad-Pro/MYRIADPRO-CONDIT.ttf'), + # bolditalic = glue::glue('{path}/Myriad-Pro/MYRIADPRO-BOLDIT.ttf') + # ) + # + # sysfonts::font_add( + # family = 'helvetica_medium', + # regular = glue::glue('{path}/Helvetica/HelveticaNeueLTStd-Md.ttf'), + # ) + # sysfonts::font_add(family = 'helvetica_light', + # regular = glue::glue('{path}/Helvetica/HelveticaNeueLTStd-Lt.ttf'), + # bold = glue::glue('{path}/Helvetica/HelveticaNeue-Bold.ttf')) + # } + # + # + # # TODO write a warning message about showtext_auto() and rendering + # showtext::showtext_auto() + # showtext::showtext_opts(dpi = 300) + # if(.Platform$OS.type == 'windows'){ + # x11() + # } + + theme_brookings(...) %+replace% + theme( + text = ggplot2::element_text( + family = base_family, + face = "plain", + colour = "black", + size = base_size, + lineheight = 1, + hjust = 0.5, + vjust = 0.5, + angle = 0, + margin = margin(), + debug = FALSE + ), + plot.title = ggtext::element_textbox_simple( + # font size "large" + family = 'Calibri', + size = rel(1.33), + color = "#007363", + hjust = 0, + vjust = 1, + margin = margin(b = half_line / 2, t = base_size)), + strip.background = ggplot2::element_rect(fill = "#007363", colour = NA), + strip.text = ggplot2::element_text(colour = "#FAFAFA", + size = rel(1), + margin = margin(0.8 * half_line, 0.8 * half_line, 0.8 * half_line, 0.8 * half_line)), + plot.subtitle = ggtext::element_textbox_simple( + # font size "regular" + family = 'Calibri', + hjust = 0, + size = rel(1), + vjust = 1, + margin = margin(b = base_size) + ), + plot.tag = ggplot2::element_text( + family = 'Calibri', + size = rel(2/3), + hjust = 0, + vjust = 1, + color = "#007363" + ), + plot.caption = ggtext::element_textbox_simple( + # font size "small" + size = rel(2/3), + family = 'Calibri', + vjust = 1, + color = "#666666", + hjust = 0, + margin = margin(t = half_line) + ), + axis.text = ggplot2::element_text(size = rel(0.833), colour = "#000000"), + axis.title.x = ggplot2::element_text( + margin = margin(t = 1.25 * half_line), + size = rel(0.833), + vjust = 1 + ), + axis.title.y = ggplot2::element_text( + angle = 90, + margin = margin(r = half_line * 1.25), + vjust = 1, + size = rel(0.833) + ), + legend.text = ggplot2::element_text(size = rel(0.833)), + + ) +} +