|
| 1 | +--- |
| 2 | +title: "Theme Helpers" |
| 3 | +format: |
| 4 | + html: |
| 5 | + toc: true |
| 6 | +vignette: > |
| 7 | + %\VignetteIndexEntry{Theme Helpers} |
| 8 | + %\VignetteEngine{quarto::html} |
| 9 | + %\VignetteEncoding{UTF-8} |
| 10 | +--- |
| 11 | + |
| 12 | +## Overview |
| 13 | + |
| 14 | +The **quarto** package includes functions to help you theme plotting and table packages, for example to provide [light and dark renderings](https://quarto.org/docs/computations/execution-options.html#output-options). |
| 15 | + |
| 16 | +The functions return theme objects or functions, which are applied differently depending on the package. |
| 17 | + |
| 18 | +This vignette demonstrates adding background and foreground colors to outputs from each package. |
| 19 | + |
| 20 | +These are very simple helper functions to get you started. If you need to customize them further, these functions can be copied from quarto-r. |
| 21 | + |
| 22 | +## flextable |
| 23 | + |
| 24 | +Demonstrates a flextable with green foreground and yellow background. |
| 25 | + |
| 26 | +```{r} |
| 27 | +#| echo: false |
| 28 | +#| warning: false |
| 29 | +library(flextable) |
| 30 | +library(quarto) |
| 31 | +
|
| 32 | +yellow_green_theme <- theme_colors_flextable('#e3e5a9', '#165b26') |
| 33 | +
|
| 34 | +ft <- flextable(airquality[ sample.int(10),]) |
| 35 | +ft <- add_header_row(ft, |
| 36 | + colwidths = c(4, 2), |
| 37 | + values = c("Air quality", "Time") |
| 38 | +) |
| 39 | +ft <- theme_vanilla(ft) |
| 40 | +ft <- add_footer_lines(ft, "Daily air quality measurements in New York, May to September 1973.") |
| 41 | +ft <- color(ft, part = "footer", color = "#666666") |
| 42 | +ft <- set_caption(ft, caption = "New York Air Quality Measurements") |
| 43 | +
|
| 44 | +ft |> yellow_green_theme() |
| 45 | +``` |
| 46 | + |
| 47 | +## ggiraph |
| 48 | + |
| 49 | +Demonstrates a ggiraph interactive plot with deep blue background and mauve foreground. |
| 50 | + |
| 51 | + |
| 52 | +```{r} |
| 53 | +#| echo: false |
| 54 | +#| warning: false |
| 55 | +library(quarto) |
| 56 | +library(ggplot2) |
| 57 | +library(ggiraph) |
| 58 | +
|
| 59 | +blue_mauve_theme = theme_colors_ggplot('#111038', '#E0B0FF') |
| 60 | +
|
| 61 | +cars <- ggplot(mtcars, aes(mpg, wt)) + |
| 62 | + geom_point_interactive(aes(colour = factor(cyl), tooltip = rownames(mtcars))) + |
| 63 | + scale_colour_manual(values = c("darkorange", "purple", "cyan4")) |
| 64 | +
|
| 65 | +girafe(ggobj = cars + blue_mauve_theme) |
| 66 | +``` |
| 67 | + |
| 68 | +## ggplot2 |
| 69 | + |
| 70 | +Demonstrates a ggplot2 plot with near-black background and green-grey foreground. |
| 71 | + |
| 72 | + |
| 73 | +```{r} |
| 74 | +#| echo: false |
| 75 | +#| warning: false |
| 76 | +library(quarto) |
| 77 | +library(ggplot2) |
| 78 | +
|
| 79 | +black_greyn <- theme_colors_ggplot('#050411', '#8faf8e') |
| 80 | +
|
| 81 | +cars <- ggplot(mtcars, aes(mpg, wt)) + |
| 82 | + geom_point(aes(colour = factor(cyl))) + |
| 83 | + scale_colour_manual(values = c("darkorange", "purple", "cyan4")) |
| 84 | +
|
| 85 | +cars + black_greyn |
| 86 | +``` |
| 87 | + |
| 88 | +## gt |
| 89 | + |
| 90 | +Demonstrates a gt table with light green background and black foreground. |
| 91 | + |
| 92 | + |
| 93 | +```{r} |
| 94 | +#| echo: false |
| 95 | +#| warning: false |
| 96 | +library(gt) |
| 97 | +library(quarto) |
| 98 | +
|
| 99 | +green_black_theme <- theme_colors_gt('#a5f7d6', '#020202') |
| 100 | +
|
| 101 | +islands_tbl <- |
| 102 | + tibble( |
| 103 | + name = names(islands), |
| 104 | + size = islands |
| 105 | + ) |> |
| 106 | + slice_max(size, n = 10) |
| 107 | +
|
| 108 | +islands_tbl |> green_black_theme() |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | +## heatmaply |
| 113 | + |
| 114 | +Demonstrates a heatmaply interactive heatmap with an off-white background and blue foreground. |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +```{r} |
| 119 | +#| echo: false |
| 120 | +#| warning: false |
| 121 | +library(heatmaply) |
| 122 | +library(quarto) |
| 123 | +
|
| 124 | +offwhite_blue_theme <- theme_colors_plotly('#f4f4f7', '#2063b5') |
| 125 | +
|
| 126 | +fig <- heatmaply(mtcars, k_row = 3, k_col = 2) |
| 127 | +
|
| 128 | +fig |> offwhite_blue_theme() |
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | +## plotly |
| 133 | + |
| 134 | +Demonstrates a heatmaply interactive heatmap with a dark green background background and light blue foreground. |
| 135 | + |
| 136 | +```{r} |
| 137 | +#| echo: false |
| 138 | +#| warning: false |
| 139 | +library(quarto) |
| 140 | +library(plotly) |
| 141 | +
|
| 142 | +green_blue_theme <- theme_colors_plotly('#293a0a', '#a5eff7') |
| 143 | +
|
| 144 | +fig <- plot_ly(iris, x = ~Species, y = ~Sepal.Width, type = 'violin', |
| 145 | + box = list(visible = TRUE), |
| 146 | + meanline = list(visible = TRUE), |
| 147 | + points = 'all') |
| 148 | +
|
| 149 | +fig |> green_blue_theme() |
| 150 | +``` |
| 151 | + |
| 152 | +## thematic |
| 153 | + |
| 154 | +Demonstrates a patchwork plot with dark red background and light grey foreground. |
| 155 | + |
| 156 | + |
| 157 | +```{r} |
| 158 | +#| echo: false |
| 159 | +#| warning: false |
| 160 | +library(ggplot2) |
| 161 | +library(quarto) |
| 162 | +library(patchwork) |
| 163 | +
|
| 164 | +darkred_grey_theme <- theme_colors_thematic('#560115', '#ddeeee'); |
| 165 | +
|
| 166 | +#generate three scatterplots |
| 167 | +plot1 <- ggplot(mtcars, aes(mpg, wt)) + |
| 168 | + geom_point() |
| 169 | +
|
| 170 | +plot2 <- ggplot(mtcars, aes(mpg, disp)) + |
| 171 | + geom_point() |
| 172 | +
|
| 173 | +plot3 <- ggplot(mtcars, aes(mpg, hp)) + |
| 174 | + geom_point() |
| 175 | +
|
| 176 | +#display all three scatterplots in same graphic |
| 177 | +darkred_grey_theme() |
| 178 | +plot1 + plot2 + plot3 |
| 179 | +
|
| 180 | +``` |
| 181 | + |
0 commit comments