|
| 1 | +--- |
| 2 | +title: "knitr dark mode - ggplot" |
| 3 | +brand: |
| 4 | + light: united-brand.yml |
| 5 | + dark: slate-brand.yml |
| 6 | +execute: |
| 7 | + echo: false |
| 8 | + warning: false |
| 9 | +_quarto: |
| 10 | + tests: |
| 11 | + html: |
| 12 | + ensureHtmlElements: |
| 13 | + - |
| 14 | + - 'body.quarto-light' |
| 15 | + - 'div.cell div.light-content' |
| 16 | + - 'div.cell div.dark-content' |
| 17 | + - 'div.cell div.cell-code pre.code-with-copy' |
| 18 | + - [] |
| 19 | + ensureFileRegexMatches: |
| 20 | + - |
| 21 | + - 'united_theme <span class="sc">\+</span> colour_scale' |
| 22 | + - 'slate_theme <span class="sc">\+</span> colour_scale' |
| 23 | + - [] |
| 24 | +--- |
| 25 | + |
| 26 | +```{r} |
| 27 | +#| echo: false |
| 28 | +#| warning: false |
| 29 | +library(ggplot2) |
| 30 | +
|
| 31 | +ggplot_theme <- function(bgcolor, fgcolor) { |
| 32 | + theme_minimal(base_size = 11) %+% |
| 33 | + theme( |
| 34 | + panel.border = element_blank(), |
| 35 | + panel.grid.major.y = element_blank(), |
| 36 | + panel.grid.minor.y = element_blank(), |
| 37 | + panel.grid.major.x = element_blank(), |
| 38 | + panel.grid.minor.x = element_blank(), |
| 39 | + text = element_text(colour = fgcolor), |
| 40 | + axis.text = element_text(colour = fgcolor), |
| 41 | + rect = element_rect(colour = bgcolor, fill = bgcolor), |
| 42 | + plot.background = element_rect(fill = bgcolor, colour = NA), |
| 43 | + axis.line = element_line(colour = fgcolor), |
| 44 | + axis.ticks = element_line(colour = fgcolor) |
| 45 | + ) |
| 46 | +} |
| 47 | +
|
| 48 | +brand_ggplot <- function(brand_yml) { |
| 49 | + brand <- yaml::yaml.load_file(brand_yml) |
| 50 | + ggplot_theme(brand$color$background, brand$color$foreground) |
| 51 | +} |
| 52 | +
|
| 53 | +united_theme <- brand_ggplot("united-brand.yml") |
| 54 | +slate_theme <- brand_ggplot("slate-brand.yml") |
| 55 | +
|
| 56 | +colour_scale <- scale_colour_manual(values = c("darkorange", "purple", "cyan4")) |
| 57 | +``` |
| 58 | + |
| 59 | +### with crossref but no caption |
| 60 | + |
| 61 | +and `echo: true` |
| 62 | + |
| 63 | +::: {#fig-thematic-ggplot} |
| 64 | + |
| 65 | +```{r} |
| 66 | +#| echo: true |
| 67 | +#| renderings: [light, dark] |
| 68 | +ggplot(mtcars, aes(mpg, disp)) + |
| 69 | + geom_point(aes(colour = factor(cyl))) + united_theme + colour_scale |
| 70 | +ggplot(mtcars, aes(mpg, disp)) + |
| 71 | + geom_point(aes(colour = factor(cyl))) + slate_theme + colour_scale |
| 72 | +``` |
| 73 | + |
| 74 | +::: |
0 commit comments