Skip to content

Commit c55c401

Browse files
ggplot theme helpers
1 parent 60d23e4 commit c55c401

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Imports:
2929
yaml
3030
Suggests:
3131
curl,
32+
ggplot2,
3233
knitr,
3334
rsconnect (>= 0.8.26),
3435
testthat (>= 3.1.7),

R/theme.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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

Comments
 (0)