---
title: "knitr dark mode - ggplot"
format:
  revealjs: default
  html: default
brand:
  light: united-brand.yml
  dark: slate-brand.yml
execute:
  echo: false
  warning: false
_quarto:
  tests:
    html:
      ensureHtmlElements:
        -
          - 'body.quarto-light'
          - 'div.cell div.light-content'
          - 'div.cell div.dark-content'
          - 'div.cell div.cell-code pre.code-with-copy'
        - []
    revealjs:
      ensureHtmlElements:
        -
          - 'img[data-src="ggplot-duobrand_files/figure-revealjs/cars-1.png"]'
        -
          - 'img[data-src="ggplot-duobrand_files/figure-revealjs/cars-2.png"]'
          - 'div.light-content'
          - 'div.dark-content'
---

```{r}
#| echo: false
#| warning: false
library(ggplot2)

ggplot_theme <- function(bgcolor, fgcolor) {
  theme_minimal(base_size = 11) %+%
    theme(
      panel.border = element_blank(),
      panel.grid.major.y = element_blank(),
      panel.grid.minor.y = element_blank(),
      panel.grid.major.x = element_blank(),
      panel.grid.minor.x = element_blank(),
      text = element_text(colour = fgcolor),
      axis.text = element_text(colour = fgcolor),
      rect = element_rect(colour = bgcolor, fill = bgcolor),
      plot.background = element_rect(fill = bgcolor, colour = NA),
      axis.line = element_line(colour = fgcolor),
      axis.ticks = element_line(colour = fgcolor)
    )
}

brand_ggplot <- function(brand_yml) {
  brand <- yaml::yaml.load_file(brand_yml)
  ggplot_theme(brand$color$background, brand$color$foreground)
}

united_theme <- brand_ggplot("united-brand.yml")
slate_theme <- brand_ggplot("slate-brand.yml")

colour_scale <- scale_colour_manual(values = c("darkorange", "purple", "cyan4"))
```

```{r}
#| label: cars
#| echo: true
#| renderings: [light, dark]
ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(colour = factor(cyl))) +
  united_theme +
  colour_scale
ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(colour = factor(cyl))) +
  slate_theme +
  colour_scale
```
