From 71e5441e4537751cd42fb9a823d84de06cda483a Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 31 Jul 2023 19:08:27 +0200 Subject: [PATCH 1/9] Draft subtheme --- R/theme-sub.R | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 R/theme-sub.R diff --git a/R/theme-sub.R b/R/theme-sub.R new file mode 100644 index 0000000000..a7355d8154 --- /dev/null +++ b/R/theme-sub.R @@ -0,0 +1,134 @@ + + +#' Shortcuts for theme settings +#' +#' This collection of functions serves as a shortcut for `[theme()][theme]` with +#' shorter argument names. Besides the shorter arguments, it also helps in +#' keeping theme declarations more organised. +#' +#' @eval subtheme_param_doc() +#' +#' @return A `theme`-class object that can be added to a plot. +#' @name subtheme +#' +#' @examples +#' # A standard plot +#' p <- ggplot(mtcars, aes(disp, mpg, colour = drat)) + +#' geom_point() +#' +#' red_text <- element_text(colour = "red") +#' red_line <- element_line(colour = "red") +#' +#' # The theme settings below: +#' p + theme( +#' axis.title.x.bottom = red_text, +#' axis.text.x.bottom = red_text, +#' axis.line.x.bottom = red_line, +#' axis.ticks.x.bottom = red_line +#' ) +#' +#' # Are equivalent to these less verbose theme settings +#' p + theme_axis_bottom( +#' title = red_text, +#' text = red_text, +#' line = red_line, +#' ticks = red_line +#' ) +NULL + +subtheme <- function(elements, prefix = "", suffix = "") { + if (length(elements) < 1) { + return(theme()) + } + names(elements) <- paste0(prefix, names(elements), suffix) + exec(theme, !!!elements) +} + +#' @export +#' @describeIn subtheme Theme specification for all axes. +theme_axis <- function(title, text, ticks, ticks.length, line) { + subtheme(find_args(), "axis.") +} + +#' @export +#' @describeIn subtheme Theme specification for both x axes. +theme_axis_x <- function(title, text, ticks, ticks.length, line) { + subtheme(find_args(), "axis.", ".x") +} + +#' @export +#' @describeIn subtheme Theme specification for both y axes. +theme_axis_y <- function(title, text, ticks, ticks.length, line) { + subtheme(find_args(), "axis.", ".y") +} + +#' @export +#' @describeIn subtheme Theme specification for the bottom x axis. +theme_axis_bottom <- function(title, text, ticks, ticks.length, line) { + subtheme(find_args(), "axis.", ".x.bottom") +} + +#' @export +#' @describeIn subtheme Theme specification for the top x axis. +theme_axis_top <- function(title, text, ticks, ticks.length, line) { + subtheme(find_args(), "axis.", ".x.top") +} + +#' @export +#' @describeIn subtheme Theme specification for the left y axis. +theme_axis_left <- function(title, text, ticks, ticks.length, line) { + subtheme(find_args(), "axis.", ".y.left") +} + +#' @export +#' @describeIn subtheme Theme specification for the right y axis. +theme_axis_right <- function(title, text, ticks, ticks.length, line) { + subtheme(find_args(), "axis.", ".y.right") +} + +#' @export +#' @describeIn subtheme Theme specification for the legend. +theme_legend <- function(background, margin, spacing, spacing.x, spacing.y, + key, key.size, key.height, key.width, text, title, + position, direction, justification, box, box.just, + box.margin, box.background, box.spacing) { + subtheme(find_args(), "legend.") +} + +#' @export +#' @describeIn subtheme Theme specification for the panels. +theme_panel <- function(background, border, spacing, spacing.x, spacing.y, + grid, grid.major, grid.minor, grid.major.x, + grid.major.y, grid.minor.x, grid.minor.y, ontop) { + subtheme(find_args(), "panel.") +} + +#' @export +#' @describeIn subtheme Theme specification for the whole plot. +theme_plot <- function(background, title, title.position, subtitle, caption, + caption.position, tag, tag.position, tag.location, + margin) { + subtheme(find_args(), "plot.") +} + +#' @export +#' @describeIn subtheme Theme specification for facet strips. +theme_strip <- function(background, background.x, background.y, clip, + placement, text, text.x, text.x.bottom, text.x.top, + text.y, text.y.left, text.y.right, + switch.pad.grid, switch.pad.wrap) { + subtheme(find_args(), "strip.") +} + +subtheme_param_doc <- function() { + funs <- list(theme_axis, theme_axis_x, theme_axis_y, theme_axis_bottom, + theme_axis_top, theme_axis_left, theme_axis_right, theme_legend, + theme_panel, theme_plot, theme_strip) + args <- sort(unique(unlist(lapply(funs, fn_fmls_names), use.names = FALSE))) + paste0( + "@param ", + paste0(args, collapse = ","), + " Arguments that are renamed and passed on to ", + "\\code{\\link[=theme]{theme()}}." + ) +} From a12654f500724c8fc0b46f5f6500a9f7a16df1d8 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Tue, 19 Sep 2023 17:24:46 +0200 Subject: [PATCH 2/9] warn when elements are unknown --- R/theme-sub.R | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/R/theme-sub.R b/R/theme-sub.R index a7355d8154..8d147680fb 100644 --- a/R/theme-sub.R +++ b/R/theme-sub.R @@ -2,7 +2,7 @@ #' Shortcuts for theme settings #' -#' This collection of functions serves as a shortcut for `[theme()][theme]` with +#' This collection of functions serves as a shortcut for [`theme()`][theme] with #' shorter argument names. Besides the shorter arguments, it also helps in #' keeping theme declarations more organised. #' @@ -36,11 +36,21 @@ #' ) NULL -subtheme <- function(elements, prefix = "", suffix = "") { +subtheme <- function(elements, prefix = "", suffix = "", call = caller_env()) { if (length(elements) < 1) { return(theme()) } names(elements) <- paste0(prefix, names(elements), suffix) + + extra <- setdiff(names(elements), names(get_element_tree())) + if (length(extra) > 0) { + cli::cli_warn( + "Ignoring unknown {.fn theme} element{?s}: {.and {.field {extra}}}.", + call = call + ) + elements <- elements[setdiff(names(elements), extra)] + } + exec(theme, !!!elements) } @@ -121,9 +131,11 @@ theme_strip <- function(background, background.x, background.y, clip, } subtheme_param_doc <- function() { - funs <- list(theme_axis, theme_axis_x, theme_axis_y, theme_axis_bottom, - theme_axis_top, theme_axis_left, theme_axis_right, theme_legend, - theme_panel, theme_plot, theme_strip) + funs <- list( + theme_axis, theme_axis_x, theme_axis_y, theme_axis_bottom, + theme_axis_top, theme_axis_left, theme_axis_right, theme_legend, + theme_panel, theme_plot, theme_strip + ) args <- sort(unique(unlist(lapply(funs, fn_fmls_names), use.names = FALSE))) paste0( "@param ", From 1c3ba14e3184cc3dafa67db001ed746d4702513b Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 20 Sep 2023 11:16:55 +0200 Subject: [PATCH 3/9] Document --- DESCRIPTION | 1 + NAMESPACE | 11 ++++ R/theme-sub.R | 2 - man/subtheme.Rd | 159 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 man/subtheme.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 218c8a88bd..8dd7b2acf7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -267,6 +267,7 @@ Collate: 'theme.R' 'theme-defaults.R' 'theme-current.R' + 'theme-sub.R' 'utilities-break.R' 'utilities-grid.R' 'utilities-help.R' diff --git a/NAMESPACE b/NAMESPACE index eb67c79182..7371b91183 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -663,17 +663,28 @@ export(summarise_layout) export(sym) export(syms) export(theme) +export(theme_axis) +export(theme_axis_bottom) +export(theme_axis_left) +export(theme_axis_right) +export(theme_axis_top) +export(theme_axis_x) +export(theme_axis_y) export(theme_bw) export(theme_classic) export(theme_dark) export(theme_get) export(theme_gray) export(theme_grey) +export(theme_legend) export(theme_light) export(theme_linedraw) export(theme_minimal) +export(theme_panel) +export(theme_plot) export(theme_replace) export(theme_set) +export(theme_strip) export(theme_test) export(theme_update) export(theme_void) diff --git a/R/theme-sub.R b/R/theme-sub.R index 8d147680fb..9b74ee69ea 100644 --- a/R/theme-sub.R +++ b/R/theme-sub.R @@ -1,5 +1,3 @@ - - #' Shortcuts for theme settings #' #' This collection of functions serves as a shortcut for [`theme()`][theme] with diff --git a/man/subtheme.Rd b/man/subtheme.Rd new file mode 100644 index 0000000000..36d9d73e62 --- /dev/null +++ b/man/subtheme.Rd @@ -0,0 +1,159 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/theme-sub.R +\name{subtheme} +\alias{subtheme} +\alias{theme_axis} +\alias{theme_axis_x} +\alias{theme_axis_y} +\alias{theme_axis_bottom} +\alias{theme_axis_top} +\alias{theme_axis_left} +\alias{theme_axis_right} +\alias{theme_legend} +\alias{theme_panel} +\alias{theme_plot} +\alias{theme_strip} +\title{Shortcuts for theme settings} +\usage{ +theme_axis(title, text, ticks, ticks.length, line) + +theme_axis_x(title, text, ticks, ticks.length, line) + +theme_axis_y(title, text, ticks, ticks.length, line) + +theme_axis_bottom(title, text, ticks, ticks.length, line) + +theme_axis_top(title, text, ticks, ticks.length, line) + +theme_axis_left(title, text, ticks, ticks.length, line) + +theme_axis_right(title, text, ticks, ticks.length, line) + +theme_legend( + background, + margin, + spacing, + spacing.x, + spacing.y, + key, + key.size, + key.height, + key.width, + text, + title, + position, + direction, + justification, + box, + box.just, + box.margin, + box.background, + box.spacing +) + +theme_panel( + background, + border, + spacing, + spacing.x, + spacing.y, + grid, + grid.major, + grid.minor, + grid.major.x, + grid.major.y, + grid.minor.x, + grid.minor.y, + ontop +) + +theme_plot( + background, + title, + title.position, + subtitle, + caption, + caption.position, + tag, + tag.position, + tag.location, + margin +) + +theme_strip( + background, + background.x, + background.y, + clip, + placement, + text, + text.x, + text.x.bottom, + text.x.top, + text.y, + text.y.left, + text.y.right, + switch.pad.grid, + switch.pad.wrap +) +} +\arguments{ +\item{background, background.x, background.y, border, box, box.background, box.just, box.margin, box.spacing, caption, caption.position, clip, direction, grid, grid.major, grid.major.x, grid.major.y, grid.minor, grid.minor.x, grid.minor.y, justification, key, key.height, key.size, key.width, line, margin, ontop, placement, position, spacing, spacing.x, spacing.y, subtitle, switch.pad.grid, switch.pad.wrap, tag, tag.location, tag.position, text, text.x, text.x.bottom, text.x.top, text.y, text.y.left, text.y.right, ticks, ticks.length, title, title.position}{Arguments that are renamed and passed on to \code{\link[=theme]{theme()}}.} +} +\value{ +A \code{theme}-class object that can be added to a plot. +} +\description{ +This collection of functions serves as a shortcut for \code{\link[=theme]{theme()}} with +shorter argument names. Besides the shorter arguments, it also helps in +keeping theme declarations more organised. +} +\section{Functions}{ +\itemize{ +\item \code{theme_axis()}: Theme specification for all axes. + +\item \code{theme_axis_x()}: Theme specification for both x axes. + +\item \code{theme_axis_y()}: Theme specification for both y axes. + +\item \code{theme_axis_bottom()}: Theme specification for the bottom x axis. + +\item \code{theme_axis_top()}: Theme specification for the top x axis. + +\item \code{theme_axis_left()}: Theme specification for the left y axis. + +\item \code{theme_axis_right()}: Theme specification for the right y axis. + +\item \code{theme_legend()}: Theme specification for the legend. + +\item \code{theme_panel()}: Theme specification for the panels. + +\item \code{theme_plot()}: Theme specification for the whole plot. + +\item \code{theme_strip()}: Theme specification for facet strips. + +}} +\examples{ +# A standard plot +p <- ggplot(mtcars, aes(disp, mpg, colour = drat)) + + geom_point() + +red_text <- element_text(colour = "red") +red_line <- element_line(colour = "red") + +# The theme settings below: +p + theme( + axis.title.x.bottom = red_text, + axis.text.x.bottom = red_text, + axis.line.x.bottom = red_line, + axis.ticks.x.bottom = red_line +) + +# Are equivalent to these less verbose theme settings +p + theme_axis_bottom( + title = red_text, + text = red_text, + line = red_line, + ticks = red_line +) +} From da460b28f03ff7e1ead8cea11707a0ea7261fae8 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 20 Sep 2023 12:06:11 +0200 Subject: [PATCH 4/9] add tests --- tests/testthat/test-theme.R | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R index 9973f1128d..e3700066d8 100644 --- a/tests/testthat/test-theme.R +++ b/tests/testthat/test-theme.R @@ -492,6 +492,29 @@ test_that("Theme elements are checked during build", { expect_snapshot_error(ggplotGrob(p)) }) +test_that("subtheme functions rename arguments as intended", { + + line <- element_line(colour = "red") + rect <- element_rect(colour = "red") + + expect_equal(theme_axis(ticks = line), theme(axis.ticks = line)) + expect_equal(theme_axis_x(ticks = line), theme(axis.ticks.x = line)) + expect_equal(theme_axis_y(ticks = line), theme(axis.ticks.y = line)) + expect_equal(theme_axis_top(ticks = line), theme(axis.ticks.x.top = line)) + expect_equal(theme_axis_bottom(ticks = line), theme(axis.ticks.x.bottom = line)) + expect_equal(theme_axis_left(ticks = line), theme(axis.ticks.y.left = line)) + expect_equal(theme_axis_right(ticks = line), theme(axis.ticks.y.right = line)) + expect_equal(theme_legend(key = rect), theme(legend.key = rect)) + expect_equal(theme_panel(border = rect), theme(panel.border = rect)) + expect_equal(theme_plot(background = rect), theme(plot.background = rect)) + expect_equal(theme_strip(background = rect), theme(strip.background = rect)) + + # Test rejection of unknown theme elements + expect_snapshot_error( + subtheme(list(foo = 1, bar = 2)) + ) +}) + # Visual tests ------------------------------------------------------------ test_that("aspect ratio is honored", { From 7b0393013a76e046c3ea8265d29b93056ad3fb01 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 20 Sep 2023 12:11:34 +0200 Subject: [PATCH 5/9] Add news bullet --- NEWS.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS.md b/NEWS.md index 6fb136dcfc..eb7c379186 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ # ggplot2 (development version) +* New function family for setting parts of a theme. For example, you can now use + `theme_axis(line, text, ticks, ticks.length, line)` as a substitute for + `theme(axis.line, axis.text, axis.ticks, axis.ticks.length, axis.line)`. This + should allow slightly terser and more organised theme declarations + (@teunbrand, #5301). + * Nicer error messages for xlim/ylim arguments in coord-* functions (@92amartins, #4601, #5297). From 8936374a78d162aecba964e334a8a10b22ac6c09 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 20 Sep 2023 12:43:56 +0200 Subject: [PATCH 6/9] Tweak test --- tests/testthat/_snaps/theme.md | 4 ++++ tests/testthat/test-theme.R | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/testthat/_snaps/theme.md b/tests/testthat/_snaps/theme.md index af1b3d4744..4b25c8d9b1 100644 --- a/tests/testthat/_snaps/theme.md +++ b/tests/testthat/_snaps/theme.md @@ -53,6 +53,10 @@ `plot.tag.position` must be one of "topleft", "top", "topright", "left", "right", "bottomleft", "bottom", or "bottomright", not "test". i Did you mean "left"? +# subtheme functions rename arguments as intended + + Ignoring unknown `theme()` elements: foo and bar. + # Theme validation behaves as expected The `aspect.ratio` theme element must be a object. diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R index 6ec90c31df..bb0e7e1b2f 100644 --- a/tests/testthat/test-theme.R +++ b/tests/testthat/test-theme.R @@ -520,8 +520,11 @@ test_that("subtheme functions rename arguments as intended", { expect_equal(theme_strip(background = rect), theme(strip.background = rect)) # Test rejection of unknown theme elements - expect_snapshot_error( - subtheme(list(foo = 1, bar = 2)) + expect_snapshot_warning( + expect_equal( + subtheme(list(foo = 1, bar = 2, axis.line = line)), + theme(axis.line = line) + ) ) }) From 3943dec1836caf56beb194a0c33cfd7df427b874 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 20 Sep 2023 12:54:46 +0200 Subject: [PATCH 7/9] Add topic to pkgdown index --- _pkgdown.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/_pkgdown.yml b/_pkgdown.yml index 7dbedc3062..01df17f68d 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -178,6 +178,7 @@ reference: - theme - theme_bw - theme_update + - subtheme - element_line - margin From ae1ac75eae7611c8a95347d1732d996cdcf3bb49 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Nov 2024 11:00:42 +0100 Subject: [PATCH 8/9] rename prefix to `theme_sub_*()` --- NAMESPACE | 22 ++++++------ NEWS.md | 2 +- R/theme-sub.R | 50 +++++++++++++-------------- man/subtheme.Rd | 68 ++++++++++++++++++------------------- tests/testthat/test-theme.R | 22 ++++++------ 5 files changed, 82 insertions(+), 82 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 310834f6c1..0fab6841c5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -707,28 +707,28 @@ export(summarise_layout) export(sym) export(syms) export(theme) -export(theme_axis) -export(theme_axis_bottom) -export(theme_axis_left) -export(theme_axis_right) -export(theme_axis_top) -export(theme_axis_x) -export(theme_axis_y) export(theme_bw) export(theme_classic) export(theme_dark) export(theme_get) export(theme_gray) export(theme_grey) -export(theme_legend) export(theme_light) export(theme_linedraw) export(theme_minimal) -export(theme_panel) -export(theme_plot) export(theme_replace) export(theme_set) -export(theme_strip) +export(theme_sub_axis) +export(theme_sub_axis_bottom) +export(theme_sub_axis_left) +export(theme_sub_axis_right) +export(theme_sub_axis_top) +export(theme_sub_axis_x) +export(theme_sub_axis_y) +export(theme_sub_legend) +export(theme_sub_panel) +export(theme_sub_plot) +export(theme_sub_strip) export(theme_test) export(theme_update) export(theme_void) diff --git a/NEWS.md b/NEWS.md index 973073ee98..91128c0be9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,7 @@ # ggplot2 (development version) * New function family for setting parts of a theme. For example, you can now use - `theme_axis(line, text, ticks, ticks.length, line)` as a substitute for + `theme_sub_axis(line, text, ticks, ticks.length, line)` as a substitute for `theme(axis.line, axis.text, axis.ticks, axis.ticks.length, axis.line)`. This should allow slightly terser and more organised theme declarations (@teunbrand, #5301). diff --git a/R/theme-sub.R b/R/theme-sub.R index 9b74ee69ea..abfb178c44 100644 --- a/R/theme-sub.R +++ b/R/theme-sub.R @@ -26,7 +26,7 @@ #' ) #' #' # Are equivalent to these less verbose theme settings -#' p + theme_axis_bottom( +#' p + theme_sub_axis_bottom( #' title = red_text, #' text = red_text, #' line = red_line, @@ -54,85 +54,85 @@ subtheme <- function(elements, prefix = "", suffix = "", call = caller_env()) { #' @export #' @describeIn subtheme Theme specification for all axes. -theme_axis <- function(title, text, ticks, ticks.length, line) { +theme_sub_axis <- function(title, text, ticks, ticks.length, line) { subtheme(find_args(), "axis.") } #' @export #' @describeIn subtheme Theme specification for both x axes. -theme_axis_x <- function(title, text, ticks, ticks.length, line) { +theme_sub_axis_x <- function(title, text, ticks, ticks.length, line) { subtheme(find_args(), "axis.", ".x") } #' @export #' @describeIn subtheme Theme specification for both y axes. -theme_axis_y <- function(title, text, ticks, ticks.length, line) { +theme_sub_axis_y <- function(title, text, ticks, ticks.length, line) { subtheme(find_args(), "axis.", ".y") } #' @export #' @describeIn subtheme Theme specification for the bottom x axis. -theme_axis_bottom <- function(title, text, ticks, ticks.length, line) { +theme_sub_axis_bottom <- function(title, text, ticks, ticks.length, line) { subtheme(find_args(), "axis.", ".x.bottom") } #' @export #' @describeIn subtheme Theme specification for the top x axis. -theme_axis_top <- function(title, text, ticks, ticks.length, line) { +theme_sub_axis_top <- function(title, text, ticks, ticks.length, line) { subtheme(find_args(), "axis.", ".x.top") } #' @export #' @describeIn subtheme Theme specification for the left y axis. -theme_axis_left <- function(title, text, ticks, ticks.length, line) { +theme_sub_axis_left <- function(title, text, ticks, ticks.length, line) { subtheme(find_args(), "axis.", ".y.left") } #' @export #' @describeIn subtheme Theme specification for the right y axis. -theme_axis_right <- function(title, text, ticks, ticks.length, line) { +theme_sub_axis_right <- function(title, text, ticks, ticks.length, line) { subtheme(find_args(), "axis.", ".y.right") } #' @export #' @describeIn subtheme Theme specification for the legend. -theme_legend <- function(background, margin, spacing, spacing.x, spacing.y, - key, key.size, key.height, key.width, text, title, - position, direction, justification, box, box.just, - box.margin, box.background, box.spacing) { +theme_sub_legend <- function(background, margin, spacing, spacing.x, spacing.y, + key, key.size, key.height, key.width, text, title, + position, direction, justification, box, box.just, + box.margin, box.background, box.spacing) { subtheme(find_args(), "legend.") } #' @export #' @describeIn subtheme Theme specification for the panels. -theme_panel <- function(background, border, spacing, spacing.x, spacing.y, - grid, grid.major, grid.minor, grid.major.x, - grid.major.y, grid.minor.x, grid.minor.y, ontop) { +theme_sub_panel <- function(background, border, spacing, spacing.x, spacing.y, + grid, grid.major, grid.minor, grid.major.x, + grid.major.y, grid.minor.x, grid.minor.y, ontop) { subtheme(find_args(), "panel.") } #' @export #' @describeIn subtheme Theme specification for the whole plot. -theme_plot <- function(background, title, title.position, subtitle, caption, - caption.position, tag, tag.position, tag.location, - margin) { +theme_sub_plot <- function(background, title, title.position, subtitle, caption, + caption.position, tag, tag.position, tag.location, + margin) { subtheme(find_args(), "plot.") } #' @export #' @describeIn subtheme Theme specification for facet strips. -theme_strip <- function(background, background.x, background.y, clip, - placement, text, text.x, text.x.bottom, text.x.top, - text.y, text.y.left, text.y.right, - switch.pad.grid, switch.pad.wrap) { +theme_sub_strip <- function(background, background.x, background.y, clip, + placement, text, text.x, text.x.bottom, text.x.top, + text.y, text.y.left, text.y.right, + switch.pad.grid, switch.pad.wrap) { subtheme(find_args(), "strip.") } subtheme_param_doc <- function() { funs <- list( - theme_axis, theme_axis_x, theme_axis_y, theme_axis_bottom, - theme_axis_top, theme_axis_left, theme_axis_right, theme_legend, - theme_panel, theme_plot, theme_strip + theme_sub_axis, theme_sub_axis_x, theme_sub_axis_y, theme_sub_axis_bottom, + theme_sub_axis_top, theme_sub_axis_left, theme_sub_axis_right, theme_sub_legend, + theme_sub_panel, theme_sub_plot, theme_sub_strip ) args <- sort(unique(unlist(lapply(funs, fn_fmls_names), use.names = FALSE))) paste0( diff --git a/man/subtheme.Rd b/man/subtheme.Rd index 36d9d73e62..a05a98a54c 100644 --- a/man/subtheme.Rd +++ b/man/subtheme.Rd @@ -2,34 +2,34 @@ % Please edit documentation in R/theme-sub.R \name{subtheme} \alias{subtheme} -\alias{theme_axis} -\alias{theme_axis_x} -\alias{theme_axis_y} -\alias{theme_axis_bottom} -\alias{theme_axis_top} -\alias{theme_axis_left} -\alias{theme_axis_right} -\alias{theme_legend} -\alias{theme_panel} -\alias{theme_plot} -\alias{theme_strip} +\alias{theme_sub_axis} +\alias{theme_sub_axis_x} +\alias{theme_sub_axis_y} +\alias{theme_sub_axis_bottom} +\alias{theme_sub_axis_top} +\alias{theme_sub_axis_left} +\alias{theme_sub_axis_right} +\alias{theme_sub_legend} +\alias{theme_sub_panel} +\alias{theme_sub_plot} +\alias{theme_sub_strip} \title{Shortcuts for theme settings} \usage{ -theme_axis(title, text, ticks, ticks.length, line) +theme_sub_axis(title, text, ticks, ticks.length, line) -theme_axis_x(title, text, ticks, ticks.length, line) +theme_sub_axis_x(title, text, ticks, ticks.length, line) -theme_axis_y(title, text, ticks, ticks.length, line) +theme_sub_axis_y(title, text, ticks, ticks.length, line) -theme_axis_bottom(title, text, ticks, ticks.length, line) +theme_sub_axis_bottom(title, text, ticks, ticks.length, line) -theme_axis_top(title, text, ticks, ticks.length, line) +theme_sub_axis_top(title, text, ticks, ticks.length, line) -theme_axis_left(title, text, ticks, ticks.length, line) +theme_sub_axis_left(title, text, ticks, ticks.length, line) -theme_axis_right(title, text, ticks, ticks.length, line) +theme_sub_axis_right(title, text, ticks, ticks.length, line) -theme_legend( +theme_sub_legend( background, margin, spacing, @@ -51,7 +51,7 @@ theme_legend( box.spacing ) -theme_panel( +theme_sub_panel( background, border, spacing, @@ -67,7 +67,7 @@ theme_panel( ontop ) -theme_plot( +theme_sub_plot( background, title, title.position, @@ -80,7 +80,7 @@ theme_plot( margin ) -theme_strip( +theme_sub_strip( background, background.x, background.y, @@ -110,27 +110,27 @@ keeping theme declarations more organised. } \section{Functions}{ \itemize{ -\item \code{theme_axis()}: Theme specification for all axes. +\item \code{theme_sub_axis()}: Theme specification for all axes. -\item \code{theme_axis_x()}: Theme specification for both x axes. +\item \code{theme_sub_axis_x()}: Theme specification for both x axes. -\item \code{theme_axis_y()}: Theme specification for both y axes. +\item \code{theme_sub_axis_y()}: Theme specification for both y axes. -\item \code{theme_axis_bottom()}: Theme specification for the bottom x axis. +\item \code{theme_sub_axis_bottom()}: Theme specification for the bottom x axis. -\item \code{theme_axis_top()}: Theme specification for the top x axis. +\item \code{theme_sub_axis_top()}: Theme specification for the top x axis. -\item \code{theme_axis_left()}: Theme specification for the left y axis. +\item \code{theme_sub_axis_left()}: Theme specification for the left y axis. -\item \code{theme_axis_right()}: Theme specification for the right y axis. +\item \code{theme_sub_axis_right()}: Theme specification for the right y axis. -\item \code{theme_legend()}: Theme specification for the legend. +\item \code{theme_sub_legend()}: Theme specification for the legend. -\item \code{theme_panel()}: Theme specification for the panels. +\item \code{theme_sub_panel()}: Theme specification for the panels. -\item \code{theme_plot()}: Theme specification for the whole plot. +\item \code{theme_sub_plot()}: Theme specification for the whole plot. -\item \code{theme_strip()}: Theme specification for facet strips. +\item \code{theme_sub_strip()}: Theme specification for facet strips. }} \examples{ @@ -150,7 +150,7 @@ p + theme( ) # Are equivalent to these less verbose theme settings -p + theme_axis_bottom( +p + theme_sub_axis_bottom( title = red_text, text = red_text, line = red_line, diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R index 58487962dd..f985e27712 100644 --- a/tests/testthat/test-theme.R +++ b/tests/testthat/test-theme.R @@ -515,17 +515,17 @@ test_that("subtheme functions rename arguments as intended", { line <- element_line(colour = "red") rect <- element_rect(colour = "red") - expect_equal(theme_axis(ticks = line), theme(axis.ticks = line)) - expect_equal(theme_axis_x(ticks = line), theme(axis.ticks.x = line)) - expect_equal(theme_axis_y(ticks = line), theme(axis.ticks.y = line)) - expect_equal(theme_axis_top(ticks = line), theme(axis.ticks.x.top = line)) - expect_equal(theme_axis_bottom(ticks = line), theme(axis.ticks.x.bottom = line)) - expect_equal(theme_axis_left(ticks = line), theme(axis.ticks.y.left = line)) - expect_equal(theme_axis_right(ticks = line), theme(axis.ticks.y.right = line)) - expect_equal(theme_legend(key = rect), theme(legend.key = rect)) - expect_equal(theme_panel(border = rect), theme(panel.border = rect)) - expect_equal(theme_plot(background = rect), theme(plot.background = rect)) - expect_equal(theme_strip(background = rect), theme(strip.background = rect)) + expect_equal(theme_sub_axis(ticks = line), theme(axis.ticks = line)) + expect_equal(theme_sub_axis_x(ticks = line), theme(axis.ticks.x = line)) + expect_equal(theme_sub_axis_y(ticks = line), theme(axis.ticks.y = line)) + expect_equal(theme_sub_axis_top(ticks = line), theme(axis.ticks.x.top = line)) + expect_equal(theme_sub_axis_bottom(ticks = line), theme(axis.ticks.x.bottom = line)) + expect_equal(theme_sub_axis_left(ticks = line), theme(axis.ticks.y.left = line)) + expect_equal(theme_sub_axis_right(ticks = line), theme(axis.ticks.y.right = line)) + expect_equal(theme_sub_legend(key = rect), theme(legend.key = rect)) + expect_equal(theme_sub_panel(border = rect), theme(panel.border = rect)) + expect_equal(theme_sub_plot(background = rect), theme(plot.background = rect)) + expect_equal(theme_sub_strip(background = rect), theme(strip.background = rect)) # Test rejection of unknown theme elements expect_snapshot_warning( From b1522d31deae9d78918b641f9b924cbc376d2fd3 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Nov 2024 11:21:27 +0100 Subject: [PATCH 9/9] cleanup merge debris --- NEWS.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/NEWS.md b/NEWS.md index 91128c0be9..d9ef534b64 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,8 +5,6 @@ `theme(axis.line, axis.text, axis.ticks, axis.ticks.length, axis.line)`. This should allow slightly terser and more organised theme declarations (@teunbrand, #5301). -* `ScaleContinuous$get_breaks()` now only calls `scales::zero_range()` on limits - in transformed space, rather than in data space (#5304). * `geom_boxplot()` gains additional arguments to style the colour, linetype and linewidths of the box, whiskers, median line and staples (@teunbrand, #5126) * (internal) Using `after_scale()` in the `Geom*$default_aes()` field is now @@ -461,18 +459,6 @@ stats, facets and coords (#3329, @teunbrand) in a manner that does affects the scale range. For hiding outliers that does not affect the scale range, you can continue to use `outlier.shape = NA` (@teunbrand, #4892). - -* Binned scales now treat `NA`s in limits the same way continuous scales do - (#5355). - -* Binned scales work better with `trans = "reverse"` (#5355). - -* The `legend.text.align` and `legend.title.align` arguments in `theme()` are - deprecated. The `hjust` setting of the `legend.text` and `legend.title` - elements continues to fulfil the role of text alignment (@teunbrand, #5347). - -* Integers are once again valid input to theme arguments that expect numeric - input (@teunbrand, #5369) * Nicer error messages for xlim/ylim arguments in coord-* functions (@92amartins, #4601, #5297).