|
| 1 | +#' Create a ggplot2 theme using background and foreground colors |
| 2 | +#' |
| 3 | +#' Create a ggplot2 theme that uses the specified background and |
| 4 | +#' foreground colors. |
| 5 | +#' |
| 6 | +#' @param bgcolor The background color |
| 7 | +#' @param fgcolor The foreground color |
| 8 | +#' |
| 9 | +#' @export |
| 10 | +ggplot_theme_colors <- function(bgcolor, fgcolor) { |
| 11 | + if(!requireNamespace("ggplot2", quietly = TRUE)) { |
| 12 | + return(NULL) |
| 13 | + } |
| 14 | + ggplot2::`%+%`(ggplot2::theme_minimal(base_size = 11), |
| 15 | + ggplot2::theme( |
| 16 | + panel.border = ggplot2::element_blank(), |
| 17 | + panel.grid.major.y = ggplot2::element_blank(), |
| 18 | + panel.grid.minor.y = ggplot2::element_blank(), |
| 19 | + panel.grid.major.x = ggplot2::element_blank(), |
| 20 | + panel.grid.minor.x = ggplot2::element_blank(), |
| 21 | + text = ggplot2::element_text(colour = fgcolor), |
| 22 | + axis.text = ggplot2::element_text(colour = fgcolor), |
| 23 | + rect = ggplot2::element_rect(colour = bgcolor, fill = bgcolor), |
| 24 | + plot.background = ggplot2::element_rect(fill = bgcolor, colour = NA), |
| 25 | + axis.line = ggplot2::element_line(colour = fgcolor), |
| 26 | + axis.ticks = ggplot2::element_line(colour = fgcolor) |
| 27 | + )) |
| 28 | +} |
| 29 | + |
| 30 | +#' Create a ggplot2 theme from a brand.yml file |
| 31 | +#' |
| 32 | +#' Create a ggplot2 theme that uses colors from the brand file. |
| 33 | +#' |
| 34 | +#' @param brand_yml The path to the brand.yml file |
| 35 | +#' |
| 36 | +#' @export |
| 37 | +ggplot_theme_brand <- function(brand_yml) { |
| 38 | + brand <- yaml::yaml.load_file(brand_yml) |
| 39 | + ggplot_theme_colors(brand$color$background, brand$color$foreground) |
| 40 | +} |
0 commit comments