|
| 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 | +renderings: [light, dark] |
| 10 | +_quarto: |
| 11 | + tests: |
| 12 | + html: |
| 13 | + ensureHtmlElements: |
| 14 | + - |
| 15 | + - 'body.quarto-light' |
| 16 | + - 'div.cell div.light-content' |
| 17 | + - 'div.cell div.dark-content' |
| 18 | + - 'div.cell div.cell-code pre.code-with-copy' |
| 19 | + - [] |
| 20 | +--- |
| 21 | + |
| 22 | +```{r} |
| 23 | +#| echo: false |
| 24 | +#| warning: false |
| 25 | +library(ggplot2) |
| 26 | +
|
| 27 | +ggplot_theme <- function(bgcolor, fgcolor) { |
| 28 | + theme_minimal(base_size = 11) %+% |
| 29 | + theme( |
| 30 | + panel.border = element_blank(), |
| 31 | + panel.grid.major.y = element_blank(), |
| 32 | + panel.grid.minor.y = element_blank(), |
| 33 | + panel.grid.major.x = element_blank(), |
| 34 | + panel.grid.minor.x = element_blank(), |
| 35 | + text = element_text(colour = fgcolor), |
| 36 | + axis.text = element_text(colour = fgcolor), |
| 37 | + rect = element_rect(colour = bgcolor, fill = bgcolor), |
| 38 | + plot.background = element_rect(fill = bgcolor, colour = NA), |
| 39 | + axis.line = element_line(colour = fgcolor), |
| 40 | + axis.ticks = element_line(colour = fgcolor) |
| 41 | + ) |
| 42 | +} |
| 43 | +
|
| 44 | +brand_ggplot <- function(brand_yml) { |
| 45 | + brand <- yaml::yaml.load_file(brand_yml) |
| 46 | + ggplot_theme(brand$color$background, brand$color$foreground) |
| 47 | +} |
| 48 | +
|
| 49 | +united_theme <- brand_ggplot("united-brand.yml") |
| 50 | +slate_theme <- brand_ggplot("slate-brand.yml") |
| 51 | +
|
| 52 | +colour_scale <- scale_colour_manual(values = c("darkorange", "purple", "cyan4")) |
| 53 | +``` |
| 54 | + |
| 55 | +### no crossref, no caption |
| 56 | + |
| 57 | +```{r} |
| 58 | +ggplot(mtcars, aes(mpg, wt)) + |
| 59 | + geom_point(aes(colour = factor(cyl))) + |
| 60 | + united_theme + |
| 61 | + colour_scale |
| 62 | +ggplot(mtcars, aes(mpg, wt)) + |
| 63 | + geom_point(aes(colour = factor(cyl))) + |
| 64 | + slate_theme + |
| 65 | + colour_scale |
| 66 | +``` |
| 67 | + |
| 68 | +### with crossref but no caption |
| 69 | + |
| 70 | +and `echo: true` |
| 71 | + |
| 72 | +::: {#fig-ggplot} |
| 73 | + |
| 74 | +```{r} |
| 75 | +#| echo: true |
| 76 | +#| renderings: [dark, light] |
| 77 | +
|
| 78 | +# override renderings order |
| 79 | +ggplot(mtcars, aes(mpg, disp)) + |
| 80 | + geom_point(aes(colour = factor(cyl))) + |
| 81 | + slate_theme + |
| 82 | + colour_scale |
| 83 | +ggplot(mtcars, aes(mpg, disp)) + |
| 84 | + geom_point(aes(colour = factor(cyl))) + |
| 85 | + united_theme + |
| 86 | + colour_scale |
| 87 | +``` |
| 88 | + |
| 89 | +::: |
| 90 | + |
| 91 | + |
| 92 | +### with caption but no crossref |
| 93 | + |
| 94 | +<div> |
| 95 | + |
| 96 | +```{r} |
| 97 | +#| renderings: [dark] |
| 98 | +
|
| 99 | +# override number of renderings |
| 100 | +ggplot(mtcars, aes(mpg, disp)) + |
| 101 | + geom_point(aes(colour = factor(cyl))) + |
| 102 | + slate_theme + |
| 103 | + colour_scale |
| 104 | +``` |
| 105 | + |
| 106 | +ggplot - dark only |
| 107 | + |
| 108 | +</div> |
| 109 | + |
| 110 | + |
| 111 | +### with crossref and caption |
| 112 | + |
| 113 | +::: {#fig-ggplot-mpg-hp} |
| 114 | +```{r} |
| 115 | +ggplot(mtcars, aes(mpg, hp)) + |
| 116 | + geom_point(aes(colour = factor(cyl))) + |
| 117 | + united_theme + |
| 118 | + colour_scale |
| 119 | +ggplot(mtcars, aes(mpg, hp)) + |
| 120 | + geom_point(aes(colour = factor(cyl))) + |
| 121 | + slate_theme + |
| 122 | + colour_scale |
| 123 | +``` |
| 124 | + |
| 125 | +mtcars - mpg vs hp |
| 126 | +::: |
| 127 | + |
| 128 | +Here's a [link](https://example.com). |
| 129 | + |
| 130 | +{{< lipsum 3 >}} |
0 commit comments