Skip to content

Commit f666daf

Browse files
authored
Add missing theme sub args (#6492)
* add panel settings * add minor ticks settings * add test for covering theme_sub items * add new arguments * accept snapshot
1 parent 0f7d790 commit f666daf

File tree

4 files changed

+131
-23
lines changed

4 files changed

+131
-23
lines changed

R/theme-sub.R

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,58 +54,73 @@ subtheme <- function(elements, prefix = "", suffix = "", call = caller_env()) {
5454

5555
#' @export
5656
#' @describeIn subtheme Theme specification for all axes.
57-
theme_sub_axis <- function(title, text, ticks, ticks.length, line) {
57+
theme_sub_axis <- function(title, text, ticks, ticks.length, line, minor.ticks.length) {
5858
subtheme(find_args(), "axis.")
5959
}
6060

6161
#' @export
6262
#' @describeIn subtheme Theme specification for both x axes.
63-
theme_sub_axis_x <- function(title, text, ticks, ticks.length, line) {
63+
theme_sub_axis_x <- function(title, text, ticks, ticks.length, line, minor.ticks.length) {
6464
subtheme(find_args(), "axis.", ".x")
6565
}
6666

6767
#' @export
6868
#' @describeIn subtheme Theme specification for both y axes.
69-
theme_sub_axis_y <- function(title, text, ticks, ticks.length, line) {
69+
theme_sub_axis_y <- function(title, text, ticks, ticks.length, line, minor.ticks.length) {
7070
subtheme(find_args(), "axis.", ".y")
7171
}
7272

7373
#' @export
7474
#' @describeIn subtheme Theme specification for the bottom x axis.
75-
theme_sub_axis_bottom <- function(title, text, ticks, ticks.length, line) {
75+
theme_sub_axis_bottom <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
7676
subtheme(find_args(), "axis.", ".x.bottom")
7777
}
7878

7979
#' @export
8080
#' @describeIn subtheme Theme specification for the top x axis.
81-
theme_sub_axis_top <- function(title, text, ticks, ticks.length, line) {
81+
theme_sub_axis_top <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
8282
subtheme(find_args(), "axis.", ".x.top")
8383
}
8484

8585
#' @export
8686
#' @describeIn subtheme Theme specification for the left y axis.
87-
theme_sub_axis_left <- function(title, text, ticks, ticks.length, line) {
87+
theme_sub_axis_left <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
8888
subtheme(find_args(), "axis.", ".y.left")
8989
}
9090

9191
#' @export
9292
#' @describeIn subtheme Theme specification for the right y axis.
93-
theme_sub_axis_right <- function(title, text, ticks, ticks.length, line) {
93+
theme_sub_axis_right <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
9494
subtheme(find_args(), "axis.", ".y.right")
9595
}
9696

9797
#' @export
9898
#' @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) {
99+
theme_sub_legend <- function(
100+
# Text stuff
101+
text, text.position, title, title.position,
102+
# Drawn elements
103+
background, frame, ticks, ticks.length, axis.line,
104+
# Spacings
105+
spacing, spacing.x, spacing.y, margin,
106+
# Seys
107+
key, key.size, key.height, key.width, key.spacing, key.spacing.x,
108+
key.spacing.y, key.justification,
109+
# Settings
110+
byrow, position, direction, location, position.inside,
111+
# Justification
112+
justification, justification.top, justification.bottom, justification.left,
113+
justification.right, justification.inside,
114+
# Box
115+
box, box.just, box.margin, box.background, box.spacing
116+
) {
103117
subtheme(find_args(), "legend.")
104118
}
105119

106120
#' @export
107121
#' @describeIn subtheme Theme specification for the panels.
108-
theme_sub_panel <- function(background, border, spacing, spacing.x, spacing.y,
122+
theme_sub_panel <- function(background, border,
123+
widths, heights, spacing, spacing.x, spacing.y,
109124
grid, grid.major, grid.minor, grid.major.x,
110125
grid.major.y, grid.minor.x, grid.minor.y, ontop) {
111126
subtheme(find_args(), "panel.")

man/subtheme.Rd

Lines changed: 63 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/theme.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,22 @@
134134
The `options('ggplot2.discrete.colour')` setting is incompatible with the `palette.colour.discrete` theme setting.
135135
i You can set `options(ggplot2.discrete.colour = NULL)`.
136136

137+
# theme elements are covered in `theme_sub_*()` functions
138+
139+
Code
140+
extra_elements
141+
Output
142+
[1] "..." "line"
143+
[3] "rect" "text"
144+
[5] "title" "point"
145+
[7] "polygon" "geom"
146+
[9] "spacing" "margins"
147+
[11] "aspect.ratio" "axis.text.theta"
148+
[13] "axis.text.r" "axis.ticks.theta"
149+
[15] "axis.ticks.r" "axis.minor.ticks.theta"
150+
[17] "axis.minor.ticks.r" "axis.ticks.length.theta"
151+
[19] "axis.ticks.length.r" "axis.minor.ticks.length.theta"
152+
[21] "axis.minor.ticks.length.r" "axis.line.theta"
153+
[23] "axis.line.r" "complete"
154+
[25] "validate"
155+

tests/testthat/test-theme.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,28 @@ test_that("geom elements are inherited correctly", {
763763
expect_equal(p$colour, "red")
764764
})
765765

766+
test_that("theme elements are covered in `theme_sub_*()` functions", {
767+
# We use a snapshot test here to trigger when a new theme element is added
768+
# or removed.
769+
# A failure of this test should be taken as a prompt to see if the new
770+
# theme element should be included in one of the `theme_sub_*` functions.
771+
772+
fmls <- paste0("axis.", fn_fmls_names(theme_sub_axis))
773+
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_x), ".x"))
774+
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_y), ".y"))
775+
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_top), ".x.top"))
776+
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_bottom), ".x.bottom"))
777+
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_left), ".y.left"))
778+
fmls <- c(fmls, paste0("axis.", fn_fmls_names(theme_sub_axis_right), ".y.right"))
779+
fmls <- c(fmls, paste0("legend.", fn_fmls_names(theme_sub_legend)))
780+
fmls <- c(fmls, paste0("plot.", fn_fmls_names(theme_sub_plot)))
781+
fmls <- c(fmls, paste0("panel.", fn_fmls_names(theme_sub_panel)))
782+
fmls <- c(fmls, paste0("strip.", fn_fmls_names(theme_sub_strip)))
783+
784+
extra_elements <- setdiff(fn_fmls_names(theme), fmls)
785+
expect_snapshot(extra_elements)
786+
})
787+
766788
# Visual tests ------------------------------------------------------------
767789

768790
test_that("element_polygon() can render a grob", {

0 commit comments

Comments
 (0)