|
| 1 | +#' Shortcuts for theme settings |
| 2 | +#' |
| 3 | +#' This collection of functions serves as a shortcut for [`theme()`][theme] with |
| 4 | +#' shorter argument names. Besides the shorter arguments, it also helps in |
| 5 | +#' keeping theme declarations more organised. |
| 6 | +#' |
| 7 | +#' @eval subtheme_param_doc() |
| 8 | +#' |
| 9 | +#' @return A `theme`-class object that can be added to a plot. |
| 10 | +#' @name subtheme |
| 11 | +#' |
| 12 | +#' @examples |
| 13 | +#' # A standard plot |
| 14 | +#' p <- ggplot(mtcars, aes(disp, mpg, colour = drat)) + |
| 15 | +#' geom_point() |
| 16 | +#' |
| 17 | +#' red_text <- element_text(colour = "red") |
| 18 | +#' red_line <- element_line(colour = "red") |
| 19 | +#' |
| 20 | +#' # The theme settings below: |
| 21 | +#' p + theme( |
| 22 | +#' axis.title.x.bottom = red_text, |
| 23 | +#' axis.text.x.bottom = red_text, |
| 24 | +#' axis.line.x.bottom = red_line, |
| 25 | +#' axis.ticks.x.bottom = red_line |
| 26 | +#' ) |
| 27 | +#' |
| 28 | +#' # Are equivalent to these less verbose theme settings |
| 29 | +#' p + theme_sub_axis_bottom( |
| 30 | +#' title = red_text, |
| 31 | +#' text = red_text, |
| 32 | +#' line = red_line, |
| 33 | +#' ticks = red_line |
| 34 | +#' ) |
| 35 | +NULL |
| 36 | + |
| 37 | +subtheme <- function(elements, prefix = "", suffix = "", call = caller_env()) { |
| 38 | + if (length(elements) < 1) { |
| 39 | + return(theme()) |
| 40 | + } |
| 41 | + names(elements) <- paste0(prefix, names(elements), suffix) |
| 42 | + |
| 43 | + extra <- setdiff(names(elements), names(get_element_tree())) |
| 44 | + if (length(extra) > 0) { |
| 45 | + cli::cli_warn( |
| 46 | + "Ignoring unknown {.fn theme} element{?s}: {.and {.field {extra}}}.", |
| 47 | + call = call |
| 48 | + ) |
| 49 | + elements <- elements[setdiff(names(elements), extra)] |
| 50 | + } |
| 51 | + |
| 52 | + exec(theme, !!!elements) |
| 53 | +} |
| 54 | + |
| 55 | +#' @export |
| 56 | +#' @describeIn subtheme Theme specification for all axes. |
| 57 | +theme_sub_axis <- function(title, text, ticks, ticks.length, line) { |
| 58 | + subtheme(find_args(), "axis.") |
| 59 | +} |
| 60 | + |
| 61 | +#' @export |
| 62 | +#' @describeIn subtheme Theme specification for both x axes. |
| 63 | +theme_sub_axis_x <- function(title, text, ticks, ticks.length, line) { |
| 64 | + subtheme(find_args(), "axis.", ".x") |
| 65 | +} |
| 66 | + |
| 67 | +#' @export |
| 68 | +#' @describeIn subtheme Theme specification for both y axes. |
| 69 | +theme_sub_axis_y <- function(title, text, ticks, ticks.length, line) { |
| 70 | + subtheme(find_args(), "axis.", ".y") |
| 71 | +} |
| 72 | + |
| 73 | +#' @export |
| 74 | +#' @describeIn subtheme Theme specification for the bottom x axis. |
| 75 | +theme_sub_axis_bottom <- function(title, text, ticks, ticks.length, line) { |
| 76 | + subtheme(find_args(), "axis.", ".x.bottom") |
| 77 | +} |
| 78 | + |
| 79 | +#' @export |
| 80 | +#' @describeIn subtheme Theme specification for the top x axis. |
| 81 | +theme_sub_axis_top <- function(title, text, ticks, ticks.length, line) { |
| 82 | + subtheme(find_args(), "axis.", ".x.top") |
| 83 | +} |
| 84 | + |
| 85 | +#' @export |
| 86 | +#' @describeIn subtheme Theme specification for the left y axis. |
| 87 | +theme_sub_axis_left <- function(title, text, ticks, ticks.length, line) { |
| 88 | + subtheme(find_args(), "axis.", ".y.left") |
| 89 | +} |
| 90 | + |
| 91 | +#' @export |
| 92 | +#' @describeIn subtheme Theme specification for the right y axis. |
| 93 | +theme_sub_axis_right <- function(title, text, ticks, ticks.length, line) { |
| 94 | + subtheme(find_args(), "axis.", ".y.right") |
| 95 | +} |
| 96 | + |
| 97 | +#' @export |
| 98 | +#' @describeIn subtheme Theme specification for the legend. |
| 99 | +theme_sub_legend <- function(background, margin, spacing, spacing.x, spacing.y, |
| 100 | + key, key.size, key.height, key.width, text, title, |
| 101 | + position, direction, justification, box, box.just, |
| 102 | + box.margin, box.background, box.spacing) { |
| 103 | + subtheme(find_args(), "legend.") |
| 104 | +} |
| 105 | + |
| 106 | +#' @export |
| 107 | +#' @describeIn subtheme Theme specification for the panels. |
| 108 | +theme_sub_panel <- function(background, border, spacing, spacing.x, spacing.y, |
| 109 | + grid, grid.major, grid.minor, grid.major.x, |
| 110 | + grid.major.y, grid.minor.x, grid.minor.y, ontop) { |
| 111 | + subtheme(find_args(), "panel.") |
| 112 | +} |
| 113 | + |
| 114 | +#' @export |
| 115 | +#' @describeIn subtheme Theme specification for the whole plot. |
| 116 | +theme_sub_plot <- function(background, title, title.position, subtitle, caption, |
| 117 | + caption.position, tag, tag.position, tag.location, |
| 118 | + margin) { |
| 119 | + subtheme(find_args(), "plot.") |
| 120 | +} |
| 121 | + |
| 122 | +#' @export |
| 123 | +#' @describeIn subtheme Theme specification for facet strips. |
| 124 | +theme_sub_strip <- function(background, background.x, background.y, clip, |
| 125 | + placement, text, text.x, text.x.bottom, text.x.top, |
| 126 | + text.y, text.y.left, text.y.right, |
| 127 | + switch.pad.grid, switch.pad.wrap) { |
| 128 | + subtheme(find_args(), "strip.") |
| 129 | +} |
| 130 | + |
| 131 | +subtheme_param_doc <- function() { |
| 132 | + funs <- list( |
| 133 | + theme_sub_axis, theme_sub_axis_x, theme_sub_axis_y, theme_sub_axis_bottom, |
| 134 | + theme_sub_axis_top, theme_sub_axis_left, theme_sub_axis_right, theme_sub_legend, |
| 135 | + theme_sub_panel, theme_sub_plot, theme_sub_strip |
| 136 | + ) |
| 137 | + args <- sort(unique(unlist(lapply(funs, fn_fmls_names), use.names = FALSE))) |
| 138 | + paste0( |
| 139 | + "@param ", |
| 140 | + paste0(args, collapse = ","), |
| 141 | + " Arguments that are renamed and passed on to ", |
| 142 | + "\\code{\\link[=theme]{theme()}}." |
| 143 | + ) |
| 144 | +} |
0 commit comments